Return and save to state only the neccessary album data for paku biiti

This commit is contained in:
Mihkel Martin Kasterpalu 2025-01-22 14:02:22 +02:00
parent c7d7ca349d
commit 8360d5d6de
4 changed files with 38 additions and 21 deletions

View file

@ -1,9 +1,9 @@
import type { AlbumSolveState } from '$lib/types'; import type { AlbumData, AlbumSolveState } from '$lib/types';
class AlbumState { class AlbumState {
private albums: SpotifyApi.AlbumObjectSimplified[] | undefined = undefined; private albums: AlbumSolveState[] | undefined = undefined;
setAlbums(data: SpotifyApi.AlbumObjectSimplified[]) { setAlbums(data: AlbumSolveState[]) {
if (!data) { if (!data) {
return; return;
} }
@ -29,14 +29,12 @@ class AlbumState {
return false; return false;
} }
if (matching.images.at(0)?.url !== solve.imageUrl) { if (matching.image !== solve.image) {
return false; return false;
} }
for (const artistName of solve.artists) { if (matching.artists !== solve.artists) {
if (!matching.artists.find((artist) => artist.name === artistName)) { return false;
return false;
}
} }
} }

View file

@ -1,7 +1,18 @@
export type AlbumData = {
name: AlbumDataField;
artists: AlbumDataField;
image: AlbumDataField;
};
export type AlbumDataField = {
id: string;
value: string;
};
export type AlbumSolveState = { export type AlbumSolveState = {
name: string; name: string;
artists: string[]; artists: string;
imageUrl: string; image: string;
}; };
export type Player = { export type Player = {

View file

@ -1,7 +1,7 @@
import { shuffleArray } from '$lib/utils'; import { shuffleArray } from '$lib/utils';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import type { AlbumSolveState } from '$lib/types'; import type { AlbumData, AlbumSolveState } from '$lib/types';
import { albumState } from '$lib/server/AlbumState.svelte'; import { albumState } from '$lib/server/AlbumState.svelte';
import { playerState } from '$lib/server/PlayerState.svelte'; import { playerState } from '$lib/server/PlayerState.svelte';
@ -38,17 +38,17 @@ export const load: PageServerLoad = async ({ fetch, locals }) => {
return res.json(); return res.json();
}) })
.then((data) => { .then((data) => {
const albumNames = data.albums.map((album: SpotifyApi.AlbumObjectSimplified) => ({ const albumNames = data.albums.map((album: AlbumData) => ({
id: nanoid(), id: nanoid(),
value: album.name value: album.name
})); }));
const albumImages = data.albums.map((album: SpotifyApi.AlbumObjectSimplified) => ({ const albumImages = data.albums.map((album: AlbumData) => ({
id: nanoid(), id: nanoid(),
value: album.images.at(0)?.url || '' value: album.image
})); }));
const albumArtists = data.albums.map((album: SpotifyApi.AlbumObjectSimplified) => ({ const albumArtists = data.albums.map((album: AlbumData) => ({
id: nanoid(), id: nanoid(),
value: album.artists.map((artist) => artist.name).join(', ') value: album.artists
})); }));
return { return {
@ -84,9 +84,7 @@ export const actions = {
const image = data.get(`images_${i}`); const image = data.get(`images_${i}`);
const artists = data.get(`artists_${i}`); const artists = data.get(`artists_${i}`);
const artistList = artists.split(', '); state.push({ name: name, image: image, artists: artists });
state.push({ name: name, imageUrl: image, artists: artistList });
} }
const solved = albumState.checkSolve(state); const solved = albumState.checkSolve(state);

View file

@ -1,16 +1,26 @@
import { albumState } from '$lib/server/AlbumState.svelte'; import { albumState } from '$lib/server/AlbumState.svelte';
import { spotifyAPI } from '$lib/server/Spotify.svelte'; import { spotifyAPI } from '$lib/server/Spotify.svelte';
import type { AlbumSolveState } from '$lib/types';
import { json } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
export async function GET({ params }) { export async function GET({ params }) {
const count = params.count || 1; const count = params.count || 1;
const albums: SpotifyApi.AlbumObjectSimplified[] = []; const albums: AlbumSolveState[] = [];
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
const album = await spotifyAPI.getRandomAlbum(); const album = await spotifyAPI.getRandomAlbum();
if (album) { if (album) {
albums.push(album); const image = album.images.at(0);
if (!image?.url) {
return;
}
albums.push({
name: album.name,
artists: album.artists.map((artist) => artist.name).join(', '),
image: image.url
});
} }
} }