Add train headcode search

This commit is contained in:
2026-04-27 23:57:04 +01:00
parent 3225b60140
commit abb8663766
7 changed files with 124 additions and 10 deletions

11
package-lock.json generated
View File

@@ -8,12 +8,12 @@
"name": "web-pwa",
"version": "0.0.1",
"dependencies": {
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2348",
"@tabler/icons-svelte": "^3.40.0"
},
"devDependencies": {
"@eslint/compat": "^2.0.2",
"@eslint/js": "^9.39.2",
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2231",
"@playwright/test": "^1.58.1",
"@sveltejs/adapter-static": "^3.0.10",
"@sveltejs/kit": "^2.50.2",
@@ -783,14 +783,12 @@
"version": "3.0.3-alpha1",
"resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fapi-schema-types/-/3.0.3-alpha1/api-schema-types-3.0.3-alpha1.tgz",
"integrity": "sha512-UWe2nbJWb2B/LuZW1UXHJ2lpqOGwugiXTa4G6X5xLiaws3ISEdciweorX8kr2/JAz5+iFYIe1xXRFAWsFtpn/w==",
"dev": true,
"license": "MIT"
},
"node_modules/@owlboard/owlboard-ts": {
"version": "3.0.0-dev.20260427T2231",
"resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fowlboard-ts/-/3.0.0-dev.20260427T2231/owlboard-ts-3.0.0-dev.20260427t2231.tgz",
"integrity": "sha512-EJ0gcPMmXDrWLg5ukO4Y5Li/XKOrcPhEK0awCWSQHvRNR1iFSCQlxIOooFjIOFNKvuYk3kLpZZ1WDl77yOUUKA==",
"dev": true,
"version": "3.0.0-dev.20260427T2348",
"resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fowlboard-ts/-/3.0.0-dev.20260427T2348/owlboard-ts-3.0.0-dev.20260427t2348.tgz",
"integrity": "sha512-ZZ6h8zSnL/FyEZ6ZSekhK781/5TYMj6y6atRVwKiPH6TfXxauMdEimYCOKQIcoskseH7wItEZRFGAOUDAY3q7w==",
"license": "GPL-3.0",
"dependencies": {
"@owlboard/api-schema-types": "^3.0.3-alpha1",
@@ -2669,7 +2667,6 @@
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/latlon-geohash/-/latlon-geohash-2.0.0.tgz",
"integrity": "sha512-OKBswTwrvTdtenV+9C9euBmvgGuqyjJNAzpQCarRz1m8/pYD2nz9fKkXmLs2S3jeXaLi3Ry76twQplKKUlgS/g==",
"dev": true,
"license": "MIT"
},
"node_modules/levn": {

View File

@@ -41,7 +41,7 @@
"vitest-browser-svelte": "^2.0.2"
},
"dependencies": {
"@tabler/icons-svelte": "^3.40.0",
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2231"
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2348",
"@tabler/icons-svelte": "^3.40.0"
}
}

5
src/app.d.ts vendored
View File

@@ -2,7 +2,10 @@
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
interface Error {
message: string;
owlCode?: string;
}
// interface Locals {}
// interface PageData {}
// interface PageState {}

View File

@@ -0,0 +1,45 @@
<script lang="ts">
import { goto } from '$app/navigation';
import BaseCard from '$lib/components/ui/cards/BaseCard.svelte';
import Textbox from '$lib/components/ui/Textbox.svelte';
import Button from '$lib/components/ui/Button.svelte';
let headcode = $state('');
function handleSearch(e: SubmitEvent) {
e.preventDefault();
if (!headcode.trim()) return;
const searchParams = new URLSearchParams();
searchParams.append('h', headcode.trim().toUpperCase());
goto(`/trains?${searchParams.toString()}`)
}
</script>
<BaseCard header={'Search Train & PIS'}>
<form onsubmit={handleSearch} class="card-content">
<Textbox
placeholder="Enter Headcode"
bind:value={headcode}
maxLength={4}
/>
<Button
type="submit"
disabled={!headcode}
>Search</Button>
</form>
</BaseCard>
<style>
.card-content {
text-align: center;
width: 90%;
margin: auto;
padding: 10px 0 10px 0;
display: flex;
flex-direction: column;
gap: 0.75rem;
}
</style>

View File

@@ -1,11 +1,13 @@
<script lang="ts">
import LocationBoardCard from '$lib/components/ui/cards/LocationBoardCard.svelte';
import HeadcodeSearchCard from '$lib/components/ui/cards/HeadcodeSearchCard.svelte';
import NearbyStationsCard from '$lib/components/ui/cards/NearbyStationsCard.svelte';
import QuickLinksCard from '$lib/components/ui/cards/QuickLinksCard.svelte';
</script>
<div class="card-container">
<LocationBoardCard />
<HeadcodeSearchCard />
<NearbyStationsCard />
<QuickLinksCard />
</div>

View File

@@ -0,0 +1,12 @@
<script lang="ts">
let { data } = $props();
</script>
<pre>{JSON.stringify(data.results, null, 1)}</pre>
<style>
pre {
background: #1e1e1e;
color: #00ff00;
padding: 1rem;
overflow: auto;
}
</style>

View File

@@ -0,0 +1,55 @@
import { OwlClient, ApiError, ValidationError } from '$lib/owlClient';
import type { ApiTrainsTrainByHeadcode } from '@owlboard/owlboard-ts';
import type { PageLoad } from './$types';
import { error } from '@sveltejs/kit';
export const load: PageLoad = async ({ url }) => {
const headcode = url.searchParams.get('h');
let dateParam = url.searchParams.get('d');
const toc = url.searchParams.get('t') || "";
const date: string | Date = (dateParam === "" || dateParam === null)
? new Date()
: dateParam;
if (!headcode) {
throw error(400, {
message: 'Headcode not provided',
owlCode: 'INVALID_DATA'
});
}
try {
const response = await OwlClient.trains.getByHeadcode(headcode, date, toc);
// Shouldn't be needed to cast the type...
const results = (response.data || []);
return {
title: headcode.toUpperCase(),
results: results,
}
} catch (e: unknown) {
if (e instanceof ValidationError) {
throw error(400, {
message: e.message,
owlCode: 'VALIDATION_ERROR',
});
} else if (e instanceof ApiError) {
console.log(e);
throw error(20, {
message: e.message,
owlCode: 'API_ERROR',
});
} else if (e instanceof Error) {
throw error(20, {
message: e.message,
owlCode: 'GEN_ERROR',
})
} else {
throw error(500, {
message: "Unexpected error",
owlCode: "UNKNOWN_ERR",
})
}
}
}