Add payment stores

This commit is contained in:
2024-08-19 11:37:03 +02:00
parent e33dea3e4b
commit 46b50b6bd3
4 changed files with 59 additions and 9 deletions

View File

@@ -1,17 +1,17 @@
import { type Writable, writable } from "svelte/store";
import {GetBills} from '$wails/main/App'
import { GetBills } from "$wails/main/App";
import { main } from "$wails/models";
import { toast } from "svelte-sonner";
async function createStore(): Promise<Writable<Map<number, main.Bill>>> {
async function createStore(): Promise<Writable<Map<number, main.Bill>> & { refresh: Function }> {
const bills: Map<number, main.Bill> = new Map<number, main.Bill>();
const res = await GetBills()
const res = await GetBills();
if (!res.success) {
toast.error("Error getting bills " + res.error);
} else {
for (let i = 0; i < res.data.length; i++) {
const bill = res.data[i];
bills.set(bill.id, bill)
bills.set(bill.id, bill);
}
}
@@ -20,15 +20,14 @@ async function createStore(): Promise<Writable<Map<number, main.Bill>>> {
subscribe,
update,
set,
// @ts-ignore
refresh: async () => {
const res = await GetBills()
const res = await GetBills();
if (!res.success) {
toast.error("Error getting bills " + res.error);
} else {
for (let i = 0; i < res.data.length; i++) {
const bill = res.data[i];
bills.set(bill.id, bill)
bills.set(bill.id, bill);
}
}
},

View File

@@ -0,0 +1,26 @@
import { get, type Writable, writable } from "svelte/store";
import { GetPaymentsForMonth } from "$wails/main/App";
import { main } from "$wails/models";
import { toast } from "svelte-sonner";
import { nowStore } from "$lib/store/nowStore";
async function createStore(): Promise<Writable<main.Payment[]>> {
const payments: main.Payment[] = [];
const now = new Date();
now.setMonth(get(nowStore).getMonth() - 1)
const res = await GetPaymentsForMonth(now);
if (!res.success) {
toast.error("Error getting payments " + res.error);
} else {
payments.push(...res.data);
}
const { subscribe, update, set } = writable(payments);
return {
subscribe,
update,
set,
};
}
export const lastMonthPaymentsStore = await createStore();

View File

@@ -9,13 +9,13 @@ async function createStore(): Promise<Writable<Date> & { next: Function; prev: F
next: () => {
update((now: Date) => {
now.setMonth(now.getMonth() + 1);
return now
return now;
});
},
prev: () => {
update((now: Date) => {
now.setMonth(now.getMonth() - 1);
return now
return now;
});
},
};

View File

@@ -0,0 +1,25 @@
import { get, type Writable, writable } from "svelte/store";
import { GetPaymentsForMonth } from "$wails/main/App";
import { main } from "$wails/models";
import { toast } from "svelte-sonner";
import { nowStore } from "$lib/store/nowStore";
async function createStore(): Promise<Writable<main.Payment[]>> {
const payments: main.Payment[] = [];
const now = get(nowStore)
const res = await GetPaymentsForMonth(now);
if (!res.success) {
toast.error("Error getting payments " + res.error);
} else {
payments.push(...res.data);
}
const { subscribe, update, set } = writable(payments);
return {
subscribe,
update,
set,
};
}
export const thisMonthPaymentsStore = await createStore();