diff --git a/web/src/main.js b/web/src/main.js index c8b11ff..6fcac3f 100644 --- a/web/src/main.js +++ b/web/src/main.js @@ -1,54 +1,7 @@ import { createApp } from 'vue/dist/vue.esm-bundler' -import { createStore } from 'vuex' import App from './App.vue' import router from './router/routes.js' - -const store = createStore({ - state () { - return { - Session: { - Token: null, - User: null - }, - Budgets: [], - CurrentBudget: null, - } - }, - mutations: { - initializeStore(state) { - let store = localStorage.getItem("store"); - if(store){ - this.replaceState( - Object.assign(state, JSON.parse(store)) - ); - } - }, - getDashboard (state) { - fetch("/api/v1/dashboard", { - headers: { - 'Authorization': 'Bearer ' + state.Session.Token - } - }) - .then(x => x.json()) - .then(x => state.Budgets = x.Budgets); - }, - setTitle (state, title) { - document.title = "Budgeteer - " + title; - }, - setToken(state, token) { - state.Session.Token = token; - } - } -}) - -store.subscribe((mutation, state) => { - let persistedState = { - Session: state.Session, - Budgets: state.Budgets, - CurrentBudget: state.CurrentBudget - } - localStorage.setItem("store", JSON.stringify(persistedState)); -}) +import store from './store/index.js' const app = createApp(App) app.use(router) diff --git a/web/src/pages/Dashboard.vue b/web/src/pages/Dashboard.vue index 29feb3f..cfc904f 100644 --- a/web/src/pages/Dashboard.vue +++ b/web/src/pages/Dashboard.vue @@ -6,7 +6,7 @@ export default { } }, mounted () { - this.$store.commit("getDashboard"); + this.$store.dispatch("fetchDashboard"); } } diff --git a/web/src/store/dashboard/index.js b/web/src/store/dashboard/index.js new file mode 100644 index 0000000..ae310cf --- /dev/null +++ b/web/src/store/dashboard/index.js @@ -0,0 +1,26 @@ +const dashboard = { + state () { + return { + Budgets: [], + CurrentBudget: null, + } + }, + mutations: { + setBudgets (state, budgets) { + state.Budgets = budgets; + } + }, + actions: { + fetchDashboard ({state, commit, rootState}) { + fetch("/api/v1/dashboard", { + headers: { + 'Authorization': 'Bearer ' + rootState.Session.Token + } + }) + .then(x => x.json()) + .then(x => commit("setBudgets", x.Budgets)); + } + } +} + +export default dashboard \ No newline at end of file diff --git a/web/src/store/index.js b/web/src/store/index.js new file mode 100644 index 0000000..dc1838f --- /dev/null +++ b/web/src/store/index.js @@ -0,0 +1,43 @@ +import { createStore } from 'vuex' +import dashboard from './dashboard/index.js' + +const store = createStore({ + state () { + return { + Session: { + Token: null, + User: null + }, + } + }, + mutations: { + initializeStore(state) { + let store = localStorage.getItem("store"); + if(store){ + this.replaceState( + Object.assign(state, JSON.parse(store)) + ); + } + }, + setTitle (state, title) { + document.title = "Budgeteer - " + title; + }, + setToken(state, token) { + state.Session.Token = token; + } + }, + modules: { + dashboard + } +}) + +store.subscribe((mutation, state) => { + let persistedState = { + Session: state.Session, + Budgets: state.Budgets, + CurrentBudget: state.CurrentBudget + } + localStorage.setItem("store", JSON.stringify(persistedState)); +}) + +export default store \ No newline at end of file