Fix case, where pakubiiti recieved only 2 albums from the server
Use a while loop instead of the previous for loop Cap the amount of retries to not create infinite loops Show an error message on the client when this cap is reached without getting the required amount of albums
This commit is contained in:
parent
e08b0e87fb
commit
a3687ac56b
3 changed files with 32 additions and 7 deletions
|
@ -1,15 +1,19 @@
|
|||
import { albumState } from '$lib/server/AlbumState.svelte';
|
||||
import { spotifyAPI } from '$lib/server/Spotify.svelte';
|
||||
import type { AlbumSolveState } from '$lib/types';
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { error, json } from '@sveltejs/kit';
|
||||
|
||||
const maxTries = 10;
|
||||
|
||||
export async function GET({ params }) {
|
||||
const count = params.count || 1;
|
||||
const count = Number(params.count) || 1;
|
||||
|
||||
const albums: AlbumSolveState[] = [];
|
||||
let tries = 0;
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
while (albums.length < count && tries++ < maxTries) {
|
||||
const album = await spotifyAPI.getRandomAlbum();
|
||||
|
||||
if (album) {
|
||||
const image = album.images.at(0);
|
||||
if (!image?.url) {
|
||||
|
@ -24,6 +28,12 @@ export async function GET({ params }) {
|
|||
}
|
||||
}
|
||||
|
||||
if (albums.length !== count) {
|
||||
albumState.setAlbums([]);
|
||||
|
||||
return error(500, "Couldn't get albums from Spotify.");
|
||||
}
|
||||
|
||||
albumState.setAlbums(albums);
|
||||
|
||||
return json({ albums: albums });
|
||||
|
|
|
@ -56,6 +56,11 @@ export const load: PageServerLoad = async ({ fetch, locals }) => {
|
|||
images: shuffleArray(albumImages),
|
||||
artists: shuffleArray(albumArtists)
|
||||
};
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err);
|
||||
|
||||
return undefined;
|
||||
});
|
||||
|
||||
return {
|
||||
|
|
|
@ -112,10 +112,20 @@
|
|||
</div>
|
||||
{@render footer(true)}
|
||||
{:then albums}
|
||||
{#if albums}
|
||||
<div class="grid w-full gap-4" in:fade={{ duration: 150, delay: 150, easing: expoOut }}>
|
||||
{@render playArea(albums as AlbumData)}
|
||||
</div>
|
||||
{@render footer(false)}
|
||||
{:else}
|
||||
<p class="mx-auto mt-16 max-w-prose text-center text-lg text-red-500">
|
||||
<strong>Serveris tekkis mingi error.</strong>
|
||||
<br />No clue miks see katki on, sorry.
|
||||
</p>
|
||||
<p class="mx-auto mt-6 max-w-prose text-center text-lg text-red-500">
|
||||
Proovi uuesti või kirjuta mulle.
|
||||
</p>
|
||||
{/if}
|
||||
{/await}
|
||||
{:else}
|
||||
<div class="grid w-full gap-4" out:fade={{ duration: 150, easing: expoOut }}>
|
||||
|
|
Loading…
Add table
Reference in a new issue