From 118aa36f7862d7be113700069ffaf5aa429ea460 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Thu, 18 Dec 2025 11:37:05 +0100 Subject: [PATCH] Fix calculation of optionEnd Signed-off-by: dartcafe --- src/components/Base/modules/DateBox.vue | 103 +++++++----------- src/components/Options/OptionItem.vue | 2 +- src/components/Options/OptionItemDateBox.vue | 2 +- .../Options/OptionsDateAddDialog.vue | 4 +- src/composables/optionDateTime.ts | 73 +++++++++++++ 5 files changed, 118 insertions(+), 66 deletions(-) create mode 100644 src/composables/optionDateTime.ts diff --git a/src/components/Base/modules/DateBox.vue b/src/components/Base/modules/DateBox.vue index 7f03baaa8e..3161803b39 100644 --- a/src/components/Base/modules/DateBox.vue +++ b/src/components/Base/modules/DateBox.vue @@ -5,100 +5,79 @@ diff --git a/src/components/Options/OptionItem.vue b/src/components/Options/OptionItem.vue index ff5758034f..0f0692dc16 100644 --- a/src/components/Options/OptionItem.vue +++ b/src/components/Options/OptionItem.vue @@ -57,7 +57,7 @@ const pollStore = usePollStore() diff --git a/src/components/Options/OptionItemDateBox.vue b/src/components/Options/OptionItemDateBox.vue index 92ea44a623..c8ae9debef 100644 --- a/src/components/Options/OptionItemDateBox.vue +++ b/src/components/Options/OptionItemDateBox.vue @@ -22,7 +22,7 @@ const duration = Duration.fromMillis(durationSeconds * 1000) diff --git a/src/components/Options/OptionsDateAddDialog.vue b/src/components/Options/OptionsDateAddDialog.vue index 36d644ea84..482b7657e2 100644 --- a/src/components/Options/OptionsDateAddDialog.vue +++ b/src/components/Options/OptionsDateAddDialog.vue @@ -279,7 +279,7 @@ async function addOption(): Promise {
{
diff --git a/src/composables/optionDateTime.ts b/src/composables/optionDateTime.ts new file mode 100644 index 0000000000..941b70db62 --- /dev/null +++ b/src/composables/optionDateTime.ts @@ -0,0 +1,73 @@ +/** + * SPDX-FileCopyrightText: 2024 Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { DateTime, Duration, Interval } from 'luxon' +import { ref, toValue, watchEffect } from 'vue' + +/** + * returns the width of the element with the given id + * + * @param elementId the id of the element whose width should be checked + * @param elWidthOffset the width offset to check against + */ + +export const dateFrom = ref() + +export function getDates(optionStart: DateTime, optionDuration: Duration | null) { + const endDate = ref(optionStart) + const localDuration = ref(null as Duration | null) + const interval = ref(Interval.fromDateTimes(optionStart, optionStart)) + + const fullDays = ref(false) + const isSameMonth = ref(false) + const isSameDay = ref(false) + const isSameTime = ref(false) + + const calculateValues = () => { + endDate.value = toValue(optionStart) + localDuration.value = toValue(optionDuration) + fullDays.value = false + + if (!localDuration.value) { + // without duration, no further calculation is possible + // end date remains the same as from date + return + } + + // Check if the duration represents full days + fullDays.value = + optionStart.valueOf() === optionStart.startOf('day').valueOf() + && localDuration.value.hours + localDuration.value.minutes === 0 + + // If full days are selected and duration is 0 days, set duration to 1 day + if (fullDays.value && localDuration.value.as('days') === 0) { + optionDuration = Duration.fromObject({ days: 1 }) + } + + endDate.value = optionStart.plus(localDuration.value) + // If full days are selected, subtract 1 millisecond for display purposes + if (fullDays.value) { + endDate.value = endDate.value.minus({ milliseconds: 1 }) + } + isSameMonth.value = optionStart.hasSame(endDate.value, 'month') + isSameDay.value = optionStart.hasSame(endDate.value, 'day') + isSameTime.value = localDuration.value.as('minutes') === 0 + interval.value = Interval.fromDateTimes(optionStart, endDate.value) + } + + watchEffect(() => { + calculateValues() + }) + + return { + optionStart, + optionEnd: endDate.value, + isFullDays: fullDays.value, + isSameMonth: isSameMonth.value, + isSameDay: isSameDay.value, + isSameTime: isSameTime.value, + optionInterval: interval.value, + } +}