Whalebird-desktop-client-ma.../src/renderer/store/Settings/Timeline.ts

79 lines
2.7 KiB
TypeScript
Raw Normal View History

import unreadSettings from '~/src/constants/unreadNotification'
import { Module, MutationTree, ActionTree } from 'vuex'
2019-04-14 16:11:24 +02:00
import { RootState } from '@/store'
import { UnreadNotification } from '~/src/types/unreadNotification'
import { MyWindow } from '~/src/types/global'
const win = window as MyWindow
export type TimelineState = {
unreadNotification: UnreadNotification
}
const state = (): TimelineState => ({
unreadNotification: {
direct: unreadSettings.Direct.default,
local: unreadSettings.Local.default,
public: unreadSettings.Public.default
}
})
export const MUTATION_TYPES = {
UPDATE_UNREAD_NOTIFICATION: 'updateUnreadNotification'
}
const mutations: MutationTree<TimelineState> = {
[MUTATION_TYPES.UPDATE_UNREAD_NOTIFICATION]: (state, settings: UnreadNotification) => {
state.unreadNotification = settings
}
}
2019-04-14 16:11:24 +02:00
const actions: ActionTree<TimelineState, RootState> = {
loadUnreadNotification: ({ commit, rootState }): Promise<boolean> => {
return new Promise(resolve => {
win.ipcRenderer.once('response-get-unread-notification', (_, settings: UnreadNotification) => {
win.ipcRenderer.removeAllListeners('error-get-unread-notification')
commit(MUTATION_TYPES.UPDATE_UNREAD_NOTIFICATION, settings)
resolve(true)
})
win.ipcRenderer.once('error-get-unread-notification', () => {
win.ipcRenderer.removeAllListeners('response-get-unread-notification')
const settings: UnreadNotification = {
direct: unreadSettings.Direct.default,
local: unreadSettings.Local.default,
public: unreadSettings.Public.default
}
commit(MUTATION_TYPES.UPDATE_UNREAD_NOTIFICATION, settings)
resolve(false)
})
win.ipcRenderer.send('get-unread-notification', rootState.Settings.accountID)
})
},
2019-06-05 16:16:07 +02:00
changeUnreadNotification: ({ dispatch, state, rootState }, timeline: { key: boolean }): Promise<boolean> => {
const settings: UnreadNotification = Object.assign({}, state.unreadNotification, timeline, {
accountID: rootState.Settings.accountID
})
return new Promise((resolve, reject) => {
win.ipcRenderer.once('response-update-unread-notification', () => {
win.ipcRenderer.removeAllListeners('error-update-unread-notification')
dispatch('loadUnreadNotification')
resolve(true)
})
win.ipcRenderer.once('error-update-unread-notification', (_, err: Error) => {
win.ipcRenderer.removeAllListeners('response-update-unread-notification')
reject(err)
})
win.ipcRenderer.send('update-unread-notification', settings)
})
}
}
2019-04-14 16:11:24 +02:00
const Timeline: Module<TimelineState, RootState> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default Timeline