117 lines
3.5 KiB
TypeScript
117 lines
3.5 KiB
TypeScript
import { Module } from "vuex";
|
|
import { FETCH_ACCOUNT, FETCH_BUDGET, SET_CURRENT_ACCOUNT } from "../action-types";
|
|
import { LOGOUT, TITLE } from "../mutation-types";
|
|
|
|
export interface BudgetState {
|
|
Accounts: Map<string, Account>,
|
|
CurrentAccountID?: string,
|
|
Categories: Map<string, Category>,
|
|
Transactions: [],
|
|
Assignments: []
|
|
}
|
|
|
|
export interface Account {
|
|
ID: string
|
|
OnBudget: boolean
|
|
}
|
|
|
|
export interface Category {
|
|
Group: string
|
|
Name: string
|
|
AvailableLastMonth: number
|
|
Assigned: number
|
|
Activity: number
|
|
Available: number
|
|
}
|
|
|
|
export const budgetStore : Module<BudgetState, any> = {
|
|
state: {
|
|
Accounts: new Map<string, Account>(),
|
|
CurrentAccountID: undefined,
|
|
Categories: new Map<string, Category>(),
|
|
Transactions: [],
|
|
Assignments: []
|
|
},
|
|
|
|
mutations: {
|
|
initializeStore(state) {
|
|
const store = localStorage.getItem("store");
|
|
if (!store)
|
|
return;
|
|
|
|
const restoredState = JSON.parse(store);
|
|
if (!restoredState)
|
|
return;
|
|
|
|
state.CurrentAccountID = restoredState.CurrentAccountID;
|
|
|
|
for (const account of restoredState.Accounts || []) {
|
|
state.Accounts.set(account[0], account[1]);
|
|
}
|
|
},
|
|
[LOGOUT](state) {
|
|
state.Accounts.clear();
|
|
state.Categories.clear();
|
|
state.Transactions = [];
|
|
state.Assignments = [];
|
|
},
|
|
addAccount(state, account) {
|
|
state.Accounts.set(account.ID, account);
|
|
},
|
|
addCategory(state, category) {
|
|
state.Categories.set(category.ID, category);
|
|
},
|
|
setCurrentAccountID(state, accountid) {
|
|
state.CurrentAccountID = accountid;
|
|
},
|
|
setTransactions(state, transactions) {
|
|
state.Transactions = transactions;
|
|
}
|
|
},
|
|
|
|
getters: {
|
|
Accounts(state) {
|
|
return state.Accounts.values();
|
|
},
|
|
Categories: (state) => (year : number, month : number) => {
|
|
return state.Categories.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);
|
|
},
|
|
OffBudgetAccounts(state) {
|
|
return Array.from(state.Accounts.values()).filter(x => !x.OnBudget);
|
|
},
|
|
Transactions(state) {
|
|
return (state.Transactions || []);
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
async [SET_CURRENT_ACCOUNT]({ state, commit, dispatch, getters }, { budgetid, accountid }) {
|
|
if (budgetid == null)
|
|
return
|
|
|
|
commit("setCurrentAccountID", accountid);
|
|
if (accountid == null)
|
|
return
|
|
|
|
commit(TITLE, getters.CurrentAccount.Name);
|
|
await dispatch(FETCH_ACCOUNT, accountid)
|
|
},
|
|
async [FETCH_ACCOUNT]({ state, commit, rootState }, accountid) {
|
|
const result = await fetch("/api/v1/account/" + accountid + "/transactions", {
|
|
headers: {
|
|
'Authorization': 'Bearer ' + rootState.Session.Token
|
|
}
|
|
});
|
|
const response = await result.json();
|
|
commit("setTransactions", response.Transactions);
|
|
},
|
|
}
|
|
} |