Remove vuex
This commit is contained in:
111
web/src/stores/budget-account.ts
Normal file
111
web/src/stores/budget-account.ts
Normal file
@ -0,0 +1,111 @@
|
||||
import { defineStore } from "pinia"
|
||||
import { FETCH_ACCOUNT } from "../store/action-types";
|
||||
import { useAPI } from "./api";
|
||||
import { useSessionStore } from "./session";
|
||||
|
||||
interface State {
|
||||
Accounts: Map<string, Account>,
|
||||
CurrentAccountID: string | null,
|
||||
Categories: Map<string, Category>,
|
||||
Months: Map<number, Map<number, Map<string, Category>>>,
|
||||
Transactions: [],
|
||||
Assignments: []
|
||||
}
|
||||
|
||||
export interface Account {
|
||||
ID: string
|
||||
Name: string
|
||||
OnBudget: boolean
|
||||
Balance: Number
|
||||
}
|
||||
|
||||
export interface Category {
|
||||
ID: string
|
||||
Group: string
|
||||
Name: string
|
||||
AvailableLastMonth: number
|
||||
Assigned: number
|
||||
Activity: number
|
||||
Available: number
|
||||
}
|
||||
|
||||
export const useAccountStore = defineStore("budget/account", {
|
||||
state: (): State => ({
|
||||
Accounts: new Map<string, Account>(),
|
||||
CurrentAccountID: null,
|
||||
Months: new Map<number, Map<number, Map<string, Category>>>(),
|
||||
Categories: new Map<string, Category>(),
|
||||
Transactions: [],
|
||||
Assignments: []
|
||||
}),
|
||||
getters: {
|
||||
AccountsList(state) {
|
||||
return state.Accounts.values();
|
||||
},
|
||||
Categories: (state) => (year : number, month : number) => {
|
||||
const yearMap = state.Months.get(year);
|
||||
return yearMap?.get(month)?.values();
|
||||
},
|
||||
CurrentAccount(state) : Account | undefined {
|
||||
if (state.CurrentAccountID == null)
|
||||
return undefined;
|
||||
|
||||
return state.Accounts.get(state.CurrentAccountID);
|
||||
},
|
||||
OnBudgetAccounts(state) {
|
||||
return Array.from(state.Accounts.values()).filter(x => x.OnBudget);
|
||||
},
|
||||
OnBudgetAccountsBalance(state) : Number {
|
||||
return this.OnBudgetAccounts.reduce((prev, curr) => prev + Number(curr.Balance), 0);
|
||||
},
|
||||
OffBudgetAccounts(state) {
|
||||
return Array.from(state.Accounts.values()).filter(x => !x.OnBudget);
|
||||
},
|
||||
OffBudgetAccountsBalance(state) : Number {
|
||||
return this.OffBudgetAccounts.reduce((prev, curr) => prev + Number(curr.Balance), 0);
|
||||
},
|
||||
Transactions(state) {
|
||||
return (state.Transactions || []);
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async SetCurrentAccount(budgetid : string, accountid : string) {
|
||||
if (budgetid == null)
|
||||
return
|
||||
|
||||
this.CurrentAccountID = accountid;
|
||||
if (this.CurrentAccount == undefined)
|
||||
return
|
||||
|
||||
useSessionStore().setTitle(this.CurrentAccount.Name);
|
||||
await this.FetchAccount(accountid);
|
||||
},
|
||||
async FetchAccount(accountid : string) {
|
||||
const api = useAPI();
|
||||
const result = await api.GET("/api/v1/account/" + accountid + "/transactions");
|
||||
const response = await result.json();
|
||||
this.Transactions = response.Transactions;
|
||||
},
|
||||
async FetchMonthBudget(budgetid : string, month : number, year : number) {
|
||||
const api = useAPI();
|
||||
const result = await api.GET("/budget/" + budgetid + "/" + year + "/" + month);
|
||||
const response = await result.json();
|
||||
this.addCategoriesForMonth(year, month, response.Categories);
|
||||
},
|
||||
addCategoriesForMonth(year : number, month : number, categories : Category[]) : void {
|
||||
const yearMap = this.Months.get(year) || new Map<number, Map<string, Category>>();
|
||||
this.Months.set(year, yearMap);
|
||||
|
||||
const monthMap = yearMap.get(month) || new Map<string, Category>();
|
||||
yearMap.set(month, monthMap);
|
||||
|
||||
for (const category of categories){
|
||||
monthMap.set(category.ID, category);
|
||||
}
|
||||
},
|
||||
logout() {
|
||||
this.$reset()
|
||||
},
|
||||
}
|
||||
|
||||
})
|
Reference in New Issue
Block a user