refs #850 Replace Tag in Search with typescript

This commit is contained in:
AkiraFukushima 2019-04-14 17:51:01 +09:00
parent dd2361e821
commit 15a0719844
3 changed files with 50 additions and 32 deletions

View File

@ -1,5 +1,5 @@
import Account, { AccountState } from './Search/Account' import Account, { AccountState } from './Search/Account'
import Tag from './Search/Tag' import Tag, { TagState } from './Search/Tag'
import Toots from './Search/Toots' import Toots from './Search/Toots'
import { Module, MutationTree } from 'vuex' import { Module, MutationTree } from 'vuex'
import { RootState } from '@/store' import { RootState } from '@/store'
@ -9,7 +9,8 @@ export interface SearchState {
} }
export interface SearchModuleState extends SearchState { export interface SearchModuleState extends SearchState {
Account: AccountState Account: AccountState,
Tag: TagState
} }
const state = (): SearchState => ({ const state = (): SearchState => ({

View File

@ -1,30 +0,0 @@
import Mastodon from 'megalodon'
export default {
namespaced: true,
state: {
results: []
},
mutations: {
updateResults (state, results) {
state.results = results
}
},
actions: {
search ({ commit, rootState }, query) {
commit('TimelineSpace/Contents/Search/changeLoading', true, { root: true })
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken,
rootState.TimelineSpace.account.baseURL + '/api/v2'
)
return client.get('/search', { q: query, resolve: true })
.then(res => {
commit('updateResults', res.data.hashtags)
return res.data
})
.finally(() => {
commit('TimelineSpace/Contents/Search/changeLoading', false, { root: true })
})
}
}
}

View File

@ -0,0 +1,47 @@
import Mastodon, { Tag, Results } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface TagState {
results: Array<Tag>
}
const state = (): TagState => ({
results: []
})
export const MUTATION_TYPES = {
UPDATE_RESULTS: 'updateResults'
}
const mutations: MutationTree<TagState> = {
[MUTATION_TYPES.UPDATE_RESULTS]: (state, results: Array<Tag>) => {
state.results = results
}
}
const actions: ActionTree<TagState, RootState> = {
search: ({ commit, rootState }, query: string): Promise<Array<Tag>> => {
commit('TimelineSpace/Contents/Search/changeLoading', true, { root: true })
const client = new Mastodon(
rootState.TimelineSpace.account.accessToken!,
rootState.TimelineSpace.account.baseURL + '/api/v2'
)
return client.get<Results>('/search', { q: query, resolve: true })
.then(res => {
commit(MUTATION_TYPES.UPDATE_RESULTS, res.data.hashtags)
return res.data.hashtags
})
.finally(() => {
commit('TimelineSpace/Contents/Search/changeLoading', false, { root: true })
})
}
}
export default {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
} as Module<TagState, RootState>