koel/resources/assets/js/stores/searchStore.ts

39 lines
973 B
TypeScript
Raw Normal View History

2022-04-21 16:06:45 +00:00
import { reactive } from 'vue'
2022-04-24 08:50:45 +00:00
import { httpService } from '@/services'
import { albumStore, artistStore, songStore } from '@/stores'
2022-04-15 14:24:30 +00:00
2022-07-23 11:07:14 +00:00
type ExcerptState = {
songs: Song[],
albums: Album[],
artists: Artist[]
}
export type ExcerptSearchResult = ExcerptState
2022-04-15 14:24:30 +00:00
export const searchStore = {
2022-04-21 16:06:45 +00:00
state: reactive({
2022-04-15 14:24:30 +00:00
excerpt: {
2022-07-23 11:07:14 +00:00
songs: [],
albums: [],
artists: []
} as ExcerptState,
2022-04-21 16:06:45 +00:00
songs: [] as Song[]
}),
2022-04-15 14:24:30 +00:00
2022-04-21 16:06:45 +00:00
async excerptSearch (q: string) {
2022-07-23 11:07:14 +00:00
const result = await httpService.get<ExcerptSearchResult>(`search?q=${q}`)
2022-06-10 10:47:46 +00:00
this.state.excerpt.songs = songStore.syncWithVault(result.songs)
this.state.excerpt.albums = albumStore.syncWithVault(result.albums)
this.state.excerpt.artists = artistStore.syncWithVault(result.artists)
2022-04-15 14:24:30 +00:00
},
2022-04-21 16:06:45 +00:00
async songSearch (q: string) {
2022-06-10 10:47:46 +00:00
this.state.songs = songStore.syncWithVault(await httpService.get<Song[]>(`search/songs?q=${q}`))
2022-04-15 14:24:30 +00:00
},
resetSongResultState () {
this.state.songs = []
}
}