Front: Use history include to get best/last score for a song
This commit is contained in:
committed by
Clément Le Bihan
parent
b4f04f9b71
commit
7a6dc8b0c9
+16
-2
@@ -6,6 +6,7 @@ import ResponseHandler from './ResponseHandler';
|
||||
import API from '../API';
|
||||
import { AlbumValidator } from './Album';
|
||||
import { GenreValidator } from './Genre';
|
||||
import { SongHistoryItem, SongHistoryItemValidator } from './SongHistory';
|
||||
|
||||
export type SongInclude = 'artist' | 'album' | 'genre' | 'SongHistory' | 'likedByUsers';
|
||||
|
||||
@@ -20,20 +21,33 @@ export const SongValidator = yup
|
||||
difficulties: SongDetailsValidator.required(),
|
||||
details: SongDetailsValidator.required(),
|
||||
cover: yup.string().required(),
|
||||
SongHistory: yup
|
||||
.lazy(() => yup.array(SongHistoryItemValidator.default(undefined)))
|
||||
.optional(),
|
||||
bestScore: yup.number().optional().nullable(),
|
||||
lastScore: yup.number().optional().nullable(),
|
||||
artist: yup.lazy(() => ArtistValidator.default(undefined)).optional(),
|
||||
album: yup.lazy(() => AlbumValidator.default(undefined)).optional(),
|
||||
genre: yup.lazy(() => GenreValidator.default(undefined)).optional(),
|
||||
})
|
||||
.concat(ModelValidator)
|
||||
.transform((song: Song) => ({
|
||||
.transform((song: Song & { SongHistory: SongHistoryItem[] }) => ({
|
||||
...song,
|
||||
cover: `${API.baseUrl}/song/${song.id}/illustration`,
|
||||
details: song.difficulties,
|
||||
bestScore:
|
||||
song.SongHistory?.map(({ info }) => info.score)
|
||||
.sort()
|
||||
.at(-1) ?? null,
|
||||
lastScore:
|
||||
song.SongHistory?.map(({ info, playDate }) => ({ info, playDate }))
|
||||
.sort((a, b) => yup.date().cast(a.playDate)!.getTime() - yup.date().cast(b.playDate)!.getTime())
|
||||
.at(0)?.info.score ?? null,
|
||||
}));
|
||||
|
||||
export type Song = yup.InferType<typeof SongValidator>;
|
||||
|
||||
export const SongHandler: ResponseHandler<Song> = {
|
||||
export const SongHandler: ResponseHandler<yup.InferType<typeof SongValidator>> = {
|
||||
validator: SongValidator,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user