feat: add experimental ui for mobile card assignment
This commit is contained in:
72
src/components/general/CardAssignment.svelte
Normal file
72
src/components/general/CardAssignment.svelte
Normal file
@@ -0,0 +1,72 @@
|
||||
<script>
|
||||
import QrCodeScanner from "./QrCodeScanner.svelte";
|
||||
let state = "scan_runner";
|
||||
let runnerID = undefined;
|
||||
let cardInfo = "";
|
||||
</script>
|
||||
|
||||
<div class="p-4">
|
||||
<h3 class="text-3xl font-bold">Card Assignment for Mobile</h3>
|
||||
{#if state === "done"}
|
||||
<p>Assigned Card {cardInfo} ✅</p>
|
||||
<p>(not really, needs to be implemented)</p>
|
||||
{:else}
|
||||
<!-- -->
|
||||
{#if state === "scan_runner"}
|
||||
<h3 class="text-xl font-bold">Scan Runner (Selfservice QR)</h3>
|
||||
{/if}
|
||||
{#if state === "scan_card"}
|
||||
<h3 class="text-xl font-bold">Runner Scanned</h3>
|
||||
<p>{runnerID}</p>
|
||||
<h3 class="text-xl font-bold">Scan Card (Code 128 Barcode)</h3>
|
||||
{/if}
|
||||
<QrCodeScanner
|
||||
on:detect={(e) => {
|
||||
console.log({ type: "DETECT", code: e.detail.decodedText });
|
||||
if (state === "scan_runner") {
|
||||
if (
|
||||
e.detail.decodedText.includes(
|
||||
"https://portal.lauf-fuer-kaya.de/profile/"
|
||||
)
|
||||
) {
|
||||
runnerID = JSON.parse(
|
||||
atob(
|
||||
e.detail.decodedText
|
||||
.replace("https://portal.lauf-fuer-kaya.de/profile/", "")
|
||||
.split(".")[1]
|
||||
)
|
||||
).id;
|
||||
state = "scan_card";
|
||||
}
|
||||
}
|
||||
if (state === "scan_card") {
|
||||
if (
|
||||
!e.detail.decodedText.includes(
|
||||
"https://portal.lauf-fuer-kaya.de/profile/"
|
||||
)
|
||||
) {
|
||||
cardInfo = e.detail.decodedText;
|
||||
state = "done";
|
||||
}
|
||||
}
|
||||
}}
|
||||
width={320}
|
||||
height={320}
|
||||
class="w-full max-w-sm bg-neutral-300 rounded-lg overflow-hidden"
|
||||
/>
|
||||
{#if state === "scan_card"}
|
||||
<button
|
||||
on:click={() => {
|
||||
state = "scan_runner";
|
||||
runnerID = undefined;
|
||||
cardInfo = "";
|
||||
}}
|
||||
type="button"
|
||||
class="py-3 px-4 inline-flex items-center gap-x-2 text-sm font-medium rounded-lg border border-transparent bg-red-100 text-red-800 hover:bg-red-200 focus:outline-hidden focus:bg-red-200 disabled:opacity-50 disabled:pointer-events-none dark:text-red-500 dark:bg-red-800/30 dark:hover:bg-red-800/20 dark:focus:bg-red-800/20 w-full mt-2"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
{/if}
|
||||
<!-- -->
|
||||
{/if}
|
||||
</div>
|
||||
80
src/components/general/QrCodeScanner.svelte
Normal file
80
src/components/general/QrCodeScanner.svelte
Normal file
@@ -0,0 +1,80 @@
|
||||
<script>
|
||||
import { onMount, createEventDispatcher } from "svelte";
|
||||
import {
|
||||
Html5QrcodeScanner,
|
||||
Html5QrcodeScanType,
|
||||
Html5QrcodeSupportedFormats,
|
||||
Html5QrcodeScannerState,
|
||||
} from "html5-qrcode";
|
||||
|
||||
export let width;
|
||||
export let height;
|
||||
export let paused = false;
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
function onScanSuccess(decodedText, decodedResult) {
|
||||
dispatch("detect", { decodedText });
|
||||
}
|
||||
|
||||
// usually better to ignore and keep scanning
|
||||
function onScanFailure(message) {
|
||||
dispatch("error", { message });
|
||||
}
|
||||
|
||||
let scanner;
|
||||
onMount(() => {
|
||||
scanner = new Html5QrcodeScanner(
|
||||
"qr-scanner",
|
||||
{
|
||||
fps: 10,
|
||||
rememberLastUsedCamera: true,
|
||||
qrbox: { width, height },
|
||||
aspectRatio: 1,
|
||||
supportedScanTypes: [Html5QrcodeScanType.SCAN_TYPE_CAMERA],
|
||||
formatsToSupport: [
|
||||
Html5QrcodeSupportedFormats.CODE_39,
|
||||
Html5QrcodeSupportedFormats.EAN_8,
|
||||
Html5QrcodeSupportedFormats.EAN_13,
|
||||
Html5QrcodeSupportedFormats.QR_CODE,
|
||||
Html5QrcodeSupportedFormats.CODE_128,
|
||||
],
|
||||
},
|
||||
false // non-verbose
|
||||
);
|
||||
scanner.render(onScanSuccess, onScanFailure);
|
||||
});
|
||||
|
||||
// pause/resume scanner to avoid unintended scans
|
||||
$: togglePause(paused);
|
||||
function togglePause(paused) {
|
||||
if (paused && scanner?.getState() === Html5QrcodeScannerState.SCANNING) {
|
||||
scanner?.pause();
|
||||
} else if (scanner?.getState() === Html5QrcodeScannerState.PAUSED) {
|
||||
scanner?.resume();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div id="qr-scanner" class={$$props.class} />
|
||||
|
||||
<style>
|
||||
/* Hide unwanted icons */
|
||||
#qr-scanner :global(img[alt="Info icon"]),
|
||||
#qr-scanner :global(img[alt="Camera based scan"]) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Change camera permission button text */
|
||||
#qr-scanner :global(#html5-qrcode-button-camera-permission) {
|
||||
visibility: hidden;
|
||||
}
|
||||
#qr-scanner :global(#html5-qrcode-button-camera-permission::after) {
|
||||
position: absolute;
|
||||
inset: auto 0 0;
|
||||
display: block;
|
||||
content: "Allow camera access";
|
||||
visibility: visible;
|
||||
padding: 10px 0;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user