2022-06-10 10:47:46 +00:00
|
|
|
import { reactive } from 'vue'
|
2022-07-22 22:19:42 +00:00
|
|
|
import { differenceBy, unionBy } from 'lodash'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { http } from '@/services'
|
2022-04-20 09:37:22 +00:00
|
|
|
import { arrayify } from '@/utils'
|
2022-06-10 10:47:46 +00:00
|
|
|
import { songStore } from '@/stores'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
export const favoriteStore = {
|
2024-06-02 17:15:31 +00:00
|
|
|
state: reactive<{ playables: Playable[] }>({
|
|
|
|
playables: []
|
2022-04-21 16:06:45 +00:00
|
|
|
}),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
async toggleOne (playable: Playable) {
|
2022-04-15 14:24:30 +00:00
|
|
|
// Don't wait for the HTTP response to update the status, just toggle right away.
|
|
|
|
// This may cause a minor problem if the request fails somehow, but do we care?
|
2024-05-19 05:49:42 +00:00
|
|
|
playable.liked = !playable.liked
|
|
|
|
playable.liked ? this.add(playable) : this.remove(playable)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2024-06-02 17:15:31 +00:00
|
|
|
await http.post<Playable>('interaction/like', { song: playable.id })
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
add (songs: MaybeArray<Playable>) {
|
2024-06-02 17:15:31 +00:00
|
|
|
this.state.playables = unionBy(this.state.playables, arrayify(songs), 'id')
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
remove (songs: MaybeArray<Playable>) {
|
2024-06-02 17:15:31 +00:00
|
|
|
this.state.playables = differenceBy(this.state.playables, arrayify(songs), 'id')
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
async like (songs: Playable[]) {
|
2022-04-15 14:24:30 +00:00
|
|
|
// Don't wait for the HTTP response to update the status, just set them to Liked right away.
|
|
|
|
// This may cause a minor problem if the request fails somehow, but do we care?
|
2022-06-10 10:47:46 +00:00
|
|
|
songs.forEach(song => (song.liked = true))
|
2022-04-15 14:24:30 +00:00
|
|
|
this.add(songs)
|
|
|
|
|
2022-09-15 09:07:25 +00:00
|
|
|
await http.post('interaction/batch/like', { songs: songs.map(song => song.id) })
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
async unlike (songs: Playable[]) {
|
2022-06-10 10:47:46 +00:00
|
|
|
songs.forEach(song => (song.liked = false))
|
2022-04-15 14:24:30 +00:00
|
|
|
this.remove(songs)
|
|
|
|
|
2022-09-15 09:07:25 +00:00
|
|
|
await http.post('interaction/batch/unlike', { songs: songs.map(song => song.id) })
|
2022-06-10 10:47:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async fetch () {
|
2024-06-02 17:15:31 +00:00
|
|
|
this.state.playables = songStore.syncWithVault(await http.get<Playable[]>('songs/favorite'))
|
|
|
|
return this.state.playables
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|