Convert most code to ts
This commit is contained in:
10
web/src/store/action-types.ts
Normal file
10
web/src/store/action-types.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export const IMPORT_YNAB = "YNAB import";
|
||||
export const GET = "GET";
|
||||
export const POST = "POST";
|
||||
export const NEW_BUDGET = "New budget";
|
||||
export const SET_CURRENT_BUDGET = "Set current budget";
|
||||
export const SET_CURRENT_ACCOUNT = "Set current account";
|
||||
export const FETCH_BUDGET = "Fetch budget";
|
||||
export const LOGIN = 'Log in';
|
||||
export const REGISTER = 'Register';
|
||||
export const FETCH_ACCOUNT = "Fetch account";
|
@ -1,27 +1,52 @@
|
||||
import { createStore, createLogger } from 'vuex'
|
||||
import { InjectionKey } from 'vue'
|
||||
import { createStore, Store, createLogger } from 'vuex'
|
||||
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() {
|
||||
return {
|
||||
export interface State {
|
||||
Session: {
|
||||
Token?: string
|
||||
User?: string
|
||||
},
|
||||
ShowMenu?: boolean,
|
||||
Budgets: Map<string, Budget>,
|
||||
CurrentBudgetID?: string,
|
||||
Accounts: Map<string, Account>,
|
||||
CurrentAccountID?: string,
|
||||
Categories: [],
|
||||
Transactions: [],
|
||||
Assignments: []
|
||||
}
|
||||
|
||||
export interface Budget {
|
||||
ID: string
|
||||
}
|
||||
|
||||
export interface Account {
|
||||
ID: string
|
||||
OnBudget: boolean
|
||||
}
|
||||
|
||||
export const key: InjectionKey<Store<State>> = Symbol()
|
||||
|
||||
export const store = createStore<State>({
|
||||
state: {
|
||||
Session: {
|
||||
Token: null,
|
||||
User: null
|
||||
Token: undefined,
|
||||
User: undefined
|
||||
},
|
||||
ShowMenu: null,
|
||||
Budgets: [],
|
||||
CurrentBudgetID: null,
|
||||
Accounts: [],
|
||||
CurrentAccountID: null,
|
||||
ShowMenu: undefined,
|
||||
Budgets: new Map<string, Budget>(),
|
||||
CurrentBudgetID: undefined,
|
||||
Accounts: new Map<string, Account>(),
|
||||
CurrentAccountID: undefined,
|
||||
Categories: [],
|
||||
Transactions: [],
|
||||
Assignments: []
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
deleteBudget(state, budgetid) {
|
||||
delete state.Budgets[budgetid]
|
||||
deleteBudget(state : State, budgetid : string) {
|
||||
state.Budgets.delete(budgetid)
|
||||
},
|
||||
toggleMenu(state) {
|
||||
state.ShowMenu = !state.ShowMenu;
|
||||
@ -43,19 +68,19 @@ const store = createStore({
|
||||
Token: result.Token
|
||||
};
|
||||
for (const budget of result.Budgets) {
|
||||
state.Budgets[budget.ID] = budget
|
||||
state.Budgets.set(budget.ID, budget)
|
||||
}
|
||||
},
|
||||
setBudgets(state, budgets) {
|
||||
state.Budgets = budgets;
|
||||
},
|
||||
addBudget(state, budget) {
|
||||
state.Budgets.push(budget);
|
||||
state.Budgets.set(budget.ID, budget);
|
||||
},
|
||||
[LOGOUT](state, token) {
|
||||
state.Session = { Token: null, User: null };
|
||||
state.Budgets = {};
|
||||
state.Accounts = [];
|
||||
state.Session = { Token: undefined, User: undefined };
|
||||
state.Budgets.clear();
|
||||
state.Accounts.clear();
|
||||
state.Categories = [];
|
||||
state.Transactions = [];
|
||||
state.Assignments = [];
|
||||
@ -164,7 +189,7 @@ const store = createStore({
|
||||
if (state.CurrentBudgetID == null)
|
||||
return {};
|
||||
|
||||
return state.Budgets[state.CurrentBudgetID];
|
||||
return state.Budgets.get(state.CurrentBudgetID);
|
||||
},
|
||||
Accounts(state) {
|
||||
return state.Accounts || [];
|
||||
@ -172,13 +197,13 @@ const store = createStore({
|
||||
CurrentAccount(state) {
|
||||
if (state.CurrentAccountID == null)
|
||||
return { name: "Not found" };
|
||||
return state.Accounts.filter(x => x.ID == state.CurrentAccountID)[0];
|
||||
return state.Accounts.get(state.CurrentAccountID);
|
||||
},
|
||||
OnBudgetAccounts(state) {
|
||||
return (state.Accounts || []).filter(x => x.OnBudget);
|
||||
return Array.from(state.Accounts.values()).filter(x => x.OnBudget);
|
||||
},
|
||||
OffBudgetAccounts(state) {
|
||||
return (state.Accounts || []).filter(x => !x.OnBudget);
|
||||
return Array.from(state.Accounts.values()).filter(x => !x.OnBudget);
|
||||
},
|
||||
Transactions(state) {
|
||||
return (state.Transactions || []);
|
||||
@ -197,6 +222,4 @@ store.subscribe((mutation, state) => {
|
||||
ShowMenu: state.ShowMenu
|
||||
}
|
||||
localStorage.setItem("store", JSON.stringify(persistedState));
|
||||
})
|
||||
|
||||
export default store
|
||||
})
|
Reference in New Issue
Block a user