2022-04-15 14:24:30 +00:00
|
|
|
import { http } from '@/services'
|
|
|
|
import { songStore } from '@/stores/song'
|
|
|
|
import { albumStore } from '@/stores/album'
|
|
|
|
import { artistStore } from '@/stores/artist'
|
2022-04-21 16:06:45 +00:00
|
|
|
import { reactive } from 'vue'
|
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) {
|
|
|
|
const { results } = await http.get<{ [key: string]: ExcerptSearchResult }>(`search?q=${q}`)
|
|
|
|
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) {
|
|
|
|
const { songs } = await http.get<SongSearchResult>(`search/songs?q=${q}`)
|
|
|
|
this.state.songs = this.state.songs.concat(songStore.byIds(songs))
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
resetSongResultState () {
|
|
|
|
this.state.songs = []
|
|
|
|
}
|
|
|
|
}
|