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

39 lines
952 B
TypeScript
Raw Normal View History

2022-04-21 16:06:45 +00:00
import { reactive } from 'vue'
import { http } from '@/services'
2022-04-24 08:50:45 +00:00
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) {
const result = await http.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) {
this.state.songs = songStore.syncWithVault(await http.get<Song[]>(`search/songs?q=${q}`))
2022-04-15 14:24:30 +00:00
},
resetSongResultState () {
this.state.songs = []
}
}