Files
bill-manager/frontend/src/lib/store/scrollingTimeFrameStore.ts
PhatPhuckDave 3ca12139cd Default display to last month
Since we pay bills for the last month in the current month...
2024-08-19 14:34:47 +02:00

33 lines
942 B
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.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) => {
frame.from.setMonth(frame.from.getMonth() + 1);
frame.to.setMonth(frame.to.getMonth() + 1);
return frame;
});
},
prev: () => {
update((frame: ScrollingTimeframe) => {
frame.from.setMonth(frame.from.getMonth() - 1);
frame.to.setMonth(frame.to.getMonth() - 1);
return frame;
});
},
};
}
export const scrollingTimeFrameStore = await createStore();