From 0922d468d9356c195eb87747def83fed2f00d04c Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 3 Feb 2021 13:35:31 +0100 Subject: [PATCH] [pinia] Some work on balance store --- src/boot/plugins.ts | 5 +- src/plugins/balance/pages/MainPage.vue | 4 +- src/plugins/balance/plugin.ts | 6 +- src/plugins/balance/store/balance.ts | 257 +++--------------- .../user/components/settings/RoleSettings.vue | 2 - src/plugins/user/pages/Settings.vue | 2 +- 6 files changed, 43 insertions(+), 233 deletions(-) diff --git a/src/boot/plugins.ts b/src/boot/plugins.ts index 6a91fde..38723e5 100644 --- a/src/boot/plugins.ts +++ b/src/boot/plugins.ts @@ -95,7 +95,6 @@ function combineMainLinks( ): FG_Plugin.PluginMainLink[] { const targetPluginMainLink: FG_Plugin.PluginMainLink | undefined = target.find( (targetPluginMainLink: FG_Plugin.PluginMainLink) => { - console.log(targetPluginMainLink.title, source.title); return targetPluginMainLink.title == source.title; } ); @@ -173,7 +172,6 @@ function loadPlugin( loadedPlugins.mainLinks = combineMainLinks(loadedPlugins.mainLinks, route); }); loadedPlugins.shortcuts = loadShortCuts(loadedPlugins.shortcuts, plugin.mainRoutes); - console.log(loadedPlugins); } if (plugin.outRoutes) { loadedPlugins.routes = combineRoutes(loadedPlugins.routes, plugin.outRoutes); @@ -208,7 +206,7 @@ async function getBackend(): Promise { const response: AxiosResponse = await api.get('/'); backend = response.data; } catch (e) { - console.log(e); + console.warn(e); return null; } finally { return backend; @@ -241,7 +239,6 @@ export default boot(({ router, app, store }) => { // check dependencies backendPromise .then((backend) => { - console.log(backend); if (backend) { plugins.forEach((plugin: FG_Plugin.Plugin) => { plugin.requiredModules.forEach((requiredModule: string) => { diff --git a/src/plugins/balance/pages/MainPage.vue b/src/plugins/balance/pages/MainPage.vue index d791e93..9534768 100644 --- a/src/plugins/balance/pages/MainPage.vue +++ b/src/plugins/balance/pages/MainPage.vue @@ -89,12 +89,10 @@ export default defineComponent({ }); }); const transactions = computed(() => { - const a = (balanceGetters.transactions()) + return (balanceGetters.transactions()) .filter((t) => t.original_id == undefined) .filter((t) => t.time > new Date(now.getFullYear(), now.getMonth(), now.getDate())) .sort((a, b) => (a.time >= b.time ? -1 : 1)); - console.log(a); - return a; }); const canAdd = () => diff --git a/src/plugins/balance/plugin.ts b/src/plugins/balance/plugin.ts index 6a7ee9a..414cfc6 100644 --- a/src/plugins/balance/plugin.ts +++ b/src/plugins/balance/plugin.ts @@ -1,17 +1,13 @@ -import { Module } from 'vuex'; import routes from './routes'; import { FG_Plugin } from 'src/plugins'; import { defineAsyncComponent } from 'vue'; -import { StateInterface } from 'src/store'; -import balance, { BalanceInterface } from './store/balance'; const plugin: FG_Plugin.Plugin = { name: 'Balance', mainRoutes: routes, requiredModules: ['User'], requiredBackendModules: ['balance'], - version: '0.0.1', - store: new Map>([['balance', balance]]), + version: '0.0.2', widgets: [ { priority: 0, diff --git a/src/plugins/balance/store/balance.ts b/src/plugins/balance/store/balance.ts index 2c40da6..56cc945 100644 --- a/src/plugins/balance/store/balance.ts +++ b/src/plugins/balance/store/balance.ts @@ -1,7 +1,7 @@ -import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'; -import { StateInterface, useMainStore } from 'src/store'; +import { defineStore } from 'pinia'; import { api } from 'src/boot/axios'; import { AxiosResponse } from 'axios'; +import { useMainStore } from 'src/store'; interface BalanceResponse { balance: number; @@ -18,228 +18,49 @@ export interface TransactionsResponse { count?: number; } -export interface UserBalance extends BalanceResponse { - limit: number | null; -} - -export interface BalanceInterface { - balances: Map; - shortcuts: Array; - transactions: Array; - loading: number; -} - -export interface StateInterfaceBalance { - balance: BalanceInterface; -} - -const state: BalanceInterface = { - balances: new Map(), - shortcuts: [], - transactions: [], - loading: 0, -}; - function fixTransaction(t: FG.Transaction) { t.time = new Date(t.time); } -const mutations: MutationTree = { - setBalance(state, data: { userid: string; balance: BalanceResponse }) { - state.balances.set( - data.userid, - Object.assign({ limit: state.balances.get(data.userid)?.limit || null }, data.balance) - ); - }, - changeBalance(state, data: { userid: string; amount: number }) { - const user = state.balances.get(data.userid); - if (data.amount < 0) user.debit += data.amount; - else user.credit += data.amount; - user.balance += data.amount; - }, - setLimit(state, data: { userid: string; limit: number | null }) { - if (state.balances.has(data.userid)) - (state.balances.get(data.userid)).limit = data.limit; - else state.balances.set(data.userid, { balance: 0, debit: 0, credit: 0, limit: data.limit }); - }, - setLoading(state, data = true) { - if (data) state.loading += 1; - else state.loading -= 1; - }, - setShortcuts(state, data: Array) { - state.shortcuts.splice(0, state.shortcuts.length, ...data); - }, - addTransaction(state, data: FG.Transaction) { - state.transactions.push(data); - }, - addTransactions(state, data: [FG.Transaction]) { - data.forEach((transaction) => { - if (!state.transactions.find((t) => t.id == transaction.id)) { - state.transactions.push(transaction); - } - }); - state.transactions.sort((a, b) => (a.time <= b.time ? -1 : 1)); - }, - reverseTransaction(state, data: { transaction: FG.Transaction; reversal: FG.Transaction }) { - const idx = state.transactions.findIndex((value) => value.id === data.transaction.id); - data.transaction.reversal_id = data.reversal.id; - if (idx > -1) state.transactions[idx] = data.transaction; - else state.transactions.push(data.transaction); - }, -}; +export const balanceStore = defineStore({ + id: 'balance', -const actions: ActionTree = { - //const actions: ActionTree = { - addShortcut({ commit, state }, shortcut) { - const sc = [...state.shortcuts, shortcut]; - sc.sort(); - const mainStore = useMainStore(); - const user = mainStore.currentUser; - return api.put(`/users/${user.userid}/balance/shortcuts`, sc).then(() => { - commit('setShortcuts', sc); - }); - }, - removeShortcut({ commit, state }, shortcut) { - const sc = state.shortcuts.filter((value: number) => value != shortcut); - const mainStore = useMainStore(); - const user = mainStore.currentUser; - return api.put(`/users/${user.userid}/balance/shortcuts`, sc).then(() => { - commit('setShortcuts', sc); - }); - }, - getShortcuts({ commit, state }, force = false) { - if (force || state.shortcuts.length == 0) { + state: () => ({ + balances: [] as BalancesResponse[], + shortcuts: [] as number[], + }), + + getters: { + balance() { const mainStore = useMainStore(); - commit('setLoading'); - const user = mainStore.currentUser; - return api - .get(`/users/${user.userid}/balance/shortcuts`) - .then(({ data }: AxiosResponse) => { - commit('setShortcuts', data); - return data; - }) - .finally(() => commit('setLoading', false)); - } + return this.balances.find((v) => v.userid === mainStore.user?.userid); + }, }, - getBalance({ commit }, user: FG.User | undefined = undefined) { - commit('setLoading'); - const mainStore = useMainStore(); - if (!user) user = mainStore.currentUser; - return api - .get(`/users/${user.userid}/balance`) - .then(({ data }: AxiosResponse) => { - commit('setBalance', { userid: user?.userid, balance: data }); - return data; - }) - .finally(() => commit('setLoading', false)); - }, - getBalances() { - return api.get('/balance').then(({ data }: AxiosResponse>) => { - return data; - }); - }, - getTransactions( - { commit }, - payload: { - userid?: string; - filter?: { - limit?: number; - offset?: number; - from?: Date; - to?: Date; - showReversals?: boolean; - showCancelled?: boolean; - }; - } - ) { - commit('setLoading'); - const mainStore = useMainStore(); - if (!payload.userid) payload.userid = mainStore.currentUser.userid; - if (!payload.filter) payload.filter = { limit: 10 }; - return api - .get(`/users/${payload.userid}/balance/transactions`, { params: payload.filter || {} }) - .then(({ data }: AxiosResponse) => { - data.transactions.forEach((t) => fixTransaction(t)); - commit('addTransactions', data.transactions); - return data; - }) - .finally(() => commit('setLoading', false)); - }, - getLimit({ commit }) { - const mainStore = useMainStore(); - commit('setLoading'); - api - /* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */ - .get(`/users/${mainStore.currentUser.userid}/balance/limit`) - .then(({ data }) => { - console.log(data); - }) - .catch((err) => { - console.warn(err); - }) - .finally(() => commit('setLoading', false)); - }, - revert({ dispatch, commit }, transaction: FG.Transaction) { - return api - .delete(`/balance/${transaction.id}`) - .then(({ data }: AxiosResponse) => { - fixTransaction(data); - commit('reverseTransaction', { transaction: transaction, reversal: data }); - dispatch('getBalance').catch((err) => console.warn(err)); - }); - }, - changeBalance({ dispatch, commit }, data: { amount: number; user: string; sender?: string }) { - commit('setLoading'); - return api - .put(`/users/${data.user}/balance`, data) - .then((response: AxiosResponse) => { + + actions: { + async createShortcut(shortcut: number) { + const mainStore = useMainStore(); + const sc = [...this.shortcuts, shortcut]; + sc.sort(); + await api.put(`/users/${mainStore.currentUser.userid}/balance/shortcuts`, sc); + this.shortcuts = sc; + }, + + async removeShortcut(shortcut: number) { + const mainStore = useMainStore(); + const sc = this.shortcuts.filter((value: number) => value !== shortcut); + await api.put(`/users/${mainStore.currentUser.userid}/balance/shortcuts`, sc); + this.shortcuts = sc; + }, + + async getShortcuts(force = false) { + if (force || this.shortcuts.length == 0) { const mainStore = useMainStore(); - const transaction = response.data; - fixTransaction(transaction); - if ( - data.user == mainStore.currentUser.userid || - data.sender === mainStore.currentUser.userid - ) - commit('addTransaction', transaction); - commit(state.balances.has(data.user) ? 'changeBalance' : 'setBalance', { - userid: data.user, - amount: data.amount, - }); - if (data.sender) - commit(state.balances.has(data.sender) ? 'changeBalance' : 'setBalance', { - userid: data.sender, - amount: -1 * data.amount, - }); - return transaction; - }) - .catch((err) => { - console.debug(err); - // Maybe Balance changed - void dispatch('getTransactions', {}); - return dispatch('getBalance', data.sender ? data.sender : data.user); - }) - .finally(() => commit('setLoading', false)); + const { data } = await api.get( + `/users/${mainStore.currentUser.userid}/balance/shortcuts` + ); + this.shortcuts = data; + } + }, }, -}; - -const getters: GetterTree = { - balances: ({ balances }) => { - return balances; - }, - shortcuts: ({ shortcuts }) => { - return shortcuts; - }, - transactions: ({ transactions }) => { - return transactions; - }, -}; - -const balance: Module = { - namespaced: true, - state, - mutations, - actions, - getters, -}; - -export default balance; +}); diff --git a/src/plugins/user/components/settings/RoleSettings.vue b/src/plugins/user/components/settings/RoleSettings.vue index f89f824..87d69f3 100644 --- a/src/plugins/user/components/settings/RoleSettings.vue +++ b/src/plugins/user/components/settings/RoleSettings.vue @@ -99,12 +99,10 @@ export default defineComponent({ if (role.value) { if (role.value.id === -1) void userStore.newRole(role.value).then((createdRole: FG.Role) => { - console.log(createdRole); role.value = createdRole; }); else { if (newRoleName.value !== '') role.value.name = newRoleName.value; - console.log(role.value); void userStore.updateRole(role.value); } } diff --git a/src/plugins/user/pages/Settings.vue b/src/plugins/user/pages/Settings.vue index aa7b54b..e1f6840 100644 --- a/src/plugins/user/pages/Settings.vue +++ b/src/plugins/user/pages/Settings.vue @@ -20,7 +20,7 @@