99 lines
2.2 KiB
Svelte
Raw Normal View History

2023-06-13 13:38:59 +01:00
<script>
import Header from '$lib/navigation/header.svelte'
import Loading from '$lib/navigation/loading.svelte';
2023-06-27 12:38:41 +01:00
import Island from '$lib/islands/island.svelte';
import Nav from '$lib/navigation/nav.svelte';
import { uuid } from '$lib/stores/uuid';
2023-06-13 13:38:59 +01:00
import { onMount } from 'svelte'
2023-06-30 11:08:59 +01:00
import TrainDetail from '$lib/train/train-detail.svelte';
2023-06-13 13:38:59 +01:00
let title = "Timetable Results"
let id = ""
let data = [];
let isLoading = true;
2023-06-27 12:38:41 +01:00
let error = false;
let errMsg = "";
2023-06-13 13:38:59 +01:00
2023-06-20 19:22:59 +01:00
$: {
if (id) {
title = id.toUpperCase();
} else {
title = "Querying Timetable"
}
}
2023-06-13 13:38:59 +01:00
async function getHeadcode() {
return new URLSearchParams(window.location.search).get('headcode');
}
onMount(async () => {
isLoading = true;
id = await getHeadcode() || "";
2023-06-27 12:38:41 +01:00
const res = await fetchData(id);
if (res) {
data = res;
2023-06-28 12:43:32 +01:00
if (!data.length) {
error = true;
errMsg = "No services found"
}
2023-06-27 12:38:41 +01:00
}
isLoading = false;
2023-06-13 13:38:59 +01:00
})
async function fetchData(id = "") {
const date = 'now';
const searchType = 'headcode'
const options = {
method: "GET",
headers: {
"uuid": $uuid
}
}
const url = `https://owlboard.info/api/v2/timetable/train/${date}/${searchType}/${id}`
const res = await fetch(url, options);
2023-06-27 12:38:41 +01:00
isLoading = false;
if (res.status == 200) {
return await res.json();
} else if (res.status === 401) {
error = true;
errMsg = "You must be logged into the staff version for this feature"
return false;
} else {
error = true;
errMsg = "Unable to connect, check your connection and try again"
return false;
}
}
</script>
2023-06-13 13:38:59 +01:00
<Header {title} />
2023-06-30 11:08:59 +01:00
<div id="whitespace"></div>
2023-06-27 12:38:41 +01:00
{#if error}
<Island>
<p class="error">{errMsg}</p>
</Island>
{/if}
{#if isLoading}
<Loading />
{/if}
2023-06-13 13:38:59 +01:00
{#each data as service}
2023-06-30 11:08:59 +01:00
<TrainDetail {service} />
{/each}
2023-06-13 13:38:59 +01:00
2023-06-20 19:22:59 +01:00
<Nav />
<style>
2023-06-30 11:08:59 +01:00
#whitespace {
height: 15px;
2023-06-20 19:22:59 +01:00
}
p {
2023-06-20 19:24:17 +01:00
color: white;
font-size: 18px;
2023-06-20 19:22:59 +01:00
font-weight: 600;
}
</style>