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