sunrise-sunset: add basic local storage
This commit is contained in:
parent
e1769f5c6a
commit
9e31ade173
2 changed files with 192 additions and 48 deletions
|
|
@ -1,69 +1,153 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import type { PageData } from "./$types.js";
|
import type { PageData } from "./$types.js";
|
||||||
import Navbar from "$lib/components/Navbar.svelte";
|
import { format as formatDate } from "date-fns";
|
||||||
|
import { onMount } from "svelte";
|
||||||
import { writable } from "svelte/store";
|
import { writable } from "svelte/store";
|
||||||
|
import Navbar from "$lib/components/Navbar.svelte";
|
||||||
|
|
||||||
|
import type { ISunriseSunsetGuessingHistory } from "./ISunriseSunsetGuessingHistory.js";
|
||||||
|
|
||||||
|
let hasGuessingHistoryBeenLoaded = false;
|
||||||
|
const guessingHistory = writable<ISunriseSunsetGuessingHistory>({
|
||||||
|
mostRecentGuessDate: undefined,
|
||||||
|
totalNumberOfGuesses: 0,
|
||||||
|
guesses: [],
|
||||||
|
correctDays: [],
|
||||||
|
incorrectDays: []
|
||||||
|
});
|
||||||
|
|
||||||
export let data: PageData;
|
export let data: PageData;
|
||||||
|
const now = new Date();
|
||||||
|
const todaysDateString = formatDate(now, "yyyy-MM-dd");
|
||||||
|
const localStorageKey = "sunrise-sunset-guessing-history";
|
||||||
|
|
||||||
$: picture = data.body.photo;
|
$: picture = data.body.photo;
|
||||||
|
|
||||||
const guessHistory = writable({
|
function onOptionSelected(option: "sunrise" | "sunset") {
|
||||||
correct: 0,
|
$guessingHistory.mostRecentGuessDate = todaysDateString;
|
||||||
incorrect: 0,
|
$guessingHistory.totalNumberOfGuesses += 1;
|
||||||
total: 0
|
$guessingHistory.guesses = [...$guessingHistory.guesses, option];
|
||||||
|
|
||||||
|
if (option === picture.sunrise_or_sunset) {
|
||||||
|
$guessingHistory.correctDays = [
|
||||||
|
...$guessingHistory.correctDays,
|
||||||
|
todaysDateString
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
$guessingHistory.incorrectDays = [
|
||||||
|
...$guessingHistory.incorrectDays,
|
||||||
|
todaysDateString
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
guessingHistory.subscribe((value) => {
|
||||||
|
console.log("guessingHistory.subscribe", value);
|
||||||
|
|
||||||
|
if (!hasGuessingHistoryBeenLoaded) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
localStorage.setItem(localStorageKey, JSON.stringify(value));
|
||||||
});
|
});
|
||||||
|
|
||||||
function onOptionSelected(option: "sunrise" | "sunset") {
|
onMount(() => {
|
||||||
$guessHistory.total += 1;
|
const storedGuessHistory = localStorage.getItem(localStorageKey);
|
||||||
if (option === picture.sunrise_or_sunset) {
|
if (storedGuessHistory) {
|
||||||
$guessHistory.correct += 1;
|
guessingHistory.set(JSON.parse(storedGuessHistory));
|
||||||
} else {
|
|
||||||
$guessHistory.incorrect += 1;
|
|
||||||
}
|
|
||||||
console.log(option);
|
|
||||||
}
|
}
|
||||||
|
hasGuessingHistoryBeenLoaded = true;
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Navbar />
|
<Navbar />
|
||||||
|
|
||||||
|
<div class="page">
|
||||||
<section class="header">
|
<section class="header">
|
||||||
<h1>Sunrise, Sunset?</h1>
|
<h1 class="header__title">Sunrise, Sunset?</h1>
|
||||||
|
<p class="header__explanation">
|
||||||
|
It's a simple game. Is the picture below a sunrise, or a sunet?
|
||||||
|
</p>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section class="picture">
|
<section class="picture">
|
||||||
<img src={picture.small_url} alt="Sunrise or Sunset?" />
|
<img src={picture.small_url} alt="Sunrise or Sunset?" />
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
{#if hasGuessingHistoryBeenLoaded}
|
||||||
<section class="options">
|
<section class="options">
|
||||||
<div class="options__buttons-container">
|
<div class="options__buttons-container">
|
||||||
<button class="options__button" on:click={() => onOptionSelected("sunrise")}
|
<button
|
||||||
>Sunrise</button
|
disabled={$guessingHistory.mostRecentGuessDate === todaysDateString}
|
||||||
|
class="options__button option--sunrise"
|
||||||
|
on:click={() => onOptionSelected("sunrise")}>Sunrise</button
|
||||||
>
|
>
|
||||||
<button class="options__button" on:click={() => onOptionSelected("sunset")}
|
<button
|
||||||
>Sunset</button
|
disabled={$guessingHistory.mostRecentGuessDate === todaysDateString}
|
||||||
|
class="options__button option--sunset"
|
||||||
|
id="button-sunset"
|
||||||
|
on:click={() => onOptionSelected("sunset")}>Sunset</button
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
|
{#if $guessingHistory.mostRecentGuessDate === todaysDateString}
|
||||||
|
<p class="options__text">
|
||||||
|
You've already guessed today. Come back tomorrow.
|
||||||
|
</p>
|
||||||
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section class="score">
|
<section class="score">
|
||||||
{#if $guessHistory.total > 0}
|
<div class="score__card">
|
||||||
|
<h2 class="score__title">Your Score</h2>
|
||||||
|
{#if $guessingHistory.mostRecentGuessDate !== undefined}
|
||||||
<p class="score__text">
|
<p class="score__text">
|
||||||
You've guessed correctly {Number(
|
You've guessed correctly {Number(
|
||||||
$guessHistory.correct / $guessHistory.total
|
$guessingHistory.correctDays.length /
|
||||||
)}% of the time
|
$guessingHistory.totalNumberOfGuesses
|
||||||
|
) * 100}% of the time
|
||||||
</p>
|
</p>
|
||||||
{:else}
|
{:else}
|
||||||
<p class="score__text">You've not guessed yet.</p>
|
<p class="score__text">You've not guessed yet.</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
<style lang="scss" type="text/postcss">
|
<style lang="scss" type="text/postcss">
|
||||||
|
:root {
|
||||||
|
--background-colour: hsl(40, 8%, 85%);
|
||||||
|
--colour-light-grey: hsl(28, 35%, 85%);
|
||||||
|
--colour-grey: hsl(20, 6%, 10%);
|
||||||
|
--colour-grey-lightened: hsl(20, 7%, 22%);
|
||||||
|
--colour-orage: hsl(19, 69%, 49%);
|
||||||
|
--colour-orange-lightened: hsl(19, 75%, 55%);
|
||||||
|
--colour-dark-grey: #1b1918;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page {
|
||||||
|
background-color: var(--background-colour);
|
||||||
|
display: grid;
|
||||||
|
min-height: calc(100vh - 64px);
|
||||||
|
grid-template-rows: max-content max-content max-content auto;
|
||||||
|
gap: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
.header {
|
.header {
|
||||||
display: grid;
|
display: grid;
|
||||||
place-content: center;
|
place-content: center;
|
||||||
|
text-align: center;
|
||||||
padding: 12px 0px;
|
padding: 12px 0px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.header__title {
|
||||||
|
font-size: 2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header__explanation {
|
||||||
|
font-size: 1.2rem;
|
||||||
|
}
|
||||||
|
|
||||||
.picture {
|
.picture {
|
||||||
display: grid;
|
display: grid;
|
||||||
place-content: center;
|
place-content: center;
|
||||||
|
|
@ -77,27 +161,78 @@
|
||||||
|
|
||||||
.options {
|
.options {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
display: flex;
|
padding: 12px;
|
||||||
justify-content: center;
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
grid-template-columns: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.options__buttons-container {
|
.options__buttons-container {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 50% 50%;
|
|
||||||
gap: 12px;
|
gap: 12px;
|
||||||
|
grid-template-columns: 50% 50%;
|
||||||
|
place-content: center;
|
||||||
|
max-width: 500px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.options__button {
|
.options__button {
|
||||||
background-color: #fff;
|
|
||||||
border: 1px solid #000;
|
border: 1px solid #000;
|
||||||
|
text-align: center;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
padding: 12px;
|
padding: 12px;
|
||||||
font-size: 1.2rem;
|
font-size: 1.2rem;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
transition: all 0.2s ease-in-out;
|
||||||
}
|
}
|
||||||
|
|
||||||
.options__button:hover {
|
.options__button:disabled {
|
||||||
background-color: #000;
|
opacity: 0.5;
|
||||||
color: #fff;
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.option--sunrise {
|
||||||
|
border-color: var(--colour-orage);
|
||||||
|
background-color: var(--colour-orage);
|
||||||
|
color: var(--colour-light-grey);
|
||||||
|
}
|
||||||
|
|
||||||
|
.option--sunrise:hover:not(:disabled) {
|
||||||
|
color: white;
|
||||||
|
background-color: var(--colour-orange-lightened);
|
||||||
|
}
|
||||||
|
|
||||||
|
.option--sunset {
|
||||||
|
border-color: var(--colour-grey-lightened);
|
||||||
|
background-color: var(--colour-grey-lightened);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.option--sunset:hover:not(:disabled) {
|
||||||
|
color: white;
|
||||||
|
background-color: var(--colour-grey);
|
||||||
|
border-color: var(--colour-dark-grey);
|
||||||
|
}
|
||||||
|
|
||||||
|
.score {
|
||||||
|
display: grid;
|
||||||
|
place-content: center;
|
||||||
|
text-align: center;
|
||||||
|
padding: 12px 0px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.score__card {
|
||||||
|
display: grid;
|
||||||
|
place-content: center;
|
||||||
|
background: white;
|
||||||
|
text-align: center;
|
||||||
|
padding: 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
// gentle grey border, with soft shadow
|
||||||
|
border: 1px solid rgba(0, 0, 0, 0.1);
|
||||||
|
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.score__title {
|
||||||
|
font-size: 1.2rem;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
type SunriseSunsetOption = 'sunrise' | 'sunset';
|
||||||
|
|
||||||
|
export interface ISunriseSunsetGuessingHistory {
|
||||||
|
mostRecentGuessDate: undefined | string; // e.g. "2023-01-26"
|
||||||
|
totalNumberOfGuesses: number;
|
||||||
|
guesses: SunriseSunsetOption[];
|
||||||
|
incorrectDays: string[]; // e.g. ["2023-01-26", "2023-01-27"]
|
||||||
|
correctDays: string[]; // e.g. ["2023-01-28", "2023-01-29"]
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue