diff --git a/web/src/store/index.ts b/web/src/store/index.ts index de6bd34..53844ab 100644 --- a/web/src/store/index.ts +++ b/web/src/store/index.ts @@ -1,20 +1,5 @@ export const store = createStore({ actions: { - [IMPORT_YNAB]({ getters, dispatch }, formData) { - return dispatch("POST", { path: "/budget/" + getters.CurrentBudget.ID + "/import/ynab", body: formData }); - }, - [GET]({ getters }, { path }) { - return fetch("/api/v1" + path, { - headers: getters.AuthHeaders, - }) - }, - [POST]({ getters }, { path, body }) { - return fetch("/api/v1" + path, { - method: "POST", - headers: getters.AuthHeaders, - body: body, - }) - }, /*async fetchDashboard ({state, commit, rootState}) { const response = await fetch("/api/v1/dashboard", { headers: { @@ -24,22 +9,6 @@ export const store = createStore({ const data = await response.json(); commit("setBudgets", data.Budgets); },*/ - async [NEW_BUDGET]({ state, commit, dispatch, rootState }, budgetName) { - const result = await dispatch("POST", { - path: "/budget/new", - body: JSON.stringify({ name: budgetName }) - }); - const response = await result.json(); - commit("addBudget", response) - }, - async [SET_CURRENT_BUDGET]({ state, commit, dispatch, rootState }, budgetid) { - commit("setCurrentBudgetID", budgetid); - - if (budgetid == null) - return - - await dispatch(FETCH_BUDGET, budgetid) - }, }, plugins: [createLogger()], modules: { diff --git a/web/src/stores/budgets.ts b/web/src/stores/budgets.ts index f0cd8f1..4bce968 100644 --- a/web/src/stores/budgets.ts +++ b/web/src/stores/budgets.ts @@ -1,4 +1,5 @@ import { defineStore } from "pinia"; +import { useAPI } from "./api"; import { Budget, useSessionStore } from "./session"; interface State { @@ -7,7 +8,7 @@ interface State { export const useBudgetsStore = defineStore('budget', { state: (): State => ({ - CurrentBudgetID: null + CurrentBudgetID: null }), actions: { setCurrentBudgetID(budgetid : string) { @@ -22,11 +23,37 @@ export const useBudgetsStore = defineStore('budget', { const sessionStore = useSessionStore(); return sessionStore.Budgets.get(this.CurrentBudgetID); }, - CurrentBudgetName(state) : string { + CurrentBudgetName() : string { return this.CurrentBudget?.Name ?? ""; }, CurrentBudgetID() : string | undefined { return this.CurrentBudgetID; }, + ImportYNAB(formData) { + const api = useAPI(); + return api.POST( + "/budget/" + this.CurrentBudgetID + "/import/ynab", + formData + ); + }, + async NewBudget(budgetName) { + const api = useAPI(); + const result = await api.POST( + "/budget/new", + JSON.stringify({ name: budgetName }) + ); + const response = await result.json(); + + const sessionStore = useSessionStore(); + sessionStore.Budgets.set(response.ID, response); + }, + async SetCurrentBudget(budgetid : string) { + this.CurrentBudgetID = budgetid; + + if (budgetid == null) + return + + await dispatch(FETCH_BUDGET, budgetid) + }, } }) \ No newline at end of file diff --git a/web/src/stores/session.ts b/web/src/stores/session.ts index cfda1bd..adbf31b 100644 --- a/web/src/stores/session.ts +++ b/web/src/stores/session.ts @@ -23,7 +23,7 @@ export const useSessionStore = defineStore('session', { Budgets(): IterableIterator { return this.Budgets.values(); }, - AuthHeaders(): Object { + AuthHeaders(): HeadersInit { return { 'Authorization': 'Bearer ' + this.Token }