feat: defer-task script setup (#1929)

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/1929
Reviewed-by: konrad <k@knt.li>
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni 2022-05-22 20:50:03 +00:00 committed by konrad
parent e6af4772fb
commit 1d869a0497

View file

@ -37,101 +37,99 @@
</div> </div>
</template> </template>
<script lang="ts"> <script setup lang="ts">
import {defineComponent} from 'vue' import {ref, shallowReactive, computed, watch, onMounted, onBeforeUnmount} from 'vue'
import {useStore} from 'vuex'
import TaskService from '../../../services/task' import {useI18n} from 'vue-i18n'
import flatPickr from 'vue-flatpickr-component' import flatPickr from 'vue-flatpickr-component'
export default defineComponent({ import TaskService from '@/services/task'
name: 'defer-task', import TaskModel from '@/models/task'
data() {
return { const props = defineProps({
taskService: new TaskService(),
task: null,
// We're saving the due date seperately to prevent null errors in very short periods where the task is null.
dueDate: null,
lastValue: null,
changeInterval: null,
}
},
components: {
flatPickr,
},
props: {
modelValue: { modelValue: {
type: TaskModel,
required: true, required: true,
}, },
}, })
emits: ['update:modelValue'], const emit = defineEmits(['update:modelValue'])
watch: { const {t} = useI18n()
modelValue: { const store = useStore()
handler(value) {
this.task = value const taskService = shallowReactive(new TaskService())
this.dueDate = value.dueDate const task = ref<TaskModel>()
this.lastValue = value.dueDate
// We're saving the due date seperately to prevent null errors in very short periods where the task is null.
const dueDate = ref<Date>()
const lastValue = ref<Date>()
const changeInterval = ref<number>()
watch(
() => props.modelValue,
(value) => {
task.value = value
dueDate.value = value.dueDate
lastValue.value = value.dueDate
}, },
immediate: true, {immediate: true},
}, )
},
mounted() { onMounted(() => {
// Because we don't really have other ways of handling change since if we let flatpickr // Because we don't really have other ways of handling change since if we let flatpickr
// change events trigger updates, it would trigger a flatpickr change event which would trigger // change events trigger updates, it would trigger a flatpickr change event which would trigger
// an update which would trigger a change event and so on... // an update which would trigger a change event and so on...
// This is either a bug in flatpickr or in the vue component of it. // This is either a bug in flatpickr or in the vue component of it.
// To work around that, we're only updating if something changed and check each second and when closing the popup. // To work around that, we're only updating if something changed and check each second and when closing the popup.
if (this.changeInterval) { if (changeInterval.value) {
clearInterval(this.changeInterval) clearInterval(changeInterval.value)
} }
this.changeInterval = setInterval(this.updateDueDate, 1000) changeInterval.value = setInterval(updateDueDate, 1000)
}, })
beforeUnmount() {
if (this.changeInterval) { onBeforeUnmount(() => {
clearInterval(this.changeInterval) if (changeInterval.value) {
clearInterval(changeInterval.value)
} }
this.updateDueDate() updateDueDate()
}, })
computed: {
flatPickerConfig() { const flatPickerConfig = computed(() => ({
return { altFormat: t('date.altFormatLong'),
altFormat: this.$t('date.altFormatLong'),
altInput: true, altInput: true,
dateFormat: 'Y-m-d H:i', dateFormat: 'Y-m-d H:i',
enableTime: true, enableTime: true,
time_24hr: true, time_24hr: true,
inline: true, inline: true,
locale: { locale: {
firstDayOfWeek: this.$store.state.auth.settings.weekStart, firstDayOfWeek: store.state.auth.settings.weekStart,
},
}
},
},
methods: {
deferDays(days) {
this.dueDate = new Date(this.dueDate)
this.dueDate = this.dueDate.setDate(this.dueDate.getDate() + days)
this.updateDueDate()
}, },
}))
async updateDueDate() { function deferDays(days: number) {
if (!this.dueDate) { dueDate.value = new Date(dueDate.value)
const currentDate = new Date(dueDate.value).getDate()
dueDate.value = new Date(dueDate.value).setDate(currentDate + days)
updateDueDate()
}
async function updateDueDate() {
if (!dueDate.value) {
return return
} }
if (+new Date(this.dueDate) === +this.lastValue) { if (+new Date(dueDate.value) === +lastValue.value) {
return return
} }
this.task.dueDate = new Date(this.dueDate) // FIXME: direct prop manipulation
const task = await this.taskService.update(this.task) task.value.dueDate = new Date(dueDate.value)
this.lastValue = task.dueDate const newTask = await taskService.update(task.value)
this.task = task lastValue.value = newTask.dueDate
this.$emit('update:modelValue', task) task.value = newTask
}, emit('update:modelValue', newTask)
}, }
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>