From a4524823817b99d2ef7850444adf63646ad54be1 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 20:47:58 +0000 Subject: [PATCH] Normalize transaction store --- web/src/stores/budget-account.ts | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/web/src/stores/budget-account.ts b/web/src/stores/budget-account.ts index 1f0f3d2..b77c094 100644 --- a/web/src/stores/budget-account.ts +++ b/web/src/stores/budget-account.ts @@ -8,7 +8,7 @@ interface State { CurrentAccountID: string | null, Categories: Map, Months: Map>>, - Transactions: any[], + Transactions: Map, Assignments: [] } @@ -30,6 +30,7 @@ export interface Account { Name: string OnBudget: boolean Balance: number + Transactions: string[] } export interface Category { @@ -48,7 +49,7 @@ export const useAccountStore = defineStore("budget/account", { CurrentAccountID: null, Months: new Map>>(), Categories: new Map(), - Transactions: [], + Transactions: new Map(), Assignments: [] }), getters: { @@ -100,8 +101,10 @@ export const useAccountStore = defineStore("budget/account", { OffBudgetAccountsBalance(state): number { return this.OffBudgetAccounts.reduce((prev, curr) => prev + Number(curr.Balance), 0); }, - TransactionsList(state) { - return (state.Transactions || []); + TransactionsList(state) : Transaction[] { + return this.CurrentAccount!.Transactions.map(x => { + return this.Transactions.get(x)! + }); } }, actions: { @@ -110,16 +113,21 @@ export const useAccountStore = defineStore("budget/account", { return this.CurrentAccountID = accountid; - if (this.CurrentAccount == undefined) + const account = this.CurrentAccount; + if (account == undefined) return - useSessionStore().setTitle(this.CurrentAccount.Name); - await this.FetchAccount(accountid); + useSessionStore().setTitle(account.Name); + await this.FetchAccount(account); }, - async FetchAccount(accountid: string) { - const result = await GET("/account/" + accountid + "/transactions"); + async FetchAccount(account: Account) { + const result = await GET("/account/" + account.ID + "/transactions"); const response = await result.json(); - this.Transactions = response.Transactions; + account.Transactions = []; + for (const transaction of response.Transactions) { + this.Transactions.set(transaction.ID, transaction); + account.Transactions.push(transaction.ID); + } }, async FetchMonthBudget(budgetid: string, year: number, month: number) { const result = await GET("/budget/" + budgetid + "/" + year + "/" + month); @@ -151,7 +159,7 @@ export const useAccountStore = defineStore("budget/account", { async saveTransaction(payload: string) { const result = await POST("/transaction/new", payload); const response = await result.json(); - this.Transactions.unshift(response); + this.CurrentAccount?.Transactions.unshift(response); } }