Update reason fetcher to use apiGet function

This commit is contained in:
Fred Boniface 2024-07-03 11:01:05 +01:00
parent 5a9e55c695
commit f81acf348a
1 changed files with 21 additions and 21 deletions

View File

@ -1,32 +1,32 @@
<script> <script lang="ts">
import { getApiUrl } from "$lib/scripts/upstream"; import { apiGet } from "$lib/scripts/apiFetch";
import { uuid } from "$lib/stores/uuid"; import { ReasonCode } from "@owlboard/ts-types";
export let code = ""; export let code: string;
export let type = ""; export let type: string;
async function getDelay(code = "") { async function getDelay(code: string): Promise<string | undefined> {
console.log(`Fetching delay reason ${code}`);
const data = await getReason(code); const data = await getReason(code);
return data[0].lateReason || "This train has been delayed"; if (data) {
return data[0].lateReason || "This train has been delayed";
}
} }
async function getCancel(code = "") { async function getCancel(code: string): Promise<string | undefined> {
console.log(`Fetching cancel reason ${code}`);
const data = await getReason(code); const data = await getReason(code);
return data[0].cancReason || "This train has been cancelled"; if (data) {
return data[0].cancReason || "This train has been cancelled";
}
} }
async function getReason(code = "") { async function getReason(code: string): Promise<ReasonCode[] | undefined> {
const url = `${getApiUrl()}/api/v2/ref/reasonCode/${code}`; const apiString = `/api/v2/ref/reasonCode/${code}`;
const options = { try {
method: "GET", const apiRes = (await apiGet(apiString)) as ReasonCode[];
headers: { return apiRes;
uuid: $uuid, } catch (err) {
}, console.error("Unable to define reason code");
}; }
const res = await fetch(url, options);
return await res.json();
} }
</script> </script>