scanclient/src/Scanner.svelte

165 lines
5.7 KiB
Svelte
Raw Normal View History

2021-03-16 13:19:53 +00:00
<script>
2021-03-17 17:44:40 +00:00
import axios from "axios";
2021-03-18 17:40:36 +00:00
import { _ } from "svelte-i18n";
import { apikey, api_endpoint, page, stationinfo } from "./store.js";
2021-03-16 13:19:53 +00:00
function init(el) {
el.focus();
}
2021-03-17 17:52:37 +00:00
let lastscan_error = "";
2021-03-16 13:19:53 +00:00
let lastscan_time = "";
let lastscan_laptime = "";
let lastscan_totaldistance = "";
2021-04-06 16:00:05 +00:00
let lastscan_valid = true;
2021-03-16 13:19:53 +00:00
let card = "";
// live clock at the top
let time = new Date();
$: hours = (time.getHours() + "").padStart(2, "0");
$: minutes = (time.getMinutes() + "").padStart(2, "0");
$: seconds = (time.getSeconds() + "").padStart(2, "0");
2021-03-17 17:52:37 +00:00
setInterval(() => {
2021-03-16 13:19:53 +00:00
time = new Date();
}, 1000);
</script>
2021-03-17 18:59:07 +00:00
<div class="min-h-screen">
2021-03-18 17:15:54 +00:00
<div class="bg-white shadow p-2">
<div class="flex flex-wrap -mx-1 overflow-hidden">
<div class="my-1 px-1 w-1/3 overflow-hidden text-center self-center">
<img src="/favicon.png" alt="" class="h-14 mx-auto" />
</div>
2021-03-17 17:44:40 +00:00
2021-03-18 17:15:54 +00:00
<div class="my-1 px-1 w-1/3 overflow-hidden text-center self-center">
Lauf Für Kaya! Scan 📷
</div>
2021-03-17 17:44:40 +00:00
2021-03-18 17:15:54 +00:00
<div class="my-1 px-1 w-1/3 overflow-hidden text-center self-center">
{JSON.parse($stationinfo).track.name} - #{JSON.parse($stationinfo).track
.id} - {JSON.parse($stationinfo).track.distance}m
</div>
2021-03-17 17:44:40 +00:00
</div>
</div>
2021-03-17 17:52:37 +00:00
<h1 class="mr-6 text-7xl font-semibold text-center text-gray-900">
2021-03-16 13:19:53 +00:00
{hours}:{minutes}:{seconds}
</h1>
<section class="px-4 py-24 mx-auto max-w-7xl">
<div class="mx-auto space-y-5 w-full md:w-1/2">
2021-03-17 17:52:37 +00:00
{#if lastscan_error}
<div
class="text-white px-6 py-4 border-0 rounded relative bg-red-500 mt-2"
>
<span class="inline-block align-middle">
<b class="capitalize">Error!</b><br />{lastscan_error}
</span>
</div>
{/if}
2021-03-16 13:19:53 +00:00
<form
class="space-y-4"
onsubmit="event.preventDefault();"
on:submit={() => {
if (card === "cnf") {
page.set("settings");
} else {
2021-03-17 17:55:50 +00:00
card = parseInt(card);
2021-03-17 17:52:37 +00:00
lastscan_error = "";
2021-03-17 17:44:40 +00:00
axios
.request({
2021-03-17 17:55:50 +00:00
method: "POST",
url: $api_endpoint + "api/scans/trackscans",
2021-03-17 17:44:40 +00:00
headers: { Authorization: "Bearer " + $apikey },
2021-03-17 18:59:07 +00:00
data: { card },
2021-03-17 17:44:40 +00:00
})
.then((response) => {
const time = new Date();
2021-03-17 17:44:40 +00:00
const hours = (time.getHours() + "").padStart(2, "0");
const minutes = (time.getMinutes() + "").padStart(2, "0");
const seconds = (time.getSeconds() + "").padStart(2, "0");
lastscan_time = hours + ":" + minutes + ":" + seconds;
2021-03-18 17:15:54 +00:00
response.data.lapTime =
Math.floor(response.data.lapTime / 60) +
"min " +
(Math.floor(response.data.lapTime % 60) + "").padStart(
2,
"0"
) +
"s";
2021-03-17 18:59:07 +00:00
lastscan_laptime = response.data.lapTime;
2021-04-06 16:00:05 +00:00
lastscan_valid = response.data.valid;
2021-03-18 17:15:54 +00:00
lastscan_totaldistance =
Math.floor(response.data.runner.distance / 1000) +
"km " +
(
Math.floor(response.data.runner.distance % 1000) + ""
).padStart(3, "0") +
"m";
2021-03-17 17:44:40 +00:00
})
.catch((e) => {
2021-03-17 17:52:37 +00:00
lastscan_error = e.response.data.message;
2021-03-17 17:44:40 +00:00
});
2021-03-16 13:19:53 +00:00
}
card = "";
}}
>
<label class="block">
2021-03-16 13:19:53 +00:00
<span class="block mb-1 text-xs font-medium text-gray-700"
2021-03-18 17:43:37 +00:00
>{$_("runner_card")}</span
2021-03-16 13:19:53 +00:00
>
<input
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mt-1 leading-tight focus:outline-none focus:shadow-outline"
type="text"
placeholder="123456789"
required
use:init
bind:value={card}
/>
</label>
2021-03-17 17:44:40 +00:00
{#if lastscan_totaldistance}
{#if !lastscan_valid || lastscan_error}
2021-04-06 16:00:31 +00:00
<svg
xmlns="http://www.w3.org/2000/svg"
width="20rem"
height="20rem"
class="ml-auto mr-auto"
version="1.0"
2021-04-06 16:07:13 +00:00
title="Invalid"
2021-04-06 16:00:31 +00:00
viewBox="0 0 100 100"
><g fill="none" stroke="red"
><path d="M6 6l88 88" stroke-width="18.1" /><path
d="M6 94L94 6"
stroke-width="17.8"
/></g
></svg
>
2021-04-06 16:00:05 +00:00
{:else}
2021-04-06 16:00:31 +00:00
<svg
xmlns="http://www.w3.org/2000/svg"
width="20rem"
height="20rem"
class="ml-auto mr-auto"
2021-04-06 16:07:13 +00:00
title="Valid"
2021-04-06 16:00:31 +00:00
viewBox="0 0 600 600"
><path
d="M8 405s115 129 138 182h99c41-126 203-429 341-535 28-37-43-52-102-27-87 36-252 317-283 384-44 12-90-74-90-74z"
fill="#181"
/></svg
>
2021-04-06 16:00:05 +00:00
{/if}
2021-04-06 16:07:13 +00:00
<h1 class="text-2xl font-bold text-center">{$_("total-distance")}</h1>
<h1 class="text-6xl font-bold text-center">
2021-03-17 17:44:40 +00:00
{lastscan_totaldistance}
</h1>
2021-04-06 16:07:13 +00:00
<h1 class="text-2xl font-bold text-center">{$_("lap-time")}</h1>
<h1 class="text-6xl font-bold text-center">{lastscan_laptime}</h1>
2021-04-06 16:07:13 +00:00
<h1 class="text-2xl font-bold text-center">{$_("last-scan")}</h1>
2021-04-06 16:00:05 +00:00
<h1 class="text-5xl font-bold text-center">{lastscan_time}</h1>
2021-03-17 17:44:40 +00:00
{:else}
2021-03-18 17:43:37 +00:00
<h1 class="text-3xl font-bold text-center">
{$_("please_scan_a_card")}
</h1>
2021-03-17 17:44:40 +00:00
{/if}
2021-03-18 17:43:37 +00:00
<button type="submit" class="hidden">{$_("scan")}</button>
2021-03-16 13:19:53 +00:00
</form>
</div>
</section>
</div>