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