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

41 lines
1.1 KiB
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
interface ExcerptSearchResult {
songs: Array<string>
albums: Array<number>
artists: Array<number>
}
interface SongSearchResult {
songs: Array<string>
}
export const searchStore = {
2022-04-21 16:06:45 +00:00
state: reactive({
2022-04-15 14:24:30 +00:00
excerpt: {
songs: [] as Song[],
albums: [] as Album[],
artists: [] as Artist[]
},
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-04-24 08:50:45 +00:00
const { results } = await httpService.get<{ [key: string]: ExcerptSearchResult }>(`search?q=${q}`)
2022-04-21 16:06:45 +00:00
this.state.excerpt.songs = songStore.byIds(results.songs)
this.state.excerpt.albums = albumStore.byIds(results.albums)
this.state.excerpt.artists = artistStore.byIds(results.artists)
2022-04-15 14:24:30 +00:00
},
2022-04-21 16:06:45 +00:00
async songSearch (q: string) {
2022-04-24 08:50:45 +00:00
const { songs } = await httpService.get<SongSearchResult>(`search/songs?q=${q}`)
2022-04-21 16:06:45 +00:00
this.state.songs = this.state.songs.concat(songStore.byIds(songs))
2022-04-15 14:24:30 +00:00
},
resetSongResultState () {
this.state.songs = []
}
}