|
| 1 | +<!-- |
| 2 | + - @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud> |
| 3 | + - |
| 4 | + - @author Richard Steinmetz <richard@steinmetz.cloud> |
| 5 | + - |
| 6 | + - @license AGPL-3.0-or-later |
| 7 | + - |
| 8 | + - This program is free software: you can redistribute it and/or modify |
| 9 | + - it under the terms of the GNU General Public License as published by |
| 10 | + - the Free Software Foundation, either version 3 of the License, or |
| 11 | + - (at your option) any later version. |
| 12 | + - |
| 13 | + - This program is distributed in the hope that it will be useful, |
| 14 | + - but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + - GNU General Public License for more details. |
| 17 | + - |
| 18 | + - You should have received a copy of the GNU General Public License |
| 19 | + - along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + - |
| 21 | + --> |
| 22 | + |
| 23 | +<template> |
| 24 | + <div class="absence"> |
| 25 | + <div class="absence__dates"> |
| 26 | + <NcDateTimePickerNative id="absence-first-day" |
| 27 | + v-model="firstDay" |
| 28 | + :label="$t('dav', 'First day')" |
| 29 | + class="absence__dates__picker" /> |
| 30 | + <NcDateTimePickerNative id="absence-last-day" |
| 31 | + v-model="lastDay" |
| 32 | + :label="$t('dav', 'Last day (inclusive)')" |
| 33 | + class="absence__dates__picker" /> |
| 34 | + </div> |
| 35 | + <NcTextField :value.sync="status" :label="$t('dav', 'Short absence status')" /> |
| 36 | + <NcTextArea :value.sync="message" :label="$t('dav', 'Long absence Message')" /> |
| 37 | + |
| 38 | + <div class="absence__buttons"> |
| 39 | + <NcButton :disabled="loading || !valid" |
| 40 | + type="primary" |
| 41 | + @click="saveForm"> |
| 42 | + {{ $t('dav', 'Save') }} |
| 43 | + </NcButton> |
| 44 | + <NcButton :disabled="loading || !valid" |
| 45 | + type="error" |
| 46 | + @click="clearAbsence"> |
| 47 | + {{ $t('dav', 'Disable absence') }} |
| 48 | + </NcButton> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | +</template> |
| 52 | + |
| 53 | +<script> |
| 54 | +import NcButton from '@nextcloud/vue/dist/Components/NcButton.js' |
| 55 | +import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js' |
| 56 | +import NcTextArea from '@nextcloud/vue/dist/Components/NcTextArea.js' |
| 57 | +import NcDateTimePickerNative from '@nextcloud/vue/dist/Components/NcDateTimePickerNative.js' |
| 58 | +import { generateUrl } from '@nextcloud/router' |
| 59 | +import axios from '@nextcloud/axios' |
| 60 | +import { formatDateAsYMD } from '../utils/date.js' |
| 61 | +import { loadState } from '@nextcloud/initial-state' |
| 62 | +import { showError } from '@nextcloud/dialogs' |
| 63 | +
|
| 64 | +export default { |
| 65 | + name: 'AbsenceForm', |
| 66 | + components: { |
| 67 | + NcButton, |
| 68 | + NcTextField, |
| 69 | + NcTextArea, |
| 70 | + NcDateTimePickerNative, |
| 71 | + }, |
| 72 | + data() { |
| 73 | + const { firstDay, lastDay, status, message } = loadState('dav', 'absence', {}) |
| 74 | +
|
| 75 | + return { |
| 76 | + loading: false, |
| 77 | + status: status ?? '', |
| 78 | + message: message ?? '', |
| 79 | + firstDay: firstDay ? new Date(firstDay) : new Date(), |
| 80 | + lastDay: lastDay ? new Date(lastDay) : null, |
| 81 | + } |
| 82 | + }, |
| 83 | + computed: { |
| 84 | + /** |
| 85 | + * @return {boolean} |
| 86 | + */ |
| 87 | + valid() { |
| 88 | + return !!this.firstDay |
| 89 | + && !!this.lastDay |
| 90 | + && !!this.status |
| 91 | + && this.lastDay > this.firstDay |
| 92 | + }, |
| 93 | + }, |
| 94 | + methods: { |
| 95 | + resetForm() { |
| 96 | + this.status = '' |
| 97 | + this.message = '' |
| 98 | + this.firstDay = new Date() |
| 99 | + this.lastDay = null |
| 100 | + }, |
| 101 | + async saveForm() { |
| 102 | + if (!this.valid) { |
| 103 | + return |
| 104 | + } |
| 105 | +
|
| 106 | + this.loading = true |
| 107 | + try { |
| 108 | + await axios.post(generateUrl('/apps/dav/settings/absence'), { |
| 109 | + firstDay: formatDateAsYMD(this.firstDay), |
| 110 | + lastDay: formatDateAsYMD(this.lastDay), |
| 111 | + status: this.status, |
| 112 | + message: this.message, |
| 113 | + }) |
| 114 | + } catch (error) { |
| 115 | + showError(this.$t('dav', 'Failed to save your absence settings')) |
| 116 | + } finally { |
| 117 | + this.loading = false |
| 118 | + } |
| 119 | + }, |
| 120 | + async clearAbsence() { |
| 121 | + this.loading = true |
| 122 | + try { |
| 123 | + await axios.delete(generateUrl('/apps/dav/settings/absence')) |
| 124 | + this.resetForm() |
| 125 | + } catch (error) { |
| 126 | + showError(this.$t('dav', 'Failed to clear your absence settings')) |
| 127 | + } finally { |
| 128 | + this.loading = false |
| 129 | + } |
| 130 | + }, |
| 131 | + }, |
| 132 | +} |
| 133 | +</script> |
| 134 | +
|
| 135 | +<style lang="scss" scoped> |
| 136 | +.absence { |
| 137 | + display: flex; |
| 138 | + flex-direction: column; |
| 139 | + gap: 5px; |
| 140 | +
|
| 141 | + &__dates { |
| 142 | + display: flex; |
| 143 | + gap: 10px; |
| 144 | + width: 100%; |
| 145 | +
|
| 146 | + &__picker { |
| 147 | + flex: 1 auto; |
| 148 | +
|
| 149 | + ::v-deep .native-datetime-picker--input { |
| 150 | + margin-bottom: 0; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +
|
| 155 | + &__buttons { |
| 156 | + display: flex; |
| 157 | + gap: 5px; |
| 158 | + } |
| 159 | +} |
| 160 | +</style> |
0 commit comments