refs #850 Replace Toots in Search with typescript

This commit is contained in:
AkiraFukushima 2019-04-14 18:38:49 +09:00
parent 15a0719844
commit b66790a9aa
5 changed files with 52 additions and 36 deletions

View File

@ -24,7 +24,7 @@ export const MUTATION_TYPES = {
UPDATE_TIMELINE: 'updateTimeline',
MERGE_TIMELINE: 'mergeTimeline',
INSERT_TIMELINE: 'insertTimeline',
ARCHIVE_TIMELINE: 'archiveTimeine',
ARCHIVE_TIMELINE: 'archiveTimeline',
CLEAR_TIMELINE: 'clearTimeline',
UPDATE_TOOT: 'updateToot',
DELETE_TOOT: 'deleteToot',

View File

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

View File

@ -2,7 +2,6 @@ import Mastodon, { Tag, Results } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface TagState {
results: Array<Tag>
}

View File

@ -1,32 +0,0 @@
import Mastodon from 'megalodon'
const Toots = {
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/v1'
)
return client.get('/search', { q: query, resolve: true })
.then(res => {
commit('updateResults', res.data.statuses)
return res.data
})
.finally(() => {
commit('TimelineSpace/Contents/Search/changeLoading', false, { root: true })
})
}
}
}
export default Toots

View File

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