Use Numeric in JSON output

This commit is contained in:
2022-02-06 22:12:48 +00:00
parent 5763409aa8
commit 487aa89f18
11 changed files with 258 additions and 118 deletions

View File

@ -5,6 +5,7 @@ export const NEW_BUDGET = "New budget";
export const SET_CURRENT_BUDGET = "Set current budget";
export const SET_CURRENT_ACCOUNT = "Set current account";
export const FETCH_BUDGET = "Fetch budget";
export const FETCH_MONTH_BUDGET = "Fetch budget for month";
export const LOGIN = 'Log in';
export const REGISTER = 'Register';
export const FETCH_ACCOUNT = "Fetch account";

View File

@ -1,11 +1,12 @@
import { Module } from "vuex";
import { FETCH_ACCOUNT, FETCH_BUDGET, SET_CURRENT_ACCOUNT } from "../action-types";
import { FETCH_ACCOUNT, FETCH_BUDGET, FETCH_MONTH_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>,
Months: Map<number, Map<number, Map<string, Category>>>,
Transactions: [],
Assignments: []
}
@ -28,6 +29,7 @@ export const budgetStore : Module<BudgetState, any> = {
state: {
Accounts: new Map<string, Account>(),
CurrentAccountID: undefined,
Months: new Map<number, Map<number, Map<string, Category>>>(),
Categories: new Map<string, Category>(),
Transactions: [],
Assignments: []
@ -61,6 +63,17 @@ export const budgetStore : Module<BudgetState, any> = {
addCategory(state, category) {
state.Categories.set(category.ID, category);
},
addCategoriesForMonth(state, {year, month, categories}) {
const yearMap = state.Months.get(year) || new Map<number, Map<string, Category>>();
state.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);
}
},
setCurrentAccountID(state, accountid) {
state.CurrentAccountID = accountid;
},
@ -74,7 +87,8 @@ export const budgetStore : Module<BudgetState, any> = {
return state.Accounts.values();
},
Categories: (state) => (year : number, month : number) => {
return state.Categories.values();
const yearMap = state.Months.get(year);
return yearMap?.get(month)?.values();
},
CurrentAccount(state) : Account | undefined {
if (state.CurrentAccountID == null)
@ -113,5 +127,20 @@ export const budgetStore : Module<BudgetState, any> = {
const response = await result.json();
commit("setTransactions", response.Transactions);
},
async [FETCH_BUDGET]({ state, commit, dispatch, rootState }, budgetid) {
const result = await dispatch("GET", { path: "/budget/" + budgetid });
const response = await result.json();
for (const account of response.Accounts || []) {
commit("addAccount", account);
}
for (const category of response.Categories || []) {
commit("addCategory", category);
}
},
async [FETCH_MONTH_BUDGET]({state, commit, dispatch, rootState }, {budgetid, month, year}) {
const result = await dispatch("GET", { path: "/budget/" + budgetid + "/" + year + "/" + month});
const response = await result.json();
commit("addCategoriesForMonth", {year, month, categories: response.Categories})
}
}
}

View File

@ -130,16 +130,6 @@ export const store = createStore<State>({
await dispatch(FETCH_BUDGET, budgetid)
},
async [FETCH_BUDGET]({ state, commit, dispatch, rootState }, budgetid) {
const result = await dispatch("GET", { path: "/budget/" + budgetid });
const response = await result.json();
for (const account of response.Accounts || []) {
commit("addAccount", account);
}
for (const category of response.Categories || []) {
commit("addCategory", category);
}
},
},
getters: {
Budgets(state) {