From 93c2464fe6924667df8e0998d4239e9eeb7036f0 Mon Sep 17 00:00:00 2001
From: AkiraFukushima
Date: Thu, 4 Apr 2019 22:59:05 +0900
Subject: [PATCH] refs #850 Replace GlobalHeader with typescript
---
.../integration/store/GlobalHeader.spec.ts | 4 +-
src/renderer/store/GlobalHeader.js | 95 --------------
src/renderer/store/GlobalHeader.ts | 118 ++++++++++++++++++
src/types/account.ts | 13 ++
4 files changed, 133 insertions(+), 97 deletions(-)
delete mode 100644 src/renderer/store/GlobalHeader.js
create mode 100644 src/renderer/store/GlobalHeader.ts
create mode 100644 src/types/account.ts
diff --git a/spec/renderer/integration/store/GlobalHeader.spec.ts b/spec/renderer/integration/store/GlobalHeader.spec.ts
index 38f6e917..d4d41801 100644
--- a/spec/renderer/integration/store/GlobalHeader.spec.ts
+++ b/spec/renderer/integration/store/GlobalHeader.spec.ts
@@ -1,9 +1,9 @@
import { createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import { ipcMain } from '~/spec/mock/electron'
-import GlobalHeader from '~/src/renderer/store/GlobalHeader'
+import GlobalHeader, { GlobalHeaderState } from '~/src/renderer/store/GlobalHeader'
-const state = () => {
+const state = (): GlobalHeaderState => {
return {
accounts: [],
changing: false,
diff --git a/src/renderer/store/GlobalHeader.js b/src/renderer/store/GlobalHeader.js
deleted file mode 100644
index c9ed0f23..00000000
--- a/src/renderer/store/GlobalHeader.js
+++ /dev/null
@@ -1,95 +0,0 @@
-import { ipcRenderer } from 'electron'
-import router from '@/router'
-
-const GlobalHeader = {
- namespaced: true,
- state: {
- accounts: [],
- changing: false,
- hide: false
- },
- mutations: {
- updateAccounts (state, accounts) {
- state.accounts = accounts
- },
- updateChanging (state, value) {
- state.changing = value
- },
- changeHide (state, value) {
- state.hide = value
- }
- },
- actions: {
- listAccounts ({ dispatch, commit }) {
- return new Promise((resolve, reject) => {
- ipcRenderer.send('list-accounts', 'list')
- ipcRenderer.once('error-list-accounts', (event, err) => {
- ipcRenderer.removeAllListeners('response-list-accounts')
- reject(err)
- })
- ipcRenderer.once('response-list-accounts', (event, accounts) => {
- ipcRenderer.removeAllListeners('error-list-accounts')
- commit('updateAccounts', accounts)
- dispatch('refreshAccounts')
- resolve(accounts)
- })
- })
- },
- // Fetch account informations and save current state when GlobalHeader is displayed
- refreshAccounts ({ commit }) {
- return new Promise((resolve, reject) => {
- ipcRenderer.send('refresh-accounts')
- ipcRenderer.once('error-refresh-accounts', (event, err) => {
- ipcRenderer.removeAllListeners('response-refresh-accounts')
- reject(err)
- })
- ipcRenderer.once('response-refresh-accounts', (event, accounts) => {
- ipcRenderer.removeAllListeners('error-refresh-accounts')
- commit('updateAccounts', accounts)
- resolve(accounts)
- })
- })
- },
- watchShortcutEvents ({ state, commit, rootState, rootGetters }) {
- ipcRenderer.on('change-account', (event, account) => {
- if (state.changing) {
- return null
- }
- if (rootState.route.params.id === account._id) {
- return null
- }
- // When the modal window is active, don't change account
- if (rootGetters['TimelineSpace/Modals/modalOpened']) {
- return null
- }
- // changing finish after loading
- commit('updateChanging', true)
- router.push(`/${account._id}/home`)
- })
- },
- async removeShortcutEvents () {
- ipcRenderer.removeAllListeners('change-account')
- return true
- },
- loadHide ({ commit }) {
- return new Promise(resolve => {
- ipcRenderer.send('get-global-header')
- ipcRenderer.once('response-get-global-header', (event, value) => {
- commit('changeHide', value)
- resolve(value)
- })
- })
- },
- switchHide ({ dispatch }, value) {
- return new Promise(resolve => {
- ipcRenderer.send('change-global-header', value)
- ipcRenderer.once('response-change-global-header', () => {
- dispatch('loadHide')
- resolve(true)
- })
- })
- }
- }
-}
-
-export default GlobalHeader
diff --git a/src/renderer/store/GlobalHeader.ts b/src/renderer/store/GlobalHeader.ts
new file mode 100644
index 00000000..5302e803
--- /dev/null
+++ b/src/renderer/store/GlobalHeader.ts
@@ -0,0 +1,118 @@
+import { ipcRenderer } from 'electron'
+import router from '@/router'
+import Account from '~/src/types/account'
+import { Module, MutationTree, ActionTree } from 'vuex'
+
+export interface GlobalHeaderState {
+ accounts: Array,
+ changing: boolean,
+ hide: boolean
+}
+
+const state = (): GlobalHeaderState => ({
+ accounts: [],
+ changing: false,
+ hide: false
+})
+
+export const MUTATION_TYPES = {
+ UPDATE_ACCOUNTS: 'updateAccounts',
+ UPDATE_CHANGING: 'updateChanging',
+ CHANGE_HIDE: 'changeHide'
+}
+
+const mutations: MutationTree = {
+ [MUTATION_TYPES.UPDATE_ACCOUNTS]: (state: GlobalHeaderState, accounts: Array) => {
+ state.accounts = accounts
+ },
+ [MUTATION_TYPES.UPDATE_CHANGING]: (state: GlobalHeaderState, value: boolean) => {
+ state.changing = value
+ },
+ [MUTATION_TYPES.CHANGE_HIDE]: (state: GlobalHeaderState, value: boolean) => {
+ state.hide = value
+ }
+}
+
+// TODO: use type of rootState
+const actions: ActionTree = {
+ listAccounts: ({ dispatch, commit }): Promise> => {
+ return new Promise((resolve, reject) => {
+ ipcRenderer.send('list-accounts', 'list')
+ ipcRenderer.once('error-list-accounts', (_, err: Error) => {
+ ipcRenderer.removeAllListeners('response-list-accounts')
+ reject(err)
+ })
+ ipcRenderer.once('response-list-accounts', (_, accounts: Array) => {
+ ipcRenderer.removeAllListeners('error-list-accounts')
+ commit(MUTATION_TYPES.UPDATE_ACCOUNTS, accounts)
+ dispatch('refreshAccounts')
+ resolve(accounts)
+ })
+ })
+ },
+ // Fetch account informations and save current state when GlobalHeader is displayed
+ refreshAccounts: ({ commit }): Promise> => {
+ return new Promise((resolve, reject) => {
+ ipcRenderer.send('refresh-accounts')
+ ipcRenderer.once('error-refresh-accounts', (_, err: Error) => {
+ ipcRenderer.removeAllListeners('response-refresh-accounts')
+ reject(err)
+ })
+ ipcRenderer.once('response-refresh-accounts', (_, accounts: Array) => {
+ ipcRenderer.removeAllListeners('error-refresh-accounts')
+ commit(MUTATION_TYPES.UPDATE_ACCOUNTS, accounts)
+ resolve(accounts)
+ })
+ })
+ },
+ watchShortcutEvents: ({ state, commit, rootState, rootGetters }) => {
+ ipcRenderer.on('change-account', (_, account: Account) => {
+ if (state.changing) {
+ return null
+ }
+ if (rootState.route.params.id as string === account._id!) {
+ return null
+ }
+ // When the modal window is active, don't change account
+ if (rootGetters['TimelineSpace/Modals/modalOpened']) {
+ return null
+ }
+ // changing finish after loading
+ commit(MUTATION_TYPES.UPDATE_CHANGING, true)
+ router.push(`/${account._id}/home`)
+ return true
+ })
+ },
+ removeShortcutEvents: async () => {
+ ipcRenderer.removeAllListeners('change-account')
+ return true
+ },
+ loadHide: ({ commit }): Promise => {
+ return new Promise(resolve => {
+ ipcRenderer.send('get-global-header')
+ ipcRenderer.once('response-get-global-header', (_, hide: boolean) => {
+ commit(MUTATION_TYPES.CHANGE_HIDE, hide)
+ resolve(hide)
+ })
+ })
+ },
+ switchHide: ({ dispatch }, hide: boolean): Promise => {
+ return new Promise(resolve => {
+ ipcRenderer.send('change-global-header', hide)
+ ipcRenderer.once('response-change-global-header', () => {
+ dispatch('loadHide')
+ resolve(true)
+ })
+ })
+ }
+}
+
+// TODO: use type of rootState
+const GlobalHeader: Module = {
+ namespaced: true,
+ state: state,
+ mutations: mutations,
+ actions: actions
+}
+
+export default GlobalHeader
diff --git a/src/types/account.ts b/src/types/account.ts
new file mode 100644
index 00000000..bbd1efde
--- /dev/null
+++ b/src/types/account.ts
@@ -0,0 +1,13 @@
+export default interface Account {
+ _id?: string,
+ baseURL: string,
+ domain: string,
+ clientId: string,
+ clientSecret: string,
+ accessToken: string | null,
+ refreshToken: string | null,
+ username: string | null,
+ accountId: number | null,
+ avatar: string | null,
+ order: number
+}