1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-23 14:59:36 +02:00

refactor: Simplify date handling by replacing updateLocalDates and updateUTCDates with updateLocalDate and updateUTCDate functions

This commit is contained in:
Sean Morley 2025-04-19 21:53:17 -04:00
parent c12f94855d
commit 85b4db87ec
2 changed files with 48 additions and 65 deletions

View file

@ -6,14 +6,7 @@
import { addToast } from '$lib/toasts'; import { addToast } from '$lib/toasts';
let modal: HTMLDialogElement; let modal: HTMLDialogElement;
import { t } from 'svelte-i18n'; import { t } from 'svelte-i18n';
import { import { updateLocalDate, updateUTCDate, validateDateRange, formatUTCDate } from '$lib/dateUtils';
toLocalDatetime,
toUTCDatetime,
updateLocalDates,
updateUTCDates,
validateDateRange,
formatUTCDate
} from '$lib/dateUtils';
// Initialize with browser's timezone // Initialize with browser's timezone
let selectedTimezone: string = Intl.DateTimeFormat().resolvedOptions().timeZone; let selectedTimezone: string = Intl.DateTimeFormat().resolvedOptions().timeZone;
@ -79,13 +72,14 @@
// Update local display dates whenever timezone or UTC dates change // Update local display dates whenever timezone or UTC dates change
$: { $: {
const updatedDates = updateLocalDates({ localStartDate = updateLocalDate({
utcStartDate, utcDate: utcStartDate,
utcEndDate,
timezone: selectedTimezone timezone: selectedTimezone
}); }).localDate;
localStartDate = updatedDates.localStartDate; localEndDate = updateLocalDate({
localEndDate = updatedDates.localEndDate; utcDate: utcEndDate,
timezone: selectedTimezone
}).localDate;
} }
onMount(async () => { onMount(async () => {
@ -103,14 +97,14 @@
utcEndDate = transportationToEdit.end_date; utcEndDate = transportationToEdit.end_date;
} }
// Update local dates based on UTC dates localStartDate = updateLocalDate({
const updatedDates = updateLocalDates({ utcDate: utcStartDate,
utcStartDate,
utcEndDate,
timezone: selectedTimezone timezone: selectedTimezone
}); }).localDate;
localStartDate = updatedDates.localStartDate; localEndDate = updateLocalDate({
localEndDate = updatedDates.localEndDate; utcDate: utcEndDate,
timezone: selectedTimezone
}).localDate;
}); });
function close() { function close() {
@ -125,13 +119,14 @@
// Update UTC dates when local dates change // Update UTC dates when local dates change
function handleLocalDateChange() { function handleLocalDateChange() {
const updated = updateUTCDates({ utcStartDate = updateUTCDate({
localStartDate, localDate: localStartDate,
localEndDate,
timezone: selectedTimezone timezone: selectedTimezone
}); }).utcDate;
utcStartDate = updated.utcStartDate; utcEndDate = updateUTCDate({
utcEndDate = updated.utcEndDate; localDate: localEndDate,
timezone: selectedTimezone
}).utcDate;
} }
async function geocode(e: Event | null) { async function geocode(e: Event | null) {
@ -264,14 +259,14 @@
utcStartDate = data.date; utcStartDate = data.date;
utcEndDate = data.end_date; utcEndDate = data.end_date;
// Update displayed dates using utility function localStartDate = updateLocalDate({
const updatedDates = updateLocalDates({ utcDate: utcStartDate,
utcStartDate,
utcEndDate,
timezone: selectedTimezone timezone: selectedTimezone
}); }).localDate;
localStartDate = updatedDates.localStartDate; localEndDate = updateLocalDate({
localEndDate = updatedDates.localEndDate; utcDate: utcEndDate,
timezone: selectedTimezone
}).localDate;
addToast('success', $t('adventures.adventure_created')); addToast('success', $t('adventures.adventure_created'));
dispatch('save', transportation); dispatch('save', transportation);
@ -294,14 +289,14 @@
utcStartDate = data.date; utcStartDate = data.date;
utcEndDate = data.end_date; utcEndDate = data.end_date;
// Update displayed dates using utility function localStartDate = updateLocalDate({
const updatedDates = updateLocalDates({ utcDate: utcStartDate,
utcStartDate,
utcEndDate,
timezone: selectedTimezone timezone: selectedTimezone
}); }).localDate;
localStartDate = updatedDates.localStartDate; localEndDate = updateLocalDate({
localEndDate = updatedDates.localEndDate; utcDate: utcEndDate,
timezone: selectedTimezone
}).localDate;
addToast('success', $t('adventures.adventure_updated')); addToast('success', $t('adventures.adventure_updated'));
dispatch('save', transportation); dispatch('save', transportation);

View file

@ -33,42 +33,30 @@ export function toUTCDatetime(
} }
/** /**
* Updates local datetime display values based on UTC values and timezone * Updates local datetime values based on UTC date and timezone
* @param params Object containing UTC dates and timezone * @param params Object containing UTC date and timezone
* @returns Object with updated local datetime strings * @returns Object with updated local datetime string
*/ */
export function updateLocalDates({ export function updateLocalDate({
utcStartDate, utcDate,
utcEndDate,
timezone timezone
}: { }: {
utcStartDate: string | null; utcDate: string | null;
utcEndDate: string | null;
timezone: string; timezone: string;
}) { }) {
return { return {
localStartDate: toLocalDatetime(utcStartDate, timezone), localDate: toLocalDatetime(utcDate, timezone)
localEndDate: toLocalDatetime(utcEndDate, timezone)
}; };
} }
/** /**
* Updates UTC datetime values based on local values and timezone * Updates UTC datetime values based on local datetime and timezone
* @param params Object containing local dates and timezone * @param params Object containing local date and timezone
* @returns Object with updated UTC datetime strings * @returns Object with updated UTC datetime string
*/ */
export function updateUTCDates({ export function updateUTCDate({ localDate, timezone }: { localDate: string; timezone: string }) {
localStartDate,
localEndDate,
timezone
}: {
localStartDate: string;
localEndDate: string;
timezone: string;
}) {
return { return {
utcStartDate: toUTCDatetime(localStartDate, timezone), utcDate: toUTCDatetime(localDate, timezone)
utcEndDate: toUTCDatetime(localEndDate, timezone)
}; };
} }