From 16222089b4ce408ba9fb4e62c1a825b3a9b1667a Mon Sep 17 00:00:00 2001 From: qowevisa Date: Thu, 14 Nov 2024 19:50:22 +0200 Subject: [PATCH] Add $lib/stores/dateStore.ts to store date in localStorage --- src/lib/stores/dateStore.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/lib/stores/dateStore.ts diff --git a/src/lib/stores/dateStore.ts b/src/lib/stores/dateStore.ts new file mode 100644 index 0000000..97c723b --- /dev/null +++ b/src/lib/stores/dateStore.ts @@ -0,0 +1,18 @@ +import { writable } from 'svelte/store'; + +function createDateStore() { + const storedDate = typeof localStorage !== 'undefined' ? localStorage.getItem('selectedDate') : null; + const initialDate = storedDate ? storedDate : new Date().toISOString().split('T')[0]; + + const { subscribe, set } = writable(initialDate); + + return { + subscribe, + set: (newDate: string) => { + localStorage.setItem('selectedDate', newDate); + set(newDate); + } + }; +} + +export const selectedDate = createDateStore();