From f934bed772de4f18771893b67a91259d2d6b9cb9 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 27 Jan 2023 23:12:06 +0000 Subject: [PATCH] sunrise-sunset: add success/failure notification --- src/routes/sunrise-sunset/+page.svelte | 70 +++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/src/routes/sunrise-sunset/+page.svelte b/src/routes/sunrise-sunset/+page.svelte index 2c8547f..6f6e4f6 100644 --- a/src/routes/sunrise-sunset/+page.svelte +++ b/src/routes/sunrise-sunset/+page.svelte @@ -2,12 +2,17 @@ import type { PageData } from "./$types.js"; import { format as formatDate } from "date-fns"; import { onMount } from "svelte"; + import { spring } from "svelte/motion"; + import { fade } from "svelte/transition"; import { writable } from "svelte/store"; import Navbar from "$lib/components/Navbar.svelte"; import type { ISunriseSunsetGuessingHistory } from "./ISunriseSunsetGuessingHistory.js"; let hasGuessingHistoryBeenLoaded = false; + let debug = false; + let visibleNotification: "none" | "success" | "failure" = "none"; + const guessingHistory = writable({ mostRecentGuessDate: undefined, totalNumberOfGuesses: 0, @@ -16,6 +21,12 @@ incorrectDays: [] }); + const notificationSpring = spring(20, { + stiffness: 0.1, + damping: 0.15, + precision: 0.01 + }); + export let data: PageData; const now = new Date(); const todaysDateString = formatDate(now, "yyyy-MM-dd"); @@ -23,17 +34,41 @@ $: picture = data.body.photo; + function debugRemoveLocalStorage() { + notificationSpring.set(20); + localStorage.removeItem(localStorageKey); + visibleNotification = "none"; + guessingHistory.set({ + mostRecentGuessDate: undefined, + totalNumberOfGuesses: 0, + guesses: [], + correctDays: [], + incorrectDays: [] + }); + } + + function revealNotification(wasCorrect: boolean) { + notificationSpring.set(0); + if (wasCorrect) { + visibleNotification = "success"; + } else { + visibleNotification = "failure"; + } + } + function onOptionSelected(option: "sunrise" | "sunset") { $guessingHistory.mostRecentGuessDate = todaysDateString; $guessingHistory.totalNumberOfGuesses += 1; $guessingHistory.guesses = [...$guessingHistory.guesses, option]; if (option === picture.sunrise_or_sunset) { + revealNotification(true); $guessingHistory.correctDays = [ ...$guessingHistory.correctDays, todaysDateString ]; } else { + revealNotification(false); $guessingHistory.incorrectDays = [ ...$guessingHistory.incorrectDays, todaysDateString @@ -66,15 +101,30 @@

Sunrise, Sunset?

- It's a simple game. Is the picture below a sunrise, or a sunet? + It's a simple game. Is the picture below a sunrise, or a sunset?

+ {#if debug} + + {/if} +
Sunrise or Sunset?
{#if hasGuessingHistoryBeenLoaded} +
+ {#if visibleNotification !== "none"} +
+ {visibleNotification === "success" ? "Correct 🎉" : "Incorrect 💔"} +
+ {/if} +