Whalebird-desktop-client-ma.../src/renderer/store/Preferences/Account.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

import { Module, MutationTree, ActionTree } from 'vuex'
2022-04-23 14:46:17 +02:00
import { toRaw } from 'vue'
import { LocalAccount } from '~/src/types/localAccount'
2019-04-14 16:11:24 +02:00
import { RootState } from '@/store'
import { MyWindow } from '~/src/types/global'
2022-04-23 14:46:17 +02:00
const win = window as any as MyWindow
export type AccountState = {
accounts: Array<LocalAccount>
accountLoading: boolean
}
const state = (): AccountState => ({
accounts: [],
accountLoading: false
})
export const MUTATION_TYPES = {
UPDATE_ACCOUNTS: 'updateAccounts',
UPDATE_ACCOUNT_LOADING: 'updateAccountLoading'
}
const mutations: MutationTree<AccountState> = {
[MUTATION_TYPES.UPDATE_ACCOUNTS]: (state, accounts: Array<LocalAccount>) => {
state.accounts = accounts
},
[MUTATION_TYPES.UPDATE_ACCOUNT_LOADING]: (state, value: boolean) => {
state.accountLoading = value
}
}
2019-04-14 16:11:24 +02:00
const actions: ActionTree<AccountState, RootState> = {
loadAccounts: async ({ commit }): Promise<Array<LocalAccount>> => {
const accounts = await win.ipcRenderer.invoke('list-accounts')
commit(MUTATION_TYPES.UPDATE_ACCOUNTS, accounts)
return accounts
},
removeAccount: async (_, account: LocalAccount) => {
await win.ipcRenderer.invoke('remove-account', account._id)
},
forwardAccount: async (_, account: LocalAccount) => {
2022-04-23 14:46:17 +02:00
await win.ipcRenderer.invoke('forward-account', toRaw(account))
},
backwardAccount: async (_, account: LocalAccount) => {
2022-04-23 14:46:17 +02:00
await win.ipcRenderer.invoke('backward-account', toRaw(account))
},
removeAllAccounts: async () => {
await win.ipcRenderer.invoke('remove-all-accounts')
}
}
2019-04-14 16:11:24 +02:00
const account: Module<AccountState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default account