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

79 lines
2.1 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 = {
2022-07-24 11:47:18 +00:00
vault: new Map<number, UnwrapNestedRefs<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
2022-06-10 10:47:46 +00:00
byId (id: number) {
return this.vault.get(id)
2022-04-15 14:24:30 +00:00
},
2022-06-10 10:47:46 +00:00
removeByIds (ids: number[]) {
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
},
isVarious: (artist: Artist | number) => (typeof artist === 'number')
? artist === VARIOUS_ARTISTS_ID
: artist.id === VARIOUS_ARTISTS_ID,
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
isUnknown: (artist: Artist | number) => (typeof artist === 'number')
? artist === UNKNOWN_ARTIST_ID
: artist.id === UNKNOWN_ARTIST_ID,
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
isStandard (artist: Artist | number) {
return !this.isVarious(artist) && !this.isUnknown(artist)
2022-04-15 14:24:30 +00:00
},
async uploadImage (artist: Artist, image: string) {
artist.image = (await http.put<{ image_url: string }>(`artist/${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
},
2022-06-10 10:47:46 +00:00
syncWithVault (artists: Artist | Artist[]) {
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
},
2022-06-10 10:47:46 +00:00
async resolve (id: number) {
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 (e) {
logger.error(e)
}
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) {
const resource = await http.get<PaginatorResource>(`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
}
}