Implement account-view

This commit is contained in:
2022-01-25 19:03:58 +00:00
parent e184ef933f
commit 458f4f0e8f
5 changed files with 87 additions and 12 deletions

View File

@ -2,6 +2,7 @@ const budget = {
state () {
return {
Accounts: [],
CurrentAccount: null,
Categories: [],
Transactions: [],
Assignments: []
@ -10,29 +11,58 @@ const budget = {
mutations: {
setAccounts (state, accounts) {
state.Accounts = accounts;
},
setCurrentAccount(state, account) {
state.CurrentAccount = account;
}
},
getters: {
Accounts(state) {
return state.Accounts || [];
},
CurrentAccount(state) {
return state.CurrentAccount || {};
},
OnBudgetAccounts(state) {
return (state.Accounts || []).filter(x => x.OnBudget);
},
OffBudgetAccounts(state) {
return (state.Accounts || []).filter(x => !x.OnBudget);
},
Transactions(state) {
return (state.Transactions || []);
}
},
actions: {
fetchBudget ({state, commit, rootState}, budgetid) {
fetch("/api/v1/budget/" + budgetid, {
async fetchBudget ({state, commit, rootState}, budgetid) {
const result = await fetch("/api/v1/budget/" + budgetid, {
headers: {
'Authorization': 'Bearer ' + rootState.Session.Token
}
})
.then(x => x.json())
.then(x => commit("setAccounts", x.Accounts));
});
const response = await result.json();
return commit("setAccounts", response.Accounts);
},
async fetchAccount ({state, commit, rootState}, accountid) {
const result = await fetch("/api/v1/account/" + accountid, {
headers: {
'Authorization': 'Bearer ' + rootState.Session.Token
}
});
const response = await result.json();
return commit("setTransactions", response.Transactions);
},
async setCurrentAccount({state, commit, dispatch}, {budgetid, accountid}) {
await dispatch("fetchBudget", budgetid);
for (const element of state.Accounts) {
if(element.ID != accountid)
continue
commit("setCurrentAccount", element);
commit("setTitle", element.Name);
break
}
}
}
}

View File

@ -39,9 +39,13 @@ const store = createStore({
})
store.subscribe((mutation, state) => {
if(mutation.type == "setCurrentBudget"){
console.log(mutation, state);
store.dispatch("fetchBudget", mutation.payload.ID)
switch(mutation.type){
case "setCurrentBudget":
store.dispatch("fetchBudget", mutation.payload.ID)
break;
case "setCurrentAccount":
store.dispatch("fetchAccount", mutation.payload.ID)
break;
}
})