Add $lib/stores/dateStore.ts to store date in localStorage

This commit is contained in:
qowevisa 2024-11-14 19:50:22 +02:00
parent 5949160feb
commit 16222089b4

View File

@ -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();