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

39 lines
965 B
TypeScript
Raw Normal View History

2024-05-19 05:49:42 +00:00
import { reactive } from 'vue'
import { http } from '@/services'
export const podcastStore = {
state: reactive({
podcasts: [] as Podcast[]
}),
2024-07-05 12:20:30 +00:00
byId (id: Podcast['id']) {
2024-05-19 05:49:42 +00:00
return this.state.podcasts.find(podcast => podcast.id === id)
},
2024-07-05 12:20:30 +00:00
async resolve (id: Podcast['id']) {
2024-05-19 05:49:42 +00:00
return this.byId(id) ?? await this.fetchOne(id)
},
async store (url: string) {
const podcast = await http.post<Podcast>('podcasts', { url })
this.state.podcasts.push(podcast)
return podcast
},
async fetchAll () {
this.state.podcasts = await http.get<Podcast[]>('podcasts')
return this.state.podcasts
},
2024-07-05 12:20:30 +00:00
async fetchOne (id: Podcast['id']) {
2024-05-19 05:49:42 +00:00
this.state.podcasts.push(await http.get<Podcast>(`podcasts/${id}`))
return this.byId(id)!
},
async unsubscribe (podcast: Podcast) {
await http.delete(`podcasts/${podcast.id}/subscriptions`)
this.state.podcasts = this.state.podcasts.filter(p => p.id !== podcast.id)
},
}