Redirect to dashboard and remove budget on delete

This commit is contained in:
Jan Bader 2022-01-31 16:12:17 +00:00
parent 8e2a62929e
commit 34b9c14419
2 changed files with 38 additions and 31 deletions

View File

@ -29,7 +29,9 @@ export default {
headers: { headers: {
'Authorization': 'Bearer ' + this.$store.state.Session.Token 'Authorization': 'Bearer ' + this.$store.state.Session.Token
}, },
}) });
this.$store.commit("deleteBudget", this.$store.getters.CurrentBudget.ID)
this.$router.push("/")
}, },
clearBudget() { clearBudget() {
fetch("/api/v1/budget/" + this.$store.getters.CurrentBudget.ID + "/settings/clear", { fetch("/api/v1/budget/" + this.$store.getters.CurrentBudget.ID + "/settings/clear", {

View File

@ -2,14 +2,14 @@ import { createStore, createLogger } from 'vuex'
import { LOGIN, LOGIN_SUCCESS, LOGOUT, TITLE } from './mutation-types' import { LOGIN, LOGIN_SUCCESS, LOGOUT, TITLE } from './mutation-types'
const store = createStore({ const store = createStore({
state () { state() {
return { return {
Session: { Session: {
Token: null, Token: null,
User: null User: null
}, },
ShowMenu: null, ShowMenu: null,
Budgets: {}, Budgets: [],
CurrentBudgetID: null, CurrentBudgetID: null,
Accounts: [], Accounts: [],
CurrentAccountID: null, CurrentAccountID: null,
@ -19,12 +19,15 @@ const store = createStore({
} }
}, },
mutations: { mutations: {
deleteBudget(state, budgetid) {
delete state.Budgets[budgetid]
},
toggleMenu(state) { toggleMenu(state) {
state.ShowMenu = !state.ShowMenu; state.ShowMenu = !state.ShowMenu;
}, },
initializeStore(state) { initializeStore(state) {
const store = localStorage.getItem("store"); const store = localStorage.getItem("store");
if(store){ if (store) {
this.replaceState( this.replaceState(
Object.assign(state, JSON.parse(store)) Object.assign(state, JSON.parse(store))
); );
@ -42,14 +45,19 @@ const store = createStore({
state.Budgets[budget.ID] = budget state.Budgets[budget.ID] = budget
} }
}, },
setBudgets (state, budgets) { setBudgets(state, budgets) {
state.Budgets = budgets; state.Budgets = budgets;
}, },
addBudget(state, budget) { addBudget(state, budget) {
state.Budgets.push(budget); state.Budgets.push(budget);
}, },
[LOGOUT](state, token) { [LOGOUT](state, token) {
state.Session = { Token: null, User: null } state.Session = { Token: null, User: null };
state.Budgets = {};
state.Accounts = [];
state.Categories = [];
state.Transactions = [];
state.Assignments = [];
}, },
setCurrentBudgetID(state, budgetid) { setCurrentBudgetID(state, budgetid) {
state.CurrentBudgetID = budgetid; state.CurrentBudgetID = budgetid;
@ -57,30 +65,30 @@ const store = createStore({
setCurrentAccountID(state, accountid) { setCurrentAccountID(state, accountid) {
state.CurrentAccountID = accountid; state.CurrentAccountID = accountid;
}, },
setAccounts (state, accounts) { setAccounts(state, accounts) {
state.Accounts = accounts; state.Accounts = accounts;
}, },
setTransactions(state, transactions){ setTransactions(state, transactions) {
state.Transactions = transactions; state.Transactions = transactions;
} }
}, },
actions: { actions: {
[LOGIN]({state, commit}, login) { [LOGIN]({ state, commit }, login) {
return fetch("/api/v1/user/login", {method: "POST", body: JSON.stringify(login)}) return fetch("/api/v1/user/login", { method: "POST", body: JSON.stringify(login) })
.then(x => x.json()) .then(x => x.json())
.then(x => { .then(x => {
commit(LOGIN_SUCCESS, x); commit(LOGIN_SUCCESS, x);
}) })
}, },
YNAB({getters, dispatch}, formData) { YNAB({ getters, dispatch }, formData) {
return dispatch("POST", {path: "/budget/"+getters.CurrentBudget.ID+"/import/ynab", body: formData}); return dispatch("POST", { path: "/budget/" + getters.CurrentBudget.ID + "/import/ynab", body: formData });
}, },
GET({getters}, {path}){ GET({ getters }, { path }) {
return fetch("/api/v1" + path, { return fetch("/api/v1" + path, {
headers: getters.AuthHeaders, headers: getters.AuthHeaders,
}) })
}, },
POST({getters}, {path, body}){ POST({ getters }, { path, body }) {
return fetch("/api/v1" + path, { return fetch("/api/v1" + path, {
method: "POST", method: "POST",
headers: getters.AuthHeaders, headers: getters.AuthHeaders,
@ -96,14 +104,15 @@ const store = createStore({
const data = await response.json(); const data = await response.json();
commit("setBudgets", data.Budgets); commit("setBudgets", data.Budgets);
},*/ },*/
async newBudget ({state, commit, dispatch, rootState}, budgetName) { async newBudget({ state, commit, dispatch, rootState }, budgetName) {
const result = await dispatch("POST", { const result = await dispatch("POST", {
path: "/budget/new", path: "/budget/new",
body: JSON.stringify({name: budgetName})}); body: JSON.stringify({ name: budgetName })
});
const response = await result.json(); const response = await result.json();
commit("addBudget", response) commit("addBudget", response)
}, },
async setCurrentBudget({state, commit, dispatch, rootState}, budgetid) { async setCurrentBudget({ state, commit, dispatch, rootState }, budgetid) {
commit("setCurrentBudgetID", budgetid); commit("setCurrentBudgetID", budgetid);
if (budgetid == null) if (budgetid == null)
@ -111,12 +120,12 @@ const store = createStore({
await dispatch("fetchBudget", budgetid) await dispatch("fetchBudget", budgetid)
}, },
async fetchBudget ({state, commit, dispatch, rootState}, budgetid) { async fetchBudget({ state, commit, dispatch, rootState }, budgetid) {
const result = await dispatch("GET", {path: "/budget/" + budgetid}); const result = await dispatch("GET", { path: "/budget/" + budgetid });
const response = await result.json(); const response = await result.json();
return commit("setAccounts", response.Accounts); return commit("setAccounts", response.Accounts);
}, },
async fetchAccount ({state, commit, rootState}, accountid) { async fetchAccount({ state, commit, rootState }, accountid) {
const result = await fetch("/api/v1/account/" + accountid + "/transactions", { const result = await fetch("/api/v1/account/" + accountid + "/transactions", {
headers: { headers: {
'Authorization': 'Bearer ' + rootState.Session.Token 'Authorization': 'Bearer ' + rootState.Session.Token
@ -125,7 +134,7 @@ const store = createStore({
const response = await result.json(); const response = await result.json();
commit("setTransactions", response.Transactions); commit("setTransactions", response.Transactions);
}, },
async setCurrentAccount({state, commit, dispatch, getters}, {budgetid, accountid}) { async setCurrentAccount({ state, commit, dispatch, getters }, { budgetid, accountid }) {
if (budgetid == null) if (budgetid == null)
return return
@ -148,21 +157,17 @@ const store = createStore({
} }
}, },
CurrentBudget(state) { CurrentBudget(state) {
if(state.CurrentBudgetID == null) if (state.CurrentBudgetID == null)
return {}; return {};
const budgets = state.Budgets.filter(x => x.ID == state.CurrentBudgetID); return state.Budgets[state.CurrentBudgetID];
if(budgets.length > 0)
return budgets[0];
return {};
}, },
Accounts(state) { Accounts(state) {
return state.Accounts || []; return state.Accounts || [];
}, },
CurrentAccount(state) { CurrentAccount(state) {
if(state.CurrentAccountID == null) if (state.CurrentAccountID == null)
return {name: "Not found"}; return { name: "Not found" };
return state.Accounts.filter(x => x.ID == state.CurrentAccountID)[0]; return state.Accounts.filter(x => x.ID == state.CurrentAccountID)[0];
}, },
OnBudgetAccounts(state) { OnBudgetAccounts(state) {