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