Add train headcode search
This commit is contained in:
11
package-lock.json
generated
11
package-lock.json
generated
@@ -8,12 +8,12 @@
|
|||||||
"name": "web-pwa",
|
"name": "web-pwa",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2348",
|
||||||
"@tabler/icons-svelte": "^3.40.0"
|
"@tabler/icons-svelte": "^3.40.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/compat": "^2.0.2",
|
"@eslint/compat": "^2.0.2",
|
||||||
"@eslint/js": "^9.39.2",
|
"@eslint/js": "^9.39.2",
|
||||||
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2231",
|
|
||||||
"@playwright/test": "^1.58.1",
|
"@playwright/test": "^1.58.1",
|
||||||
"@sveltejs/adapter-static": "^3.0.10",
|
"@sveltejs/adapter-static": "^3.0.10",
|
||||||
"@sveltejs/kit": "^2.50.2",
|
"@sveltejs/kit": "^2.50.2",
|
||||||
@@ -783,14 +783,12 @@
|
|||||||
"version": "3.0.3-alpha1",
|
"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",
|
"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==",
|
"integrity": "sha512-UWe2nbJWb2B/LuZW1UXHJ2lpqOGwugiXTa4G6X5xLiaws3ISEdciweorX8kr2/JAz5+iFYIe1xXRFAWsFtpn/w==",
|
||||||
"dev": true,
|
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/@owlboard/owlboard-ts": {
|
"node_modules/@owlboard/owlboard-ts": {
|
||||||
"version": "3.0.0-dev.20260427T2231",
|
"version": "3.0.0-dev.20260427T2348",
|
||||||
"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",
|
"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-EJ0gcPMmXDrWLg5ukO4Y5Li/XKOrcPhEK0awCWSQHvRNR1iFSCQlxIOooFjIOFNKvuYk3kLpZZ1WDl77yOUUKA==",
|
"integrity": "sha512-ZZ6h8zSnL/FyEZ6ZSekhK781/5TYMj6y6atRVwKiPH6TfXxauMdEimYCOKQIcoskseH7wItEZRFGAOUDAY3q7w==",
|
||||||
"dev": true,
|
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@owlboard/api-schema-types": "^3.0.3-alpha1",
|
"@owlboard/api-schema-types": "^3.0.3-alpha1",
|
||||||
@@ -2669,7 +2667,6 @@
|
|||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/latlon-geohash/-/latlon-geohash-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/latlon-geohash/-/latlon-geohash-2.0.0.tgz",
|
||||||
"integrity": "sha512-OKBswTwrvTdtenV+9C9euBmvgGuqyjJNAzpQCarRz1m8/pYD2nz9fKkXmLs2S3jeXaLi3Ry76twQplKKUlgS/g==",
|
"integrity": "sha512-OKBswTwrvTdtenV+9C9euBmvgGuqyjJNAzpQCarRz1m8/pYD2nz9fKkXmLs2S3jeXaLi3Ry76twQplKKUlgS/g==",
|
||||||
"dev": true,
|
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/levn": {
|
"node_modules/levn": {
|
||||||
|
|||||||
@@ -41,7 +41,7 @@
|
|||||||
"vitest-browser-svelte": "^2.0.2"
|
"vitest-browser-svelte": "^2.0.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tabler/icons-svelte": "^3.40.0",
|
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2348",
|
||||||
"@owlboard/owlboard-ts": "^3.0.0-dev.20260427T2231"
|
"@tabler/icons-svelte": "^3.40.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
5
src/app.d.ts
vendored
5
src/app.d.ts
vendored
@@ -2,7 +2,10 @@
|
|||||||
// for information about these interfaces
|
// for information about these interfaces
|
||||||
declare global {
|
declare global {
|
||||||
namespace App {
|
namespace App {
|
||||||
// interface Error {}
|
interface Error {
|
||||||
|
message: string;
|
||||||
|
owlCode?: string;
|
||||||
|
}
|
||||||
// interface Locals {}
|
// interface Locals {}
|
||||||
// interface PageData {}
|
// interface PageData {}
|
||||||
// interface PageState {}
|
// interface PageState {}
|
||||||
|
|||||||
45
src/lib/components/ui/cards/HeadcodeSearchCard.svelte
Normal file
45
src/lib/components/ui/cards/HeadcodeSearchCard.svelte
Normal 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>
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import LocationBoardCard from '$lib/components/ui/cards/LocationBoardCard.svelte';
|
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 NearbyStationsCard from '$lib/components/ui/cards/NearbyStationsCard.svelte';
|
||||||
import QuickLinksCard from '$lib/components/ui/cards/QuickLinksCard.svelte';
|
import QuickLinksCard from '$lib/components/ui/cards/QuickLinksCard.svelte';
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="card-container">
|
<div class="card-container">
|
||||||
<LocationBoardCard />
|
<LocationBoardCard />
|
||||||
|
<HeadcodeSearchCard />
|
||||||
<NearbyStationsCard />
|
<NearbyStationsCard />
|
||||||
<QuickLinksCard />
|
<QuickLinksCard />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
12
src/routes/trains/+page.svelte
Normal file
12
src/routes/trains/+page.svelte
Normal 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>
|
||||||
55
src/routes/trains/+page.ts
Normal file
55
src/routes/trains/+page.ts
Normal 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",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user