Merge pull request #877 from h3poteto/iss-850

refs #850 Replace Search with typescript
This commit is contained in:
AkiraFukushima 2019-04-14 18:42:54 +09:00 committed by GitHub
commit d27562c5b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 182 additions and 116 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

@ -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,19 +0,0 @@
import Account from './Search/Account'
import Tag from './Search/Tag'
import Toots from './Search/Toots'
const Search = {
namespaced: true,
modules: { Account, Tag, Toots },
state: {
loading: false
},
mutations: {
changeLoading (state, loading) {
state.loading = loading
}
},
actions: {}
}
export default Search

View File

@ -0,0 +1,38 @@
import Account, { AccountState } from './Search/Account'
import Tag, { TagState } from './Search/Tag'
import Toots, { TootsState } from './Search/Toots'
import { Module, MutationTree } from 'vuex'
import { RootState } from '@/store'
export interface SearchState {
loading: boolean
}
export interface SearchModuleState extends SearchState {
Account: AccountState,
Tag: TagState,
Toots: TootsState
}
const state = (): SearchState => ({
loading: false
})
export const MUTATION_TYPES = {
CHANGE_LOADING: 'changeLoading'
}
const mutations: MutationTree<SearchState> = {
[MUTATION_TYPES.CHANGE_LOADING]: (state, loading: boolean) => {
state.loading = loading
}
}
const Search: Module<SearchState, RootState> = {
namespaced: true,
modules: { Account, Tag, Toots },
state: state,
mutations: mutations
}
export default Search

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>

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,46 @@
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>

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