|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +<template> |
| 7 | + <section> |
| 8 | + <HeaderBar :scope="birthdate.scope" |
| 9 | + :input-id="inputId" |
| 10 | + :readable="birthdate.readable" /> |
| 11 | + |
| 12 | + <template> |
| 13 | + <NcDateTimePickerNative :id="inputId" |
| 14 | + type="date" |
| 15 | + label="" |
| 16 | + :value="value" |
| 17 | + @input="onInput" /> |
| 18 | + </template> |
| 19 | + |
| 20 | + <p class="property__helper-text-message"> |
| 21 | + {{ t('settings', 'Enter your date of birth') }} |
| 22 | + </p> |
| 23 | + </section> |
| 24 | +</template> |
| 25 | + |
| 26 | +<script> |
| 27 | +import HeaderBar from './shared/HeaderBar.vue' |
| 28 | +import AccountPropertySection from './shared/AccountPropertySection.vue' |
| 29 | +import { NAME_READABLE_ENUM } from '../../constants/AccountPropertyConstants.js' |
| 30 | +import { NcDateTimePickerNative } from '@nextcloud/vue' |
| 31 | +import debounce from 'debounce' |
| 32 | +import { savePrimaryAccountProperty } from '../../service/PersonalInfo/PersonalInfoService' |
| 33 | +import { handleError } from '../../utils/handlers' |
| 34 | +import AlertCircle from 'vue-material-design-icons/AlertCircleOutline.vue' |
| 35 | +import { loadState } from '@nextcloud/initial-state' |
| 36 | +
|
| 37 | +const { birthdate } = loadState('settings', 'personalInfoParameters', {}) |
| 38 | +
|
| 39 | +export default { |
| 40 | + name: 'BirthdaySection', |
| 41 | +
|
| 42 | + components: { |
| 43 | + AlertCircle, |
| 44 | + AccountPropertySection, |
| 45 | + NcDateTimePickerNative, |
| 46 | + HeaderBar, |
| 47 | + }, |
| 48 | +
|
| 49 | + data() { |
| 50 | + let initialValue = null |
| 51 | + if (birthdate.value) { |
| 52 | + initialValue = new Date(birthdate.value) |
| 53 | + } |
| 54 | +
|
| 55 | + return { |
| 56 | + birthdate: { |
| 57 | + ...birthdate, |
| 58 | + readable: NAME_READABLE_ENUM[birthdate.name], |
| 59 | + }, |
| 60 | + initialValue, |
| 61 | + } |
| 62 | + }, |
| 63 | +
|
| 64 | + computed: { |
| 65 | + inputId() { |
| 66 | + return `account-property-${birthdate.name}` |
| 67 | + }, |
| 68 | + value: { |
| 69 | + get() { |
| 70 | + return new Date(this.birthdate.value) |
| 71 | + }, |
| 72 | + /** @param {Date} value */ |
| 73 | + set(value) { |
| 74 | + const day = value.getDate().toString().padStart(2, '0') |
| 75 | + const month = (value.getMonth() + 1).toString().padStart(2, '0') |
| 76 | + const year = value.getFullYear() |
| 77 | + this.birthdate.value = `${year}-${month}-${day}` |
| 78 | + } |
| 79 | + }, |
| 80 | + }, |
| 81 | +
|
| 82 | + methods: { |
| 83 | + onInput(e) { |
| 84 | + this.value = e |
| 85 | + this.debouncePropertyChange(this.value) |
| 86 | + }, |
| 87 | +
|
| 88 | + debouncePropertyChange: debounce(async function(value) { |
| 89 | + await this.updateProperty(value) |
| 90 | + }, 500), |
| 91 | +
|
| 92 | + async updateProperty(value) { |
| 93 | + try { |
| 94 | + const responseData = await savePrimaryAccountProperty( |
| 95 | + this.birthdate.name, |
| 96 | + value, |
| 97 | + ) |
| 98 | + this.handleResponse({ |
| 99 | + value, |
| 100 | + status: responseData.ocs?.meta?.status, |
| 101 | + }) |
| 102 | + } catch (error) { |
| 103 | + this.handleResponse({ |
| 104 | + errorMessage: t('settings', 'Unable to update date of birth'), |
| 105 | + error, |
| 106 | + }) |
| 107 | + } |
| 108 | + }, |
| 109 | +
|
| 110 | + handleResponse({ value, status, errorMessage, error }) { |
| 111 | + if (status === 'ok') { |
| 112 | + this.initialValue = value |
| 113 | + } else { |
| 114 | + this.$emit('update:value', this.initialValue) |
| 115 | + handleError(error, errorMessage) |
| 116 | + } |
| 117 | + }, |
| 118 | + }, |
| 119 | +} |
| 120 | +</script> |
| 121 | +
|
| 122 | +<style lang="scss" scoped> |
| 123 | +section { |
| 124 | + padding: 10px 10px; |
| 125 | +
|
| 126 | + &::v-deep button:disabled { |
| 127 | + cursor: default; |
| 128 | + } |
| 129 | +
|
| 130 | + .property__helper-text-message { |
| 131 | + color: var(--color-text-maxcontrast); |
| 132 | + padding: 4px 0; |
| 133 | + display: flex; |
| 134 | + align-items: center; |
| 135 | + } |
| 136 | +} |
| 137 | +</style> |
0 commit comments