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

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);
if (data) {
return data[0].lateReason || "This train has been delayed"; 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 = "") { async function getCancel(code: string): Promise<string | undefined> {
const url = `${getApiUrl()}/api/v2/ref/reasonCode/${code}`; const data = await getReason(code);
const options = { if (data) {
method: "GET", return data[0].cancReason || "This train has been cancelled";
headers: { }
uuid: $uuid, }
},
}; async function getReason(code: string): Promise<ReasonCode[] | undefined> {
const res = await fetch(url, options); const apiString = `/api/v2/ref/reasonCode/${code}`;
return await res.json(); try {
const apiRes = (await apiGet(apiString)) as ReasonCode[];
return apiRes;
} catch (err) {
console.error("Unable to define reason code");
}
} }
</script> </script>