Add nearToMeCache store using session storage

This commit is contained in:
Fred Boniface 2024-07-05 10:45:38 +01:00
parent bebf2eba99
commit b4a3da5174
2 changed files with 33 additions and 7 deletions

View File

@ -9,6 +9,7 @@
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";
let errorMessage: string; let errorMessage: string;
let stations: NearestStationResponse[] = []; let stations: NearestStationResponse[] = [];
@ -18,7 +19,7 @@
showHelp: true, showHelp: true,
showRefresh: true, showRefresh: true,
helpText: "Your location may not be accurate on desktop and laptop devices.", helpText: "Your location may not be accurate on desktop and laptop devices.",
onRefresh: refresh, onRefresh: getNearestStations,
refreshing: false, refreshing: false,
}; };
@ -28,19 +29,15 @@
toast.success("Done\nTo disable location, go to settings"); toast.success("Done\nTo disable location, go to settings");
} }
function refresh() {
config.refreshing = true;
stations = [];
getNearestStations();
}
async function getNearestStations() { async function getNearestStations() {
// Get location, then fetch nearest stations and push to `stations` variable // Get location, then fetch nearest stations and push to `stations` variable
config.refreshing = true;
const currentLocation = await getCurrentLocation(); const currentLocation = await getCurrentLocation();
const apiPath: string = `/api/v2/live/station/nearest/${currentLocation.latitude}/${currentLocation.longitude}`; const apiPath: string = `/api/v2/live/station/nearest/${currentLocation.latitude}/${currentLocation.longitude}`;
try { try {
const apiResponse = (await apiGet(apiPath)) as NearestStationResponse[]; const apiResponse = (await apiGet(apiPath)) as NearestStationResponse[];
stations = apiResponse; stations = apiResponse;
nearToMeCache.set(apiResponse)
} catch (err) { } catch (err) {
errorMessage = err as string; errorMessage = err as string;
} finally { } finally {
@ -53,6 +50,9 @@
if (!$uuid || $uuid === "null") { if (!$uuid || $uuid === "null") {
errorMessage = "Register to use this feature"; errorMessage = "Register to use this feature";
} else { } else {
if ($nearToMeCache.length) {
stations = $nearToMeCache
}
getNearestStations(); getNearestStations();
} }
} }

View File

@ -0,0 +1,26 @@
import { writable, type Writable } from "svelte/store";
import { browser } from "$app/environment";
import type { NearestStationResponse } from "@owlboard/ts-types";
export const nearToMeCache = writable(fromSessionStorage("nearToMeCache", []));
toSessionStorage(nearToMeCache, "nearToMeCache");
function fromSessionStorage(storageKey: string, fallback: NearestStationResponse[]): NearestStationResponse[] {
if (browser) {
const storedValue = sessionStorage.getItem(storageKey);
if (storedValue !== "undefined" && storedValue !== null) {
return typeof fallback === "object" ? JSON.parse(storedValue) : storedValue;
}
}
return fallback;
}
function toSessionStorage(store: Writable<NearestStationResponse[]>, storageKey: string) {
if (browser) {
store.subscribe((value) => {
let storageValue = typeof value === "object" ? JSON.stringify(value.sort()) : value;
sessionStorage.setItem(storageKey, storageValue);
});
}
}