From af3252277c7628481bf8f1d18816815854443337 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 25 Jan 2022 08:37:38 +0000 Subject: [PATCH] Implement currentBudget and move infos to sidebar --- web/src/App.vue | 9 +++++ web/src/pages/Budget.vue | 57 -------------------------------- web/src/pages/BudgetSidebar.vue | 55 ++++++++++++++++++++++++++++++ web/src/router/routes.js | 7 ++-- web/src/store/budget/index.js | 2 +- web/src/store/dashboard/index.js | 27 ++++++++++++--- 6 files changed, 91 insertions(+), 66 deletions(-) delete mode 100644 web/src/pages/Budget.vue create mode 100644 web/src/pages/BudgetSidebar.vue diff --git a/web/src/App.vue b/web/src/App.vue index cdcca6f..d1847a9 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -2,6 +2,11 @@ // This starter template is using Vue 3 - - \ No newline at end of file diff --git a/web/src/pages/BudgetSidebar.vue b/web/src/pages/BudgetSidebar.vue new file mode 100644 index 0000000..3481411 --- /dev/null +++ b/web/src/pages/BudgetSidebar.vue @@ -0,0 +1,55 @@ + + + \ No newline at end of file diff --git a/web/src/router/routes.js b/web/src/router/routes.js index a0c2626..8c139eb 100644 --- a/web/src/router/routes.js +++ b/web/src/router/routes.js @@ -1,4 +1,4 @@ -import { createRouter, createWebHashHistory } from 'vue-router' +import { createRouter, createWebHistory } from 'vue-router' import Budget from '../pages/Budget.vue'; import Dashboard from '../pages/Dashboard.vue'; import Login from '../pages/Login.vue'; @@ -6,13 +6,14 @@ import Register from '../pages/Register.vue'; const routes = [ { path: '/', name: 'Index', component: Dashboard }, + { path: '/dashboard', name: 'Dashboard', component: Dashboard }, { path: '/login', name: 'Login', component: Login }, { path: '/register', name: 'Register', component: Register }, - { path: '/budget/:budgetid', name: 'Budget', component: Budget }, + { path: '/budget/:budgetid', name: 'Budget', components: {default: Dashboard, sidebar: Budget } }, ] const router = createRouter({ - history: createWebHashHistory(), + history: createWebHistory(), routes, }) diff --git a/web/src/store/budget/index.js b/web/src/store/budget/index.js index aaca938..71ec39c 100644 --- a/web/src/store/budget/index.js +++ b/web/src/store/budget/index.js @@ -9,7 +9,7 @@ const budget = { }, mutations: { setAccounts (state, accounts) { - state.Accounts = accounts; + state.Accounts = accounts; } }, getters: { diff --git a/web/src/store/dashboard/index.js b/web/src/store/dashboard/index.js index 1ef347b..4138f41 100644 --- a/web/src/store/dashboard/index.js +++ b/web/src/store/dashboard/index.js @@ -11,24 +11,30 @@ const dashboard = { }, addBudget(state, budget) { state.Budgets.push(budget); + }, + setCurrentBudget(state, budget) { + state.CurrentBudget = budget; } }, getters: { Budgets(state) { return state.Budgets || []; + }, + CurrentBudget(state) { + return state.CurrentBudget || {}; } }, actions: { - fetchDashboard ({state, commit, rootState}) { - fetch("/api/v1/dashboard", { + async fetchDashboard ({state, commit, rootState}) { + const response = await fetch("/api/v1/dashboard", { headers: { 'Authorization': 'Bearer ' + rootState.Session.Token } }) - .then(x => x.json()) - .then(x => commit("setBudgets", x.Budgets)); + const data = await response.json(); + commit("setBudgets", data.Budgets); }, - newBudget ({state, commit, rootState}, budgetName) { + async newBudget ({state, commit, rootState}, budgetName) { fetch("/api/v1/budget/new", { method: "POST", body: JSON.stringify({name: budgetName}), @@ -38,6 +44,17 @@ const dashboard = { }) .then(x => x.json()) .then(x => commit("addBudget", x)); + }, + async setCurrentBudget({state, commit, dispatch}, budgetid) { + await dispatch("fetchDashboard"); + for (const element of state.Budgets) { + if(element.ID != budgetid) + continue + + commit("setCurrentBudget", element); + break + } + await dispatch("fetchBudget", budgetid); } } }