diff --git a/http/session.go b/http/session.go index 5f61b38..9fa585d 100644 --- a/http/session.go +++ b/http/session.go @@ -93,10 +93,16 @@ func (h *Handler) loginPost(c *gin.Context) { go h.Service.UpdateLastLogin(context.Background(), user.ID) + budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), user.ID) + if err != nil { + return + } + c.JSON(http.StatusOK, struct { - Token string - User postgres.User - }{t, user}) + Token string + User postgres.User + Budgets []postgres.Budget + }{t, user, budgets}) } type registerInformation struct { @@ -143,8 +149,14 @@ func (h *Handler) registerPost(c *gin.Context) { go h.Service.UpdateLastLogin(context.Background(), user.ID) + budgets, err := h.Service.GetBudgetsForUser(c.Request.Context(), user.ID) + if err != nil { + return + } + c.JSON(http.StatusOK, struct { - Token string - User postgres.User - }{t, user}) + Token string + User postgres.User + Budgets []postgres.Budget + }{t, user, budgets}) } diff --git a/web/src/pages/Dashboard.vue b/web/src/pages/Dashboard.vue index 6d2bedd..2ba6cc0 100644 --- a/web/src/pages/Dashboard.vue +++ b/web/src/pages/Dashboard.vue @@ -4,7 +4,7 @@ import NewBudget from '@/dialogs/NewBudget.vue'; export default { props: ["budgetid"], mounted() { - this.$store.dispatch("fetchDashboard"); + //this.$store.dispatch("fetchDashboard"); }, watch: { budgetid () { diff --git a/web/src/store/index.js b/web/src/store/index.js index 744776a..9f710d0 100644 --- a/web/src/store/index.js +++ b/web/src/store/index.js @@ -1,6 +1,6 @@ import { createStore } from 'vuex' import dashboard from './modules/dashboard' -import { LOGIN_SUCCESS, LOGOUT, TITLE } from './mutation-types' +import { LOGIN, LOGIN_SUCCESS, LOGOUT, TITLE } from './mutation-types' const store = createStore({ state () { @@ -9,6 +9,7 @@ const store = createStore({ Token: null, User: null }, + Budgets: {}, } }, mutations: { @@ -23,13 +24,40 @@ const store = createStore({ [TITLE](state, title) { document.title = "Budgeteer - " + title; }, - [LOGIN_SUCCESS](state, session) { - state.Session = session; + [LOGIN_SUCCESS](state, result) { + state.Session = { + User: result.User, + Token: result.Token + }; + for (const budget of result.Budgets) { + state.Budgets[budget.ID] = budget + } + }, + setBudgets (state, budgets) { + state.Budgets = budgets; + }, + addBudget(state, budget) { + state.Budgets.push(budget); }, [LOGOUT](state, token) { state.Session = { Token: null, User: null } } }, + actions: { + [LOGIN]({state, commit}, login) { + fetch("/api/v1/user/login", {method: "POST", body: JSON.stringify(login)}) + .then(x => x.json()) + .then(x => { + commit(LOGIN_SUCCESS, x); + this.$router.replace("/dashboard"); + }) + } + }, + getters: { + Budgets(state) { + return state.Budgets || []; + }, + }, modules: { dashboard } diff --git a/web/src/store/modules/dashboard.js b/web/src/store/modules/dashboard.js index 6bdc5cc..ab2f955 100644 --- a/web/src/store/modules/dashboard.js +++ b/web/src/store/modules/dashboard.js @@ -1,7 +1,8 @@ +import { TITLE } from "../mutation-types"; + const dashboard = { state () { return { - Budgets: [], CurrentBudget: null, Accounts: [], CurrentAccount: null, @@ -11,12 +12,6 @@ const dashboard = { } }, mutations: { - setBudgets (state, budgets) { - state.Budgets = budgets; - }, - addBudget(state, budget) { - state.Budgets.push(budget); - }, setCurrentBudget(state, budget) { state.CurrentBudget = budget; }, @@ -31,9 +26,6 @@ const dashboard = { } }, getters: { - Budgets(state) { - return state.Budgets || []; - }, CurrentBudget(state) { return state.CurrentBudget || {}; }, @@ -54,7 +46,7 @@ const dashboard = { } }, actions: { - async fetchDashboard ({state, commit, rootState}) { + /*async fetchDashboard ({state, commit, rootState}) { const response = await fetch("/api/v1/dashboard", { headers: { 'Authorization': 'Bearer ' + rootState.Session.Token @@ -62,7 +54,7 @@ const dashboard = { }) const data = await response.json(); commit("setBudgets", data.Budgets); - }, + },*/ async newBudget ({state, commit, rootState}, budgetName) { fetch("/api/v1/budget/new", { method: "POST", @@ -74,9 +66,9 @@ const dashboard = { .then(x => x.json()) .then(x => commit("addBudget", x)); }, - async setCurrentBudget({state, commit, dispatch}, budgetid) { + async setCurrentBudget({state, commit, dispatch, rootState}, budgetid) { await dispatch("fetchDashboard"); - for (const element of state.Budgets) { + for (const element of rootState.Budgets) { if(element.ID != budgetid) continue diff --git a/web/src/store/mutation-types.js b/web/src/store/mutation-types.js index 9fbd0c5..8216800 100644 --- a/web/src/store/mutation-types.js +++ b/web/src/store/mutation-types.js @@ -1,3 +1,4 @@ +export const LOGIN = 'Log in'; export const LOGIN_SUCCESS = '✔ Logged in'; export const LOGOUT = 'Log out'; export const TITLE = 'Update title'; \ No newline at end of file