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)
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})
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})
Budgets []postgres.Budget
}{t, user, budgets})
}

View File

@ -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 () {

View File

@ -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
}

View File

@ -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

View File

@ -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';