Files
web-pwa/src/lib/locations-object.svelte.ts

38 lines
865 B
TypeScript

interface LocationRecord {
n: string; // name
t: string; // tiploc
c?: string; // crs
s: string; // search string
}
class LocationStore {
data = $state<LocationRecord[]>([]);
loaded = $state(false);
async init(fetcher = fetch) {
if (this.loaded) return;
try {
const res = await fetcher('/api/tiplocs');
this.data = await res.json();
this.loaded = true;
} catch (err) {
console.error('Failed to load locations', err);
}
}
find(id: string | null): LocationRecord | undefined {
if (!id) return undefined;
const query = id.toUpperCase().trim();
console.log(query);
return this.data.find((loc) => {
return loc.t === query || loc.c === query;
});
}
}
export const LOCATIONS = new LocationStore();