Implement account-view
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user