Skip to content

Commit 6b3bda7

Browse files
author
Oussama
committed
fix: clarify quiz progression and data loading
1 parent 85567fb commit 6b3bda7

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

website/src/lib/scenes/Scene5Quiz.svelte

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,43 +29,56 @@
2929
let artists = $state([]);
3030
let loading = $state(false);
3131
let result = $state(null);
32+
let error = $state('');
3233
let loadPromise = null;
3334
3435
async function loadArtists() {
3536
if (artists.length) return artists;
36-
if (!loadPromise) loadPromise = fetch('./data/artist_index.json').then((r) => r.json());
37+
if (!loadPromise) {
38+
loadPromise = fetch(`${import.meta.env.BASE_URL}data/artist_index.json`).then((response) => {
39+
if (!response.ok) throw new Error(`Could not load artist index (${response.status})`);
40+
return response.json();
41+
});
42+
}
3743
artists = await loadPromise;
3844
return artists;
3945
}
4046
4147
function chooseDecade(range) {
4248
answers = { ...answers, decadeRange: range };
43-
window.setTimeout(() => (stage = 1), 200);
49+
stage = 1;
4450
}
4551
4652
function chooseRegion(region) {
4753
answers = { ...answers, region };
48-
window.setTimeout(() => (stage = 2), 200);
54+
stage = 2;
4955
}
5056
5157
async function chooseMedium(medium) {
5258
answers = { ...answers, medium };
5359
stage = 3;
5460
loading = true;
61+
error = '';
5562
const started = performance.now();
56-
const loaded = await loadArtists();
57-
const elapsed = performance.now() - started;
58-
if (elapsed < 600) await new Promise((resolve) => window.setTimeout(resolve, 600 - elapsed));
59-
result = matchArtist(loaded, { ...answers, medium });
60-
loading = false;
61-
stage = 3;
63+
try {
64+
const loaded = await loadArtists();
65+
const elapsed = performance.now() - started;
66+
if (elapsed < 600) await new Promise((resolve) => window.setTimeout(resolve, 600 - elapsed));
67+
result = matchArtist(loaded, { ...answers, medium });
68+
} catch {
69+
error = 'The artist index could not be loaded. Please refresh the page or run the site through Vite.';
70+
} finally {
71+
loading = false;
72+
stage = 3;
73+
}
6274
}
6375
6476
function reset() {
6577
stage = 0;
6678
answers = { decadeRange: null, region: '', medium: '' };
6779
result = null;
6880
loading = false;
81+
error = '';
6982
}
7083
7184
function lifespan(artist) {
@@ -86,6 +99,13 @@
8699
.map((part) => part[0])
87100
.join('');
88101
}
102+
103+
function resultPrompt() {
104+
if (stage === 0) return 'Step 1 of 3: choose a decade.';
105+
if (stage === 1) return 'Decade saved. Pick a region next.';
106+
if (stage === 2) return 'Almost there. Pick a medium to get your match.';
107+
return 'Your artist match will appear here.';
108+
}
89109
</script>
90110
91111
<section class="scene-quiz" aria-labelledby="scene-5-title">
@@ -151,6 +171,12 @@
151171
<span></span>
152172
<p>Finding someone for you...</p>
153173
</div>
174+
{:else if error}
175+
<div class="empty-result">
176+
<div class="portrait preview" aria-hidden="true">!</div>
177+
<p>{error}</p>
178+
<button type="button" onclick={reset}>Try again</button>
179+
</div>
154180
{:else if result?.artist}
155181
<article class="result">
156182
{#if result.relaxed}
@@ -196,7 +222,7 @@
196222
{:else}
197223
<div class="empty-result">
198224
<div class="portrait preview" aria-hidden="true">?</div>
199-
<p>Your artist match will appear here.</p>
225+
<p>{resultPrompt()}</p>
200226
</div>
201227
{/if}
202228
</aside>

0 commit comments

Comments
 (0)