Add nearToMeCache store using session storage
This commit is contained in:
parent
bebf2eba99
commit
b4a3da5174
@ -9,6 +9,7 @@
|
||||
import InLineLoading from "$lib/navigation/InLineLoading.svelte";
|
||||
import { apiGet } from "$lib/scripts/apiFetch";
|
||||
import { onMount } from "svelte";
|
||||
import { nearToMeCache } from "$lib/stores/nearToMeCache";
|
||||
|
||||
let errorMessage: string;
|
||||
let stations: NearestStationResponse[] = [];
|
||||
@ -18,7 +19,7 @@
|
||||
showHelp: true,
|
||||
showRefresh: true,
|
||||
helpText: "Your location may not be accurate on desktop and laptop devices.",
|
||||
onRefresh: refresh,
|
||||
onRefresh: getNearestStations,
|
||||
refreshing: false,
|
||||
};
|
||||
|
||||
@ -28,19 +29,15 @@
|
||||
toast.success("Done\nTo disable location, go to settings");
|
||||
}
|
||||
|
||||
function refresh() {
|
||||
config.refreshing = true;
|
||||
stations = [];
|
||||
getNearestStations();
|
||||
}
|
||||
|
||||
async function getNearestStations() {
|
||||
// Get location, then fetch nearest stations and push to `stations` variable
|
||||
config.refreshing = true;
|
||||
const currentLocation = await getCurrentLocation();
|
||||
const apiPath: string = `/api/v2/live/station/nearest/${currentLocation.latitude}/${currentLocation.longitude}`;
|
||||
try {
|
||||
const apiResponse = (await apiGet(apiPath)) as NearestStationResponse[];
|
||||
stations = apiResponse;
|
||||
nearToMeCache.set(apiResponse)
|
||||
} catch (err) {
|
||||
errorMessage = err as string;
|
||||
} finally {
|
||||
@ -53,6 +50,9 @@
|
||||
if (!$uuid || $uuid === "null") {
|
||||
errorMessage = "Register to use this feature";
|
||||
} else {
|
||||
if ($nearToMeCache.length) {
|
||||
stations = $nearToMeCache
|
||||
}
|
||||
getNearestStations();
|
||||
}
|
||||
}
|
||||
|
26
src/lib/stores/nearToMeCache.ts
Normal file
26
src/lib/stores/nearToMeCache.ts
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user