From 69eee93cff472d2de6b2c94f6bcc7169c002b1b7 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Mon, 19 Aug 2024 11:10:57 +0200 Subject: [PATCH] Add billsstore --- frontend/src/App.svelte | 4 +++ frontend/src/lib/store/billsStore.ts | 38 ++++++++++++++++++++++++++++ main.go | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 frontend/src/lib/store/billsStore.ts diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 7cab344..22b15a5 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -2,7 +2,11 @@ import { Toaster } from "svelte-sonner"; import Header from "$lib/components/Header.svelte"; import Router from "$lib/router/Router.svelte"; + import { billsStore } from "$lib/store/billsStore"; import { Close } from '$wails/main/App' + + console.log($billsStore); + function keyDown(event: KeyboardEvent) { if (event.ctrlKey && event.key == "r") { window.location.reload(); diff --git a/frontend/src/lib/store/billsStore.ts b/frontend/src/lib/store/billsStore.ts new file mode 100644 index 0000000..1e9426e --- /dev/null +++ b/frontend/src/lib/store/billsStore.ts @@ -0,0 +1,38 @@ +import { type Writable, writable } from "svelte/store"; +import {GetBills} from '$wails/main/App' +import { main } from "$wails/models"; +import { toast } from "svelte-sonner"; + +async function createStore(): Promise>> { + const bills: Map = new Map(); + 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) + } + } + + const { subscribe, update, set } = writable(bills); + return { + subscribe, + update, + set, + // @ts-ignore + refresh: async () => { + 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) + } + } + }, + }; +} + +export const billsStore = await createStore(); diff --git a/main.go b/main.go index fc460da..49a770e 100644 --- a/main.go +++ b/main.go @@ -61,7 +61,7 @@ func main() { // Create application with options err = wails.Run(&options.App{ - Title: "bill-manager-w", + Title: "bill-manager", Width: 1024, Height: 768, AssetServer: &assetserver.Options{