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

79 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-07-24 11:47:18 +00:00
import { reactive, UnwrapNestedRefs } from 'vue'
import { differenceBy, unionBy } from 'lodash'
import { cache, http } from '@/services'
2022-07-24 11:47:18 +00:00
import { arrayify, logger } from '@/utils'
2022-04-15 14:24:30 +00:00
const UNKNOWN_ARTIST_ID = 1
const VARIOUS_ARTISTS_ID = 2
export const artistStore = {
2024-07-05 12:20:30 +00:00
vault: new Map<Artist['id'], Artist>(),
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
state: reactive({
artists: [] as Artist[]
2022-04-21 09:38:24 +00:00
}),
2022-04-15 14:24:30 +00:00
2024-07-05 12:20:30 +00:00
byId (id: Artist['id']) {
2022-06-10 10:47:46 +00:00
return this.vault.get(id)
2022-04-15 14:24:30 +00:00
},
2024-07-05 12:20:30 +00:00
removeByIds (ids: Artist['id'][]) {
2022-07-22 21:56:13 +00:00
this.state.artists = differenceBy(this.state.artists, ids.map(id => this.byId(id)), 'id')
2022-06-10 10:47:46 +00:00
ids.forEach(id => this.vault.delete(id))
2022-04-15 14:24:30 +00:00
},
2024-07-05 12:20:30 +00:00
isVarious: (artist: Artist | Artist['id']) => (typeof artist === 'number')
? artist === VARIOUS_ARTISTS_ID
: artist.id === VARIOUS_ARTISTS_ID,
2022-04-15 14:24:30 +00:00
2024-07-05 12:20:30 +00:00
isUnknown: (artist: Artist | Artist['id']) => (typeof artist === 'number')
2022-06-10 10:47:46 +00:00
? artist === UNKNOWN_ARTIST_ID
: artist.id === UNKNOWN_ARTIST_ID,
2022-04-15 14:24:30 +00:00
2024-07-05 12:20:30 +00:00
isStandard (artist: Artist | Artist['id']) {
2022-06-10 10:47:46 +00:00
return !this.isVarious(artist) && !this.isUnknown(artist)
2022-04-15 14:24:30 +00:00
},
async uploadImage (artist: Artist, image: string) {
2024-02-24 15:37:01 +00:00
artist.image = (await http.put<{ image_url: string }>(`artists/${artist.id}/image`, { image })).image_url
2022-07-22 21:56:13 +00:00
// sync to vault
this.byId(artist.id)!.image = artist.image
2022-06-10 10:47:46 +00:00
return artist.image
2022-04-15 14:24:30 +00:00
},
2024-07-05 12:20:30 +00:00
syncWithVault (artists: MaybeArray<Artist>) {
2022-06-10 10:47:46 +00:00
return arrayify(artists).map(artist => {
let local = this.vault.get(artist.id)
local = local ? Object.assign(local, artist) : reactive(artist)
this.vault.set(artist.id, local)
2022-04-28 09:00:20 +00:00
2022-06-10 10:47:46 +00:00
return local
})
2022-04-15 14:24:30 +00:00
},
2024-07-05 12:20:30 +00:00
async resolve (id: Artist['id']) {
2022-06-10 10:47:46 +00:00
let artist = this.byId(id)
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
if (!artist) {
2022-07-24 11:47:18 +00:00
try {
artist = this.syncWithVault(
await cache.remember<Artist>(['artist', id], async () => await http.get<Artist>(`artists/${id}`))
2022-07-24 11:47:18 +00:00
)[0]
} catch (error: unknown) {
logger.error(error)
2022-07-24 11:47:18 +00:00
}
2022-04-15 14:24:30 +00:00
}
2022-06-10 10:47:46 +00:00
return artist
2022-04-15 14:24:30 +00:00
},
2022-07-22 22:03:25 +00:00
async paginate (page: number) {
2024-07-05 10:04:31 +00:00
const resource = await http.get<PaginatorResource<Artist>>(`artists?page=${page}`)
2022-07-22 21:56:13 +00:00
this.state.artists = unionBy(this.state.artists, this.syncWithVault(resource.data), 'id')
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
return resource.links.next ? ++resource.meta.current_page : null
2022-04-15 14:24:30 +00:00
}
}