2024-04-17 12:20:29 +01:00
|
|
|
<script lang="ts">
|
2024-04-30 11:17:06 +01:00
|
|
|
import Header from "$lib/navigation/header.svelte";
|
|
|
|
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
|
|
|
|
2024-04-30 11:17:06 +01:00
|
|
|
import { onMount } from "svelte";
|
|
|
|
import TrainDetail from "$lib/train/train-detail.svelte";
|
|
|
|
import { getApiUrl } from "$lib/scripts/upstream";
|
2024-06-13 21:59:01 +01:00
|
|
|
import toast from "svelte-french-toast";
|
2024-07-12 12:20:36 +01:00
|
|
|
import TimeBar from "$lib/navigation/TimeBar.svelte";
|
2024-11-13 14:35:19 +00:00
|
|
|
import { IconArrowLeft, IconArrowRight, IconCheck } from "@tabler/icons-svelte";
|
2024-11-15 11:34:02 +00:00
|
|
|
import Error from "../+error.svelte";
|
2023-06-13 13:38:59 +01:00
|
|
|
|
2024-04-30 11:17:06 +01:00
|
|
|
let title = "Timetable Results";
|
|
|
|
let id = "";
|
|
|
|
let data = [];
|
|
|
|
let error = false;
|
|
|
|
let errMsg = "";
|
2023-06-20 19:22:59 +01:00
|
|
|
|
2024-11-15 11:34:02 +00:00
|
|
|
let formattedDate = new Date().toISOString().split('T')[0];
|
2024-11-13 14:35:19 +00:00
|
|
|
|
2024-04-30 11:17:06 +01:00
|
|
|
$: {
|
|
|
|
if (id) {
|
|
|
|
title = id.toUpperCase();
|
|
|
|
} else {
|
|
|
|
title = "Querying Timetable";
|
|
|
|
}
|
2023-06-13 13:38:59 +01:00
|
|
|
}
|
|
|
|
|
2024-04-30 11:17:06 +01:00
|
|
|
async function getHeadcode() {
|
|
|
|
return new URLSearchParams(window.location.search).get("headcode");
|
2023-07-07 11:27:28 +01:00
|
|
|
}
|
2023-06-19 21:55:23 +01:00
|
|
|
|
2024-11-13 14:35:19 +00:00
|
|
|
function incrementDate() {
|
2024-11-15 11:34:02 +00:00
|
|
|
let dateInput = new Date(formattedDate)
|
2024-11-13 14:35:19 +00:00
|
|
|
dateInput.setDate(dateInput.getDate() + 1);
|
2024-11-15 11:34:02 +00:00
|
|
|
formattedDate = dateInput.toISOString().split('T')[0];
|
2024-11-13 14:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function decrementDate() {
|
2024-11-15 11:34:02 +00:00
|
|
|
let dateInput = new Date(formattedDate)
|
2024-11-13 14:35:19 +00:00
|
|
|
dateInput.setDate(dateInput.getDate() - 1);
|
2024-11-15 11:34:02 +00:00
|
|
|
formattedDate = dateInput.toISOString().split('T')[0];
|
2024-11-13 14:35:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-30 11:17:06 +01:00
|
|
|
onMount(async () => {
|
|
|
|
id = (await getHeadcode()) || "";
|
2024-11-13 14:35:19 +00:00
|
|
|
load();
|
2024-06-13 21:59:01 +01:00
|
|
|
|
2025-01-22 00:10:30 +00:00
|
|
|
if ($uuid == null || $uuid == "") {
|
|
|
|
toast("PIS Codes will soon be hidden for unregistered users. Register in the menu.", {
|
2024-06-23 11:21:45 +01:00
|
|
|
duration: 3000,
|
|
|
|
});
|
2025-01-22 00:10:30 +00:00
|
|
|
}
|
2024-04-30 11:17:06 +01:00
|
|
|
});
|
|
|
|
|
2024-11-13 14:35:19 +00:00
|
|
|
function load() {
|
2024-11-15 11:34:02 +00:00
|
|
|
error = false;
|
|
|
|
const selectedDate = new Date(formattedDate);
|
|
|
|
const currentDate = new Date();
|
|
|
|
const difference: number = currentDate.getTime() - selectedDate.getTime();
|
|
|
|
const differenceDays: number = difference / (1000 * 60 * 60 * 24)
|
|
|
|
if (differenceDays > 7) {
|
|
|
|
toast.error("Timetable data is not available for dates older than a week")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-13 14:35:19 +00:00
|
|
|
toast.promise(
|
|
|
|
fetchData(id),
|
|
|
|
{
|
|
|
|
loading: 'Searching Timetable',
|
|
|
|
success: 'Done',
|
2025-01-22 00:10:30 +00:00
|
|
|
error: 'No Services Found'
|
2024-11-13 14:35:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-30 11:17:06 +01:00
|
|
|
async function fetchData(id = "") {
|
2024-11-13 14:35:19 +00:00
|
|
|
data = [];
|
2024-04-30 11:17:06 +01:00
|
|
|
const searchType = "headcode";
|
|
|
|
const options = {
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
2024-04-30 11:18:21 +01:00
|
|
|
uuid: $uuid,
|
|
|
|
},
|
2024-04-30 11:17:06 +01:00
|
|
|
};
|
2024-11-13 14:35:19 +00:00
|
|
|
const url = `${getApiUrl()}/api/v2/timetable/train/${formattedDate}/${searchType}/${id}`;
|
2024-04-30 11:17:06 +01:00
|
|
|
try {
|
|
|
|
const res = await fetch(url, options);
|
|
|
|
if (res.status == 200) {
|
2024-11-13 14:35:19 +00:00
|
|
|
let services = await res.json();
|
|
|
|
if (!services.length) {
|
|
|
|
error = true;
|
|
|
|
errMsg = "No services found";
|
|
|
|
return Promise.reject(new Error(errMsg));
|
|
|
|
}
|
|
|
|
data = services
|
2024-04-30 11:17:06 +01:00
|
|
|
} else if (res.status === 401) {
|
|
|
|
error = true;
|
|
|
|
errMsg = "You must be logged into the staff version for this feature";
|
2024-11-13 14:35:19 +00:00
|
|
|
return Promise.reject(new Error(errMsg));
|
2024-04-30 11:17:06 +01:00
|
|
|
} else {
|
|
|
|
error = true;
|
|
|
|
errMsg = "Unable to connect, check your connection and try again";
|
2024-11-13 14:35:19 +00:00
|
|
|
return Promise.reject(new Error(errMsg));
|
2024-04-30 11:17:06 +01:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
error = true;
|
|
|
|
errMsg = "Connection error, try again later";
|
2024-11-13 14:35:19 +00:00
|
|
|
return Promise.reject(new Error(errMsg));
|
2024-04-30 11:17:06 +01:00
|
|
|
}
|
2023-07-08 21:54:33 +01:00
|
|
|
}
|
2023-06-19 21:55:23 +01:00
|
|
|
</script>
|
2023-06-13 13:38:59 +01:00
|
|
|
|
2023-06-19 21:55:23 +01:00
|
|
|
<Header {title} />
|
2024-07-12 12:20:36 +01:00
|
|
|
<TimeBar updatedTime={undefined} />
|
2024-11-13 14:35:19 +00:00
|
|
|
<div id="dateSelector">
|
|
|
|
<button on:click={decrementDate}><IconArrowLeft /></button>
|
|
|
|
<input
|
|
|
|
type="date"
|
|
|
|
id="dateInput"
|
2024-11-15 11:34:02 +00:00
|
|
|
bind:value={formattedDate}
|
2024-11-13 14:35:19 +00:00
|
|
|
/>
|
|
|
|
<button on:click={incrementDate}><IconArrowRight /></button>
|
|
|
|
<button on:click={load}><IconCheck /></button>
|
|
|
|
</div>
|
2023-06-27 12:38:41 +01:00
|
|
|
{#if error}
|
2024-04-30 11:17:06 +01:00
|
|
|
<Island>
|
|
|
|
<p style="font-weight:600">{errMsg}</p>
|
|
|
|
</Island>
|
2023-06-27 12:38:41 +01:00
|
|
|
{/if}
|
|
|
|
|
2023-06-19 21:55:23 +01:00
|
|
|
{#each data as service}
|
2024-04-30 11:17:06 +01:00
|
|
|
{#if service}
|
2024-11-15 11:34:02 +00:00
|
|
|
<TrainDetail {service} date={new Date(formattedDate)} />
|
2024-04-30 11:17:06 +01:00
|
|
|
{/if}
|
2023-06-19 21:55:23 +01:00
|
|
|
{/each}
|
2023-07-07 11:27:28 +01:00
|
|
|
|
2023-06-20 19:22:59 +01:00
|
|
|
<Nav />
|
|
|
|
|
|
|
|
<style>
|
2024-04-30 11:17:06 +01:00
|
|
|
p {
|
|
|
|
color: white;
|
|
|
|
font-size: 18px;
|
|
|
|
font-weight: 600;
|
|
|
|
}
|
2024-11-13 14:35:19 +00:00
|
|
|
|
|
|
|
#dateInput {
|
|
|
|
height: 25px;
|
2024-11-15 11:35:48 +00:00
|
|
|
transform: translateY(-8px) translateX(20px);
|
2024-11-13 14:35:19 +00:00
|
|
|
}
|
|
|
|
button {
|
|
|
|
background: none;
|
|
|
|
border: none;
|
|
|
|
color: white;
|
2024-11-15 11:34:02 +00:00
|
|
|
transform: translateX(20px);
|
2024-11-13 14:35:19 +00:00
|
|
|
}
|
2023-07-07 11:27:28 +01:00
|
|
|
</style>
|