refs #850 Replace Account in Search with typescript

This commit is contained in:
AkiraFukushima 2019-04-14 17:44:20 +09:00
parent 3921d7f7fe
commit dd2361e821
4 changed files with 51 additions and 35 deletions

View File

@ -4,7 +4,7 @@ import Notifications, { NotificationsState } from './Contents/Notifications'
import Favourites from './Contents/Favourites'
import Local, { LocalState } from './Contents/Local'
import Public, { PublicState } from './Contents/Public'
import Search from './Contents/Search'
import Search, { SearchModuleState } from './Contents/Search'
import Lists from './Contents/Lists'
import Hashtag from './Contents/Hashtag'
import DirectMessages, { DirectMessagesState } from './Contents/DirectMessages'
@ -21,7 +21,8 @@ export interface ContentsModuleState extends ContentsState {
Mentions: MentionsState,
DirectMessages: DirectMessagesState,
Local: LocalState,
Public: PublicState
Public: PublicState,
Search: SearchModuleState,
}
const state = (): ContentsState => ({})

View File

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

View File

@ -1,32 +0,0 @@
import Mastodon from 'megalodon'
const Account = {
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.accounts)
return res.data
})
.finally(() => {
commit('TimelineSpace/Contents/Search/changeLoading', false, { root: true })
})
}
}
}
export default Account

View File

@ -0,0 +1,46 @@
import Mastodon, { Account, Results } from 'megalodon'
import { Module, MutationTree, ActionTree } from 'vuex'
import { RootState } from '@/store'
export interface AccountState {
results: Array<Account>
}
const state = (): AccountState => ({
results: []
})
export const MUTATION_TYPES = {
UPDATE_RESULTS: 'updateResults'
}
const mutations: MutationTree<AccountState> = {
[MUTATION_TYPES.UPDATE_RESULTS]: (state, results: Array<Account>) => {
state.results = results
}
}
const actions: ActionTree<AccountState, RootState> = {
search: async ({ commit, rootState }, query: string): Promise<Array<Account>> => {
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.accounts)
return res.data.accounts
})
.finally(() => {
commit('TimelineSpace/Contents/Search/changeLoading', false, { root: true })
})
}
}
export default {
namespaced: true,
state: state,
mutations: mutations,
actions: actions
} as Module<AccountState, RootState>