Files
bill-manager/frontend/src/lib/store/scrollingTimeFrameStore.ts

39 lines
1.1 KiB
TypeScript

import { type ScrollingTimeframe } from "$lib/types";
import { type Writable, writable } from "svelte/store";
async function createStore(): Promise<Writable<ScrollingTimeframe> & { next: Function; prev: Function }> {
const thism = new Date();
const lastm = new Date();
thism.setDate(1);
lastm.setDate(1);
thism.setMonth(thism.getMonth() - 1);
lastm.setMonth(lastm.getMonth() - 2);
const { subscribe, update, set } = writable({ from: lastm, to: thism });
return {
subscribe,
update,
set,
next: () => {
update((frame: ScrollingTimeframe) => {
const newFrom = new Date(frame.from);
const newTo = new Date(frame.to);
newFrom.setMonth(newFrom.getMonth() + 1);
newTo.setMonth(newTo.getMonth() + 1);
return { from: newFrom, to: newTo };
});
},
prev: () => {
update((frame: ScrollingTimeframe) => {
const newFrom = new Date(frame.from);
const newTo = new Date(frame.to);
newFrom.setMonth(newFrom.getMonth() - 1);
newTo.setMonth(newTo.getMonth() - 1);
return { from: newFrom, to: newTo };
});
},
};
}
export const scrollingTimeFrameStore = await createStore();