Migrate components to new preferences store

This commit is contained in:
Fred Boniface 2025-10-14 20:42:43 +01:00
parent 65856adb33
commit 56ff114f5d
5 changed files with 56 additions and 27 deletions

View File

@ -5,13 +5,14 @@
import type { CardConfig } from "./Card.types"; import type { CardConfig } from "./Card.types";
import type { NearestStationResponse } from "@owlboard/ts-types"; import type { NearestStationResponse } from "@owlboard/ts-types";
import { uuid } from "$lib/stores/uuid"; import { uuid } from "$lib/stores/uuid";
import { location } from "$lib/stores/location"; import { preferences } from "$lib/stores/preferences";
import InLineLoading from "$lib/navigation/InLineLoading.svelte"; import InLineLoading from "$lib/navigation/InLineLoading.svelte";
import { apiGet } from "$lib/scripts/apiFetch"; import { apiGet } from "$lib/scripts/apiFetch";
import { onMount } from "svelte"; import { onMount } from "svelte";
import { nearToMeCache } from "$lib/stores/nearToMeCache"; import { nearToMeCache } from "$lib/stores/nearToMeCache";
import LinkButton from "$lib/buttons/LinkButton.svelte"; import LinkButton from "$lib/buttons/LinkButton.svelte";
import ScriptButton from "$lib/buttons/ScriptButton.svelte"; import ScriptButton from "$lib/buttons/ScriptButton.svelte";
import { togglePreference } from "$lib/scripts/preferenceUtil";
let errorMessage: string; let errorMessage: string;
let stations: NearestStationResponse[] = []; let stations: NearestStationResponse[] = [];
@ -26,8 +27,8 @@
refreshing: false, refreshing: false,
}; };
function turnOnLocation() { function turnOnLocation(): void {
location.set(true); togglePreference("location", true);
getCurrentLocation(); getCurrentLocation();
toast.success("Done\nTo disable location, go to settings"); toast.success("Done\nTo disable location, go to settings");
} }
@ -49,7 +50,7 @@
} }
onMount(() => { onMount(() => {
if ($location) { if ($preferences.location) {
if (!$uuid || $uuid === "null") { if (!$uuid || $uuid === "null") {
errorMessage = "Register to use this feature"; errorMessage = "Register to use this feature";
} else { } else {
@ -70,7 +71,7 @@
<div id="buttons"> <div id="buttons">
{#if !$uuid || $uuid === "null"} {#if !$uuid || $uuid === "null"}
<LinkButton text="Register to use this feature" link="/more/reg" /> <LinkButton text="Register to use this feature" link="/more/reg" />
{:else if $location} {:else if $preferences.location}
{#if !stations.length} {#if !stations.length}
{#if errorMessage} {#if errorMessage}
<p>{errorMessage}</p> <p>{errorMessage}</p>

View File

@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import Card from "./Card.svelte"; import Card from "./Card.svelte";
import type { CardConfig } from "./Card.types"; import type { CardConfig } from "./Card.types";
import { ql } from "$lib/stores/quick-links"; import { preferences } from "$lib/stores/preferences";
import LinkButton from "$lib/buttons/LinkButton.svelte"; import LinkButton from "$lib/buttons/LinkButton.svelte";
let upstreamProps: CardConfig = { let upstreamProps: CardConfig = {
@ -16,11 +16,11 @@
<Card config={upstreamProps}> <Card config={upstreamProps}>
<div class="quick-links"> <div class="quick-links">
{#if !$ql.length} {#if !$preferences.ql.length}
<LinkButton text={"Add Quick Links"} link={"/more/settings"} /> <LinkButton text={"Add Quick Links"} link={"/more/settings"} />
{:else} {:else}
{#each $ql as link} {#each $preferences.ql as link}
{#if link.length === 3} {#if link.length === 3}
<LinkButton text={link.toUpperCase()} link={`/ldb?station=${link.toLowerCase()}`} /> <LinkButton text={link.toUpperCase()} link={`/ldb?station=${link.toLowerCase()}`} />
{:else if link.length === 4} {:else if link.length === 4}

View File

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import Island from "$lib/islands/island.svelte"; import Island from "$lib/islands/island.svelte";
import { ql } from "$lib/stores/quick-links"; import type { Preferences } from "$lib/stores/preferences";
import { preferences } from "$lib/stores/preferences";
import toast from "svelte-french-toast"; import toast from "svelte-french-toast";
export let variables = { export let variables = {
title: "Quick Links", title: "Quick Links",
@ -8,7 +9,7 @@
let qlData: string[] = []; let qlData: string[] = [];
$: { $: {
qlData = $ql; qlData = $preferences.ql;
console.log(qlData); console.log(qlData);
} }
@ -18,7 +19,7 @@
return new Promise((resolve) => setTimeout(resolve, ms)); return new Promise((resolve) => setTimeout(resolve, ms));
} }
function save() { function save(): void {
toast.promise(saveQl(), { toast.promise(saveQl(), {
loading: "Saving...", loading: "Saving...",
success: "Quick Links saved!", success: "Quick Links saved!",
@ -26,7 +27,7 @@
}); });
} }
async function saveQl() { async function saveQl(): Promise<void> {
// Fetch the content of all text entries within the island then run ql.set([ARRAY OF INPUT CONTENT]) // Fetch the content of all text entries within the island then run ql.set([ARRAY OF INPUT CONTENT])
const inputs = document.getElementsByClassName("qlInput"); const inputs = document.getElementsByClassName("qlInput");
let inputLinks: string[] = []; let inputLinks: string[] = [];
@ -37,25 +38,33 @@
} }
} }
console.log(inputLinks); console.log(inputLinks);
ql.set(inputLinks); preferences.update((p: Preferences) => ({
...p,
ql: inputLinks,
}))
await timeout(1000); await timeout(1000);
saveButton = "Saved"; saveButton = "Saved";
} }
function clearQl() { function clearQl(): void {
ql.set([]); preferences.update((p: Preferences) => ({
...p,
ql: [],
}))
saveButton = "Saved"; saveButton = "Saved";
toast.success("Cleared Quick Links."); toast.success("Cleared Quick Links.");
} }
function addQlBox() { function addQlBox(): void {
saveButton = "Save"; saveButton = "Save";
const updatedQl = [...$ql, ""]; const updatedQl = [...qlData, ""];
$ql = updatedQl; preferences.update((p: Preferences) => ({
ql.set(updatedQl); ...p,
ql: updatedQl,
}))
} }
function handleClick(event: any) { function handleClick(event: any): void {
// Handle the click event here // Handle the click event here
console.log("Island Clicked"); console.log("Island Clicked");
// You can access the `variables` passed to the Island component here if needed // You can access the `variables` passed to the Island component here if needed
@ -63,7 +72,7 @@
</script> </script>
<Island on:click={handleClick} {variables}> <Island on:click={handleClick} {variables}>
{#if $ql.length === 0} {#if qlData.length === 0}
<p>Click the + button to add links</p> <p>Click the + button to add links</p>
{/if} {/if}
<div id="buttons" class="buttons"> <div id="buttons" class="buttons">

View File

@ -0,0 +1,9 @@
import type { Preferences } from "$lib/stores/preferences";
import { preferences } from "$lib/stores/preferences";
export function togglePreference<K extends keyof Preferences>(key: K, value: boolean): void {
preferences.update(p => ({
...p,
[key]: value,
}));
}

View File

@ -1,21 +1,31 @@
<script> <script lang="ts">
import Header from "$lib/navigation/header.svelte"; import Header from "$lib/navigation/header.svelte";
import Nav from "$lib/navigation/nav.svelte"; import Nav from "$lib/navigation/nav.svelte";
import QlSet from "$lib/islands/quick-link-set-island.svelte"; import QlSet from "$lib/islands/quick-link-set-island.svelte";
import Island from "$lib/islands/island.svelte"; import Island from "$lib/islands/island.svelte";
import { location } from "$lib/stores/location"; import { preferences } from "$lib/stores/preferences";
import { telemetry } from "$lib/stores/telemetryConsent"; import { togglePreference } from "$lib/scripts/preferenceUtil";
import { getCurrentLocation } from "$lib/scripts/getLocation"; import { getCurrentLocation } from "$lib/scripts/getLocation";
import toast from "svelte-french-toast"; import toast from "svelte-french-toast";
const title = "Settings"; const title = "Settings";
$: if ($location) { $: if ($preferences.location) {
getCurrentLocation(); getCurrentLocation();
} }
function confirmationToast() { function confirmationToast() {
toast.success("Settings updated"); toast.success("Settings updated");
} }
function onTelemetryChange(event: Event) {
const checked = (event.target as HTMLInputElement).checked;
togglePreference("telemetry", checked);
}
function onLocationChange(event: Event) {
const checked = (event.target as HTMLInputElement).checked;
togglePreference("location", checked);
}
</script> </script>
<svelte:head> <svelte:head>
@ -30,7 +40,7 @@
<p>Use your location to quickly check departure boards near you</p> <p>Use your location to quickly check departure boards near you</p>
<div class="checkbox-container"> <div class="checkbox-container">
<label for="location_enable">Enabled</label> <label for="location_enable">Enabled</label>
<input id="location_enable" type="checkbox" bind:checked={$location} on:click={confirmationToast} /> <input id="location_enable" type="checkbox" checked={$preferences.location} on:click={confirmationToast} on:change={onLocationChange}/>
</div> </div>
</Island> </Island>
@ -40,7 +50,7 @@
</p> </p>
<div class="checkbox-container"> <div class="checkbox-container">
<label for="telemetry_enable">Enabled</label> <label for="telemetry_enable">Enabled</label>
<input id="telemetry_enable" type="checkbox" bind:checked={$telemetry} on:click={confirmationToast} /> <input id="telemetry_enable" type="checkbox" checked={$preferences.telemetry} on:click={confirmationToast} on:change={onTelemetryChange}/>
</Island> </Island>
<Nav /> <Nav />