Convert frontend to Vue #3

Merged
jacob1123 merged 158 commits from vue into master 2022-02-08 22:20:11 +01:00
12 changed files with 95 additions and 69 deletions
Showing only changes of commit 7cba471de7 - Show all commits

View File

@ -1,7 +1,8 @@
<script> <script lang="ts">
import { defineComponent } from "vue";
import { LOGOUT } from "./store/mutation-types"; import { LOGOUT } from "./store/mutation-types";
export default { export default defineComponent({
computed: { computed: {
loggedIn() { loggedIn() {
return this.$store.state.Session.Token; return this.$store.state.Session.Token;
@ -19,7 +20,7 @@ export default {
beforeCreate () { beforeCreate () {
this.$store.commit("initializeStore"); this.$store.commit("initializeStore");
} }
} })
</script> </script>
<template> <template>

View File

@ -1,24 +0,0 @@
import { createApp } from 'vue/dist/vue.esm-bundler'
import App from './App.vue'
import router from './router/routes.js'
import store from './store/index.js'
import vuetify from './plugins/vuetify'
import { loadFonts } from './plugins/webfontloader'
import { SET_CURRENT_ACCOUNT, SET_CURRENT_BUDGET } from './store/action-types'
loadFonts()
const app = createApp(App)
app.use(router)
app.use(store)
app.use(vuetify)
app.mount('#app')
router.beforeEach(async (to, from, next) => {
await store.dispatch(SET_CURRENT_BUDGET, to.params.budgetid);
await store.dispatch(SET_CURRENT_ACCOUNT, {
accountid: to.params.accountid,
budgetid: to.params.budgetid
});
next();
})

View File

@ -1,7 +1,9 @@
<script> <script lang="ts">
export default { import { defineComponent } from "vue"
export default defineComponent({
props: ["budgetid", "accountid"] props: ["budgetid", "accountid"]
} })
</script> </script>
<template> <template>

View File

@ -1,7 +1,9 @@
<script> <script lang="ts">
export default { import { defineComponent } from "vue"
export default defineComponent({
props: ['budgetid', 'accountid'], props: ['budgetid', 'accountid'],
} })
</script> </script>
<template> <template>

View File

@ -1,10 +1,11 @@
<script> <script lang="ts">
import NewBudget from '@/dialogs/NewBudget.vue'; import NewBudget from '../dialogs/NewBudget.vue';
import { defineComponent } from 'vue';
export default { export default defineComponent({
props: ["budgetid"], props: ["budgetid"],
components: { NewBudget } components: { NewBudget }
} })
</script> </script>
<template> <template>

View File

@ -1,8 +1,9 @@
<script> <script lang="ts">
import { TITLE } from "../store/mutation-types"; import { TITLE } from "../store/mutation-types";
import { LOGIN } from '../store/action-types' import { LOGIN } from '../store/action-types'
import { defineComponent } from "vue";
export default { export default defineComponent({
data() { data() {
return { return {
error: [], error: [],
@ -30,7 +31,7 @@ export default {
// TODO redirect to dashboard on success // TODO redirect to dashboard on success
} }
} }
} })
</script> </script>
<template> <template>

View File

@ -1,7 +1,8 @@
<script> <script lang="ts">
import { defineComponent } from "vue"
import { TITLE } from "../store/mutation-types" import { TITLE } from "../store/mutation-types"
export default { export default defineComponent({
data() { data() {
return { return {
transactionsFile: null, transactionsFile: null,
@ -51,7 +52,7 @@ export default {
this.$store.dispatch("YNAB", formData); this.$store.dispatch("YNAB", formData);
} }
} }
} })
</script> </script>
<template> <template>

9
web/src/shims-vuex.d.ts vendored Normal file
View File

@ -0,0 +1,9 @@
import { ComponentCustomProperties } from 'vue'
import { State } from './store'
import { Store } from 'vuex'
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$store: Store<State>
}
}

View 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";

View File

@ -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 { 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' 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({ export interface State {
state() { Session: {
return { 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: { Session: {
Token: null, Token: undefined,
User: null User: undefined
}, },
ShowMenu: null, ShowMenu: undefined,
Budgets: [], Budgets: new Map<string, Budget>(),
CurrentBudgetID: null, CurrentBudgetID: undefined,
Accounts: [], Accounts: new Map<string, Account>(),
CurrentAccountID: null, CurrentAccountID: undefined,
Categories: [], Categories: [],
Transactions: [], Transactions: [],
Assignments: [] Assignments: []
}
}, },
mutations: { mutations: {
deleteBudget(state, budgetid) { deleteBudget(state : State, budgetid : string) {
delete state.Budgets[budgetid] state.Budgets.delete(budgetid)
}, },
toggleMenu(state) { toggleMenu(state) {
state.ShowMenu = !state.ShowMenu; state.ShowMenu = !state.ShowMenu;
@ -43,19 +68,19 @@ const store = createStore({
Token: result.Token Token: result.Token
}; };
for (const budget of result.Budgets) { for (const budget of result.Budgets) {
state.Budgets[budget.ID] = budget state.Budgets.set(budget.ID, budget)
} }
}, },
setBudgets(state, budgets) { setBudgets(state, budgets) {
state.Budgets = budgets; state.Budgets = budgets;
}, },
addBudget(state, budget) { addBudget(state, budget) {
state.Budgets.push(budget); state.Budgets.set(budget.ID, budget);
}, },
[LOGOUT](state, token) { [LOGOUT](state, token) {
state.Session = { Token: null, User: null }; state.Session = { Token: undefined, User: undefined };
state.Budgets = {}; state.Budgets.clear();
state.Accounts = []; state.Accounts.clear();
state.Categories = []; state.Categories = [];
state.Transactions = []; state.Transactions = [];
state.Assignments = []; state.Assignments = [];
@ -164,7 +189,7 @@ const store = createStore({
if (state.CurrentBudgetID == null) if (state.CurrentBudgetID == null)
return {}; return {};
return state.Budgets[state.CurrentBudgetID]; return state.Budgets.get(state.CurrentBudgetID);
}, },
Accounts(state) { Accounts(state) {
return state.Accounts || []; return state.Accounts || [];
@ -172,13 +197,13 @@ const store = createStore({
CurrentAccount(state) { CurrentAccount(state) {
if (state.CurrentAccountID == null) if (state.CurrentAccountID == null)
return { name: "Not found" }; return { name: "Not found" };
return state.Accounts.filter(x => x.ID == state.CurrentAccountID)[0]; return state.Accounts.get(state.CurrentAccountID);
}, },
OnBudgetAccounts(state) { OnBudgetAccounts(state) {
return (state.Accounts || []).filter(x => x.OnBudget); return Array.from(state.Accounts.values()).filter(x => x.OnBudget);
}, },
OffBudgetAccounts(state) { OffBudgetAccounts(state) {
return (state.Accounts || []).filter(x => !x.OnBudget); return Array.from(state.Accounts.values()).filter(x => !x.OnBudget);
}, },
Transactions(state) { Transactions(state) {
return (state.Transactions || []); return (state.Transactions || []);
@ -197,6 +222,4 @@ store.subscribe((mutation, state) => {
ShowMenu: state.ShowMenu ShowMenu: state.ShowMenu
} }
localStorage.setItem("store", JSON.stringify(persistedState)); localStorage.setItem("store", JSON.stringify(persistedState));
}) })
export default store