Remove dashboard call and fetch Budgets with login

This commit is contained in:
Jan Bader 2022-01-25 22:29:35 +00:00
parent 9e3dde8076
commit de6054359a
5 changed files with 57 additions and 24 deletions

View File

@ -93,10 +93,16 @@ func (h *Handler) loginPost(c *gin.Context) {
go h.Service.UpdateLastLogin(context.Background(), user.ID) 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 { c.JSON(http.StatusOK, struct {
Token string Token string
User postgres.User User postgres.User
}{t, user}) Budgets []postgres.Budget
}{t, user, budgets})
} }
type registerInformation struct { type registerInformation struct {
@ -143,8 +149,14 @@ func (h *Handler) registerPost(c *gin.Context) {
go h.Service.UpdateLastLogin(context.Background(), user.ID) 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 { c.JSON(http.StatusOK, struct {
Token string Token string
User postgres.User User postgres.User
}{t, user}) Budgets []postgres.Budget
}{t, user, budgets})
} }

View File

@ -4,7 +4,7 @@ import NewBudget from '@/dialogs/NewBudget.vue';
export default { export default {
props: ["budgetid"], props: ["budgetid"],
mounted() { mounted() {
this.$store.dispatch("fetchDashboard"); //this.$store.dispatch("fetchDashboard");
}, },
watch: { watch: {
budgetid () { budgetid () {

View File

@ -1,6 +1,6 @@
import { createStore } from 'vuex' import { createStore } from 'vuex'
import dashboard from './modules/dashboard' 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({ const store = createStore({
state () { state () {
@ -9,6 +9,7 @@ const store = createStore({
Token: null, Token: null,
User: null User: null
}, },
Budgets: {},
} }
}, },
mutations: { mutations: {
@ -23,13 +24,40 @@ const store = createStore({
[TITLE](state, title) { [TITLE](state, title) {
document.title = "Budgeteer - " + title; document.title = "Budgeteer - " + title;
}, },
[LOGIN_SUCCESS](state, session) { [LOGIN_SUCCESS](state, result) {
state.Session = session; 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) { [LOGOUT](state, token) {
state.Session = { Token: null, User: null } 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: { modules: {
dashboard dashboard
} }

View File

@ -1,7 +1,8 @@
import { TITLE } from "../mutation-types";
const dashboard = { const dashboard = {
state () { state () {
return { return {
Budgets: [],
CurrentBudget: null, CurrentBudget: null,
Accounts: [], Accounts: [],
CurrentAccount: null, CurrentAccount: null,
@ -11,12 +12,6 @@ const dashboard = {
} }
}, },
mutations: { mutations: {
setBudgets (state, budgets) {
state.Budgets = budgets;
},
addBudget(state, budget) {
state.Budgets.push(budget);
},
setCurrentBudget(state, budget) { setCurrentBudget(state, budget) {
state.CurrentBudget = budget; state.CurrentBudget = budget;
}, },
@ -31,9 +26,6 @@ const dashboard = {
} }
}, },
getters: { getters: {
Budgets(state) {
return state.Budgets || [];
},
CurrentBudget(state) { CurrentBudget(state) {
return state.CurrentBudget || {}; return state.CurrentBudget || {};
}, },
@ -54,7 +46,7 @@ const dashboard = {
} }
}, },
actions: { actions: {
async fetchDashboard ({state, commit, rootState}) { /*async fetchDashboard ({state, commit, rootState}) {
const response = await fetch("/api/v1/dashboard", { const response = await fetch("/api/v1/dashboard", {
headers: { headers: {
'Authorization': 'Bearer ' + rootState.Session.Token 'Authorization': 'Bearer ' + rootState.Session.Token
@ -62,7 +54,7 @@ const dashboard = {
}) })
const data = await response.json(); const data = await response.json();
commit("setBudgets", data.Budgets); commit("setBudgets", data.Budgets);
}, },*/
async newBudget ({state, commit, rootState}, budgetName) { async newBudget ({state, commit, rootState}, budgetName) {
fetch("/api/v1/budget/new", { fetch("/api/v1/budget/new", {
method: "POST", method: "POST",
@ -74,9 +66,9 @@ const dashboard = {
.then(x => x.json()) .then(x => x.json())
.then(x => commit("addBudget", x)); .then(x => commit("addBudget", x));
}, },
async setCurrentBudget({state, commit, dispatch}, budgetid) { async setCurrentBudget({state, commit, dispatch, rootState}, budgetid) {
await dispatch("fetchDashboard"); await dispatch("fetchDashboard");
for (const element of state.Budgets) { for (const element of rootState.Budgets) {
if(element.ID != budgetid) if(element.ID != budgetid)
continue continue

View File

@ -1,3 +1,4 @@
export const LOGIN = 'Log in';
export const LOGIN_SUCCESS = '✔ Logged in'; export const LOGIN_SUCCESS = '✔ Logged in';
export const LOGOUT = 'Log out'; export const LOGOUT = 'Log out';
export const TITLE = 'Update title'; export const TITLE = 'Update title';