Modulisation and additions to StaffLDB

This commit is contained in:
Fred Boniface
2023-07-07 21:59:06 +01:00
parent cb8aff5788
commit 5a6fe0f3f5
7 changed files with 505 additions and 180 deletions

View File

@@ -0,0 +1,50 @@
<script>
import { uuid } from "$lib/stores/uuid";
export let code = '';
export let type = '';
async function getDelay(code = '') {
console.log(`Fetching delay reason ${code}`)
const data = await getReason(code);
return data[0].lateReason || 'This train has been delayed';
}
async function getCancel(code = '') {
console.log(`Fetching cancel reason ${code}`)
const data = await getReason(code);
return data[0].cancReason || 'This train has been cancelled';
}
async function getReason(code = '') {
const url = `https://owlboard.info/api/v2/ref/reasonCode/${code}`;
const options = {
method: 'GET',
headers: {
uuid: $uuid
}
};
const res = await fetch(url, options);
return await res.json();
}
</script>
{#if type === "cancel"}
{#await getCancel(code)}
This train has been cancelled
{:then reason}
{reason}
{:catch}
This train has been cancelled
{/await}
{:else if type === "delay"}
{#await getDelay(code)}
This train has been delayed
{:then reason}
{reason}
{:catch}
This train has been delayed
{/await}
{/if}