Merge pull request #870 from h3poteto/iss-850

refs #850 Replace SideMenu with typescript
This commit is contained in:
AkiraFukushima 2019-04-08 22:48:26 +09:00 committed by GitHub
commit 358d86ef26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 145 additions and 101 deletions

View File

@ -3,7 +3,8 @@ import mockedMegalodon from '~/spec/mock/megalodon'
import { createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import { ipcMain } from '~/spec/mock/electron'
import SideMenu from '~/src/renderer/store/TimelineSpace/SideMenu'
import SideMenu, { SideMenuState } from '~/src/renderer/store/TimelineSpace/SideMenu'
import Hashtag from '~/src/types/hashtag'
jest.mock('megalodon')
@ -17,10 +18,11 @@ const list2: List = {
title: 'example2'
}
const state = () => {
const state = (): SideMenuState => {
return {
unreadHomeTimeline: false,
unreadNotifications: false,
unreadMentions: false,
unreadLocalTimeline: false,
unreadDirectMessagesTimeline: false,
unreadPublicTimeline: false,
@ -114,11 +116,17 @@ describe('SideMenu', () => {
describe('listTags', () => {
it('should be listed', async () => {
const tag1: Hashtag = {
tagName: 'tag1'
}
const tag2: Hashtag = {
tagName: 'tag2'
}
ipcMain.once('list-hashtags', (event: any, _) => {
event.sender.send('response-list-hashtags', ['tag1', 'tag2'])
event.sender.send('response-list-hashtags', [tag1, tag2])
})
await store.dispatch('SideMenu/listTags')
expect(store.state.SideMenu.tags).toEqual(['tag1', 'tag2'])
expect(store.state.SideMenu.tags).toEqual([tag1, tag2])
})
})
})

View File

@ -1,97 +0,0 @@
import Mastodon from 'megalodon'
import { ipcRenderer } from 'electron'
const SideMenu = {
namespaced: true,
state: {
unreadHomeTimeline: false,
unreadNotifications: false,
unreadMentions: false,
unreadLocalTimeline: false,
unreadDirectMessagesTimeline: false,
unreadPublicTimeline: false,
lists: [],
tags: [],
collapse: false
},
mutations: {
changeUnreadHomeTimeline (state, value) {
state.unreadHomeTimeline = value
},
changeUnreadNotifications (state, value) {
state.unreadNotifications = value
},
changeUnreadMentions (state, value) {
state.unreadMentions = value
},
changeUnreadLocalTimeline (state, value) {
state.unreadLocalTimeline = value
},
changeUnreadDirectMessagesTimeline (state, value) {
state.unreadDirectMessagesTimeline = value
},
changeUnreadPublicTimeline (state, value) {
state.unreadPublicTimeline = value
},
updateLists (state, lists) {
state.lists = lists
},
changeCollapse (state, collapse) {
state.collapse = collapse
},
updateTags (state, tags) {
state.tags = tags
}
},
actions: {
fetchLists ({ commit, rootState }, account = null) {
if (account === null) account = rootState.TimelineSpace.account
const client = new Mastodon(
account.accessToken,
account.baseURL + '/api/v1'
)
return client.get('/lists')
.then(res => {
commit('updateLists', res.data)
return res.data
})
},
clearUnread ({ commit }) {
commit('changeUnreadHomeTimeline', false)
commit('changeUnreadNotifications', false)
commit('changeUnreadMentions', false)
commit('changeUnreadLocalTimeline', false)
commit('changeUnreadDirectMessagesTimeline', false)
commit('changeUnreadPublicTimeline', false)
},
changeCollapse ({ commit }, value) {
commit('changeCollapse', value)
ipcRenderer.send('change-collapse', value)
},
readCollapse ({ commit }) {
return new Promise(resolve => {
ipcRenderer.send('get-collapse')
ipcRenderer.once('response-get-collapse', (event, value) => {
commit('changeCollapse', value)
resolve(value)
})
})
},
listTags ({ commit }) {
return new Promise((resolve, reject) => {
ipcRenderer.once('response-list-hashtags', (event, tags) => {
ipcRenderer.removeAllListeners('error-list-hashtags')
commit('updateTags', tags)
resolve(tags)
})
ipcRenderer.once('error-list-hashtags', (event, err) => {
ipcRenderer.removeAlListeners('response-list-hashtags')
reject(err)
})
ipcRenderer.send('list-hashtags')
})
}
}
}
export default SideMenu

View File

@ -0,0 +1,129 @@
import Mastodon, { List, Response } from 'megalodon'
import { ipcRenderer } from 'electron'
import { Module, MutationTree, ActionTree } from 'vuex'
import Hashtag from '~/src/types/hashtag'
import Account from '~/src/types/account'
export interface SideMenuState {
unreadHomeTimeline: boolean,
unreadNotifications: boolean,
unreadMentions: boolean,
unreadLocalTimeline: boolean,
unreadDirectMessagesTimeline: boolean,
unreadPublicTimeline: boolean,
lists: Array<List>,
tags: Array<Hashtag>,
collapse: boolean
}
const state: SideMenuState = {
unreadHomeTimeline: false,
unreadNotifications: false,
unreadMentions: false,
unreadLocalTimeline: false,
unreadDirectMessagesTimeline: false,
unreadPublicTimeline: false,
lists: [],
tags: [],
collapse: false
}
export const MUTATION_TYPES = {
CHANGE_UNREAD_HOME_TIMELINE: 'changeUnreadHomeTimeline',
CHANGE_UNREAD_NOTIFICATIONS: 'changeUnreadNotifications',
CHANGE_UNREAD_MENTIONS: 'changeUnreadMentions',
CHANGE_UNREAD_LOCAL_TIMELINE: 'changeUnreadLocalTimeline',
CHANGE_UNREAD_DIRECT_MESSAGES_TIMELINE: 'changeUnreadDirectMessagesTimeline',
CHANGE_UNREAD_PUBLIC_TIMELINE: 'changeUnreadPublicTimeline',
UPDATE_LISTS: 'updateLists',
CHANGE_COLLAPSE: 'changeCollapse',
UPDATE_TAGS: 'updateTags'
}
const mutations: MutationTree<SideMenuState> = {
[MUTATION_TYPES.CHANGE_UNREAD_HOME_TIMELINE]: (state, value: boolean) => {
state.unreadHomeTimeline = value
},
[MUTATION_TYPES.CHANGE_UNREAD_NOTIFICATIONS]: (state, value: boolean) => {
state.unreadNotifications = value
},
[MUTATION_TYPES.CHANGE_UNREAD_MENTIONS]: (state, value: boolean) => {
state.unreadMentions = value
},
[MUTATION_TYPES.CHANGE_UNREAD_LOCAL_TIMELINE]: (state, value: boolean) => {
state.unreadLocalTimeline = value
},
[MUTATION_TYPES.CHANGE_UNREAD_DIRECT_MESSAGES_TIMELINE]: (state, value: boolean) => {
state.unreadDirectMessagesTimeline = value
},
[MUTATION_TYPES.CHANGE_UNREAD_PUBLIC_TIMELINE]: (state, value: boolean) => {
state.unreadPublicTimeline = value
},
[MUTATION_TYPES.UPDATE_LISTS]: (state, lists: Array<List>) => {
state.lists = lists
},
[MUTATION_TYPES.CHANGE_COLLAPSE]: (state, collapse: boolean) => {
state.collapse = collapse
},
[MUTATION_TYPES.UPDATE_TAGS]: (state, tags: Array<Hashtag>) => {
state.tags = tags
}
}
// TODO: use type of rootState
const actions: ActionTree<SideMenuState, any> = {
fetchLists: async ({ commit, rootState }, account: Account | null = null): Promise<Array<List>> => {
if (account === null) account = rootState.TimelineSpace.account
const client = new Mastodon(
account!.accessToken!,
account!.baseURL + '/api/v1'
)
const res: Response<Array<List>> = await client.get<Array<List>>('/lists')
commit(MUTATION_TYPES.UPDATE_LISTS, res.data)
return res.data
},
clearUnread: ({ commit }) => {
commit(MUTATION_TYPES.CHANGE_UNREAD_HOME_TIMELINE, false)
commit(MUTATION_TYPES.CHANGE_UNREAD_NOTIFICATIONS, false)
commit(MUTATION_TYPES.CHANGE_UNREAD_MENTIONS, false)
commit(MUTATION_TYPES.CHANGE_UNREAD_LOCAL_TIMELINE, false)
commit(MUTATION_TYPES.CHANGE_UNREAD_DIRECT_MESSAGES_TIMELINE, false)
commit(MUTATION_TYPES.CHANGE_UNREAD_PUBLIC_TIMELINE, false)
},
changeCollapse: ({ commit }, value: boolean) => {
ipcRenderer.send('change-collapse', value)
commit(MUTATION_TYPES.CHANGE_COLLAPSE, value)
},
readCollapse: ({ commit }) => {
return new Promise(resolve => {
ipcRenderer.send('get-collapse')
ipcRenderer.once('response-get-collapse', (_, value: boolean) => {
commit(MUTATION_TYPES.CHANGE_COLLAPSE, value)
resolve(value)
})
})
},
listTags: ({ commit }) => {
return new Promise((resolve, reject) => {
ipcRenderer.once('response-list-hashtags', (_, tags: Array<Hashtag>) => {
ipcRenderer.removeAllListeners('error-list-hashtags')
commit(MUTATION_TYPES.UPDATE_TAGS, tags)
resolve(tags)
})
ipcRenderer.once('error-list-hashtags', (_, err: Error) => {
ipcRenderer.removeAllListeners('response-list-hashtags')
reject(err)
})
ipcRenderer.send('list-hashtags')
})
}
}
const SideMenu: Module<SideMenuState, any> = {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
}
export default SideMenu

4
src/types/hashtag.ts Normal file
View File

@ -0,0 +1,4 @@
export default interface Hasthag {
tagName: string,
_id?: string
}