Extract some types

This commit is contained in:
2022-01-31 20:34:32 +00:00
parent 9c0126b14c
commit 29f0c51e35
5 changed files with 40 additions and 96 deletions

View File

@ -1,5 +1,6 @@
import { createStore, createLogger } from 'vuex'
import { LOGIN, LOGIN_SUCCESS, LOGOUT, TITLE } from './mutation-types'
import { LOGIN_SUCCESS, LOGOUT, TITLE } from './mutation-types'
import { FETCH_ACCOUNT, FETCH_BUDGET, GET, REGISTER, IMPORT_YNAB, LOGIN, NEW_BUDGET, POST, SET_CURRENT_ACCOUNT, SET_CURRENT_BUDGET} from './action-types'
const store = createStore({
state() {
@ -76,19 +77,22 @@ const store = createStore({
[LOGIN]({ state, commit }, login) {
return fetch("/api/v1/user/login", { method: "POST", body: JSON.stringify(login) })
.then(x => x.json())
.then(x => {
commit(LOGIN_SUCCESS, x);
})
.then(x => commit(LOGIN_SUCCESS, x))
},
YNAB({ getters, dispatch }, formData) {
[REGISTER]({ state, commit }, login) {
return fetch("/api/v1/user/register", { method: "POST", body: JSON.stringify(login) })
.then(x => x.json())
.then(x => commit(LOGIN_SUCCESS, x))
},
[IMPORT_YNAB]({ getters, dispatch }, formData) {
return dispatch("POST", { path: "/budget/" + getters.CurrentBudget.ID + "/import/ynab", body: formData });
},
GET({ getters }, { path }) {
[GET]({ getters }, { path }) {
return fetch("/api/v1" + path, {
headers: getters.AuthHeaders,
})
},
POST({ getters }, { path, body }) {
[POST]({ getters }, { path, body }) {
return fetch("/api/v1" + path, {
method: "POST",
headers: getters.AuthHeaders,
@ -104,7 +108,7 @@ const store = createStore({
const data = await response.json();
commit("setBudgets", data.Budgets);
},*/
async newBudget({ state, commit, dispatch, rootState }, budgetName) {
async [NEW_BUDGET]({ state, commit, dispatch, rootState }, budgetName) {
const result = await dispatch("POST", {
path: "/budget/new",
body: JSON.stringify({ name: budgetName })
@ -112,20 +116,20 @@ const store = createStore({
const response = await result.json();
commit("addBudget", response)
},
async setCurrentBudget({ state, commit, dispatch, rootState }, budgetid) {
async [SET_CURRENT_BUDGET]({ state, commit, dispatch, rootState }, budgetid) {
commit("setCurrentBudgetID", budgetid);
if (budgetid == null)
return
await dispatch("fetchBudget", budgetid)
await dispatch(FETCH_BUDGET, budgetid)
},
async fetchBudget({ state, commit, dispatch, rootState }, budgetid) {
async [FETCH_BUDGET]({ state, commit, dispatch, rootState }, budgetid) {
const result = await dispatch("GET", { path: "/budget/" + budgetid });
const response = await result.json();
return commit("setAccounts", response.Accounts);
},
async fetchAccount({ state, commit, rootState }, accountid) {
async [FETCH_ACCOUNT]({ state, commit, rootState }, accountid) {
const result = await fetch("/api/v1/account/" + accountid + "/transactions", {
headers: {
'Authorization': 'Bearer ' + rootState.Session.Token
@ -134,17 +138,17 @@ const store = createStore({
const response = await result.json();
commit("setTransactions", response.Transactions);
},
async setCurrentAccount({ state, commit, dispatch, getters }, { budgetid, accountid }) {
async [SET_CURRENT_ACCOUNT]({ state, commit, dispatch, getters }, { budgetid, accountid }) {
if (budgetid == null)
return
await dispatch("fetchBudget", budgetid);
await dispatch(FETCH_BUDGET, budgetid);
commit("setCurrentAccountID", accountid);
if (accountid == null)
return
commit(TITLE, getters.CurrentAccount.Name);
await dispatch("fetchAccount", accountid)
await dispatch(FETCH_ACCOUNT, accountid)
}
},
getters: {

View File

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