Move 'LocationFilter' fetching to API Lib package

This commit is contained in:
2026-03-24 12:47:39 +00:00
parent a7c244171c
commit ec4dd5dd3b
6 changed files with 28 additions and 29 deletions

View File

@@ -4,7 +4,8 @@
import { fade } from 'svelte/transition';
import { goto } from '$app/navigation';
import { LOCATIONS } from '$lib/locations-object.svelte.ts';
import { LOCATIONS } from '$lib/locations-object.svelte';
import type { ApiLocationFilter } from '@owlboard/api-schema-types';
let { value = $bindable() } = $props();
@@ -59,7 +60,7 @@
}
});
function choose(loc: LocationRecord) {
function choose(loc: ApiLocationFilter.LocationFilterObject) {
showResults = false;
selectedIndex = -1;
value = '';

View File

@@ -1,27 +1,23 @@
interface LocationRecord {
n: string; // name
t: string; // tiploc
c?: string; // crs
s: string; // search string
}
import { OwlClient } from "./owlClient";
import type { ApiLocationFilter } from "@owlboard/api-schema-types";
class LocationStore {
data = $state<LocationRecord[]>([]);
data = $state<ApiLocationFilter.LocationFilterObject[]>([]);
loaded = $state(false);
async init(fetcher = fetch) {
async init() {
if (this.loaded) return;
try {
const res = await fetcher('/api/tiplocs');
this.data = await res.json();
const fetch = await OwlClient.locationFilter.getLocationFilterData()
this.data = fetch.data;
this.loaded = true;
} catch (err) {
console.error('Failed to load locations', err);
}
}
find(id: string | null): LocationRecord | undefined {
find(id: string | null): ApiLocationFilter.LocationFilterObject | undefined {
if (!id) return undefined;
const query = id.toUpperCase().trim();

View File

@@ -3,7 +3,7 @@
import { slide, fade } from 'svelte/transition';
import { onMount } from 'svelte';
import { LOCATIONS } from '$lib/locations-object.svelte.ts';
import { LOCATIONS } from '$lib/locations-object.svelte';
import '$lib/global.css';
@@ -13,7 +13,7 @@
import { IconHome, IconDialpad, IconSettings, IconHelp, IconDots } from '@tabler/icons-svelte';
onMount(() => LOCATIONS.init(fetch));
onMount(() => LOCATIONS.init());
let { children } = $props();

View File

@@ -2,11 +2,11 @@
import PisStartEndCard from '$lib/components/ui/cards/pis/PisStartEndCard.svelte';
import PisCode from '$lib/components/ui/cards/pis/PisCode.svelte';
import Button from '$lib/components/ui/Button.svelte';
import type { PisObjects } from '@owlboard/api-schema-types';
import type { ApiPisObject } from '@owlboard/api-schema-types';
import { OwlClient, ApiError, ValidationError } from '$lib/owlClient';
import TocStyle from '$lib/components/ui/TocStyle.svelte';
let results = $state<PisObjects[]>([]);
let results = $state<ApiPisObject.PisObjects[]>([]);
let resultsLoaded = $state<boolean>(false);
let errorState = $state<{status: number, message: string} | null>(null);
@@ -16,7 +16,7 @@
try {
const response = await OwlClient.pis.getByStartEndCrs(start, end);
results = await response.data || [];
results = response.data || [];
} catch (e) {
if (e instanceof ValidationError) {
errorState = { status: 400, message: e.message };
@@ -80,9 +80,9 @@
</tr></thead>
{#each results as result}
<tbody><tr>
<td><TocStyle toc={result.toc} /></td>
<td><TocStyle toc={result.toc || ""} /></td>
<td>{result.code}</td>
<td class="locations-row">{result.crsStops.join(' ')}</td>
<td class="locations-row">{result.crsStops?.join(' ') || ''}</td>
</tr></tbody>
{/each}
</table>