48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import { OwlClient } from './owlClient';
|
|
import type { ApiLocationFilter } from '@owlboard/owlboard-ts';
|
|
|
|
class LocationStore {
|
|
data = $state<ApiLocationFilter.LocationFilterObject[]>([]);
|
|
loaded = $state(false);
|
|
|
|
async init() {
|
|
if (this.loaded) return;
|
|
|
|
try {
|
|
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): ApiLocationFilter.LocationFilterObject | 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;
|
|
});
|
|
}
|
|
|
|
getName(code: string | number | null | undefined): ApiLocationFilter.LocationFilterObject | null {
|
|
try {
|
|
if (!code) return null;
|
|
|
|
const query = String(code).toUpperCase().trim();
|
|
|
|
const match = this.data.find((loc) => loc.t === query || loc.c === query);
|
|
return match ?? null;
|
|
} catch (e) {
|
|
console.error('Error finding location object: ', e);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
export const LOCATIONS = new LocationStore();
|