working prototype
This commit is contained in:
198
src/app/page.tsx
198
src/app/page.tsx
@@ -1,103 +1,111 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useRef } from "react";
|
||||
import Image from "next/image";
|
||||
import Scanner from "@/components/Scanner";
|
||||
import {
|
||||
useMutation,
|
||||
QueryClient,
|
||||
QueryClientProvider,
|
||||
} from "@tanstack/react-query";
|
||||
|
||||
export default function Home() {
|
||||
type SessionResponse = {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
nnin: string;
|
||||
documentPhoto: string;
|
||||
age: number;
|
||||
phoneNumber: string;
|
||||
};
|
||||
|
||||
function SessionInfo({ session }: { session: SessionResponse }) {
|
||||
return (
|
||||
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
|
||||
<main className="flex flex-col gap-[32px] row-start-2 items-center sm:items-start">
|
||||
<div className="max-w-md mx-auto mt-6 p-6 rounded-xl shadow-lg border border-gray-200 bg-gradient-to-br from-gray-50 to-gray-200 space-y-4">
|
||||
<div className="flex flex-col items-center">
|
||||
<Image
|
||||
className="dark:invert"
|
||||
src="/next.svg"
|
||||
alt="Next.js logo"
|
||||
width={180}
|
||||
height={38}
|
||||
priority
|
||||
src={`data:image/jpeg;base64,${session.documentPhoto}`}
|
||||
alt="Document"
|
||||
width={200}
|
||||
height={261}
|
||||
className="w-40 h-50 object-cover rounded-lg mb-4 border border-gray-300 shadow"
|
||||
unoptimized
|
||||
/>
|
||||
<ol className="list-inside list-decimal text-sm/6 text-center sm:text-left font-[family-name:var(--font-geist-mono)]">
|
||||
<li className="mb-2 tracking-[-.01em]">
|
||||
Get started by editing{" "}
|
||||
<code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-[family-name:var(--font-geist-mono)] font-semibold">
|
||||
src/app/page.tsx
|
||||
</code>
|
||||
.
|
||||
</li>
|
||||
<li className="tracking-[-.01em]">
|
||||
Save and see your changes instantly.
|
||||
</li>
|
||||
</ol>
|
||||
|
||||
<div className="flex gap-4 items-center flex-col sm:flex-row">
|
||||
<a
|
||||
className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:w-auto"
|
||||
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Image
|
||||
className="dark:invert"
|
||||
src="/vercel.svg"
|
||||
alt="Vercel logomark"
|
||||
width={20}
|
||||
height={20}
|
||||
/>
|
||||
Deploy now
|
||||
</a>
|
||||
<a
|
||||
className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 w-full sm:w-auto md:w-[158px]"
|
||||
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Read our docs
|
||||
</a>
|
||||
</div>
|
||||
</main>
|
||||
<footer className="row-start-3 flex gap-[24px] flex-wrap items-center justify-center">
|
||||
<a
|
||||
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
|
||||
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Image
|
||||
aria-hidden
|
||||
src="/file.svg"
|
||||
alt="File icon"
|
||||
width={16}
|
||||
height={16}
|
||||
/>
|
||||
Learn
|
||||
</a>
|
||||
<a
|
||||
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
|
||||
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Image
|
||||
aria-hidden
|
||||
src="/window.svg"
|
||||
alt="Window icon"
|
||||
width={16}
|
||||
height={16}
|
||||
/>
|
||||
Examples
|
||||
</a>
|
||||
<a
|
||||
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
|
||||
href="https://nextjs.org?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Image
|
||||
aria-hidden
|
||||
src="/globe.svg"
|
||||
alt="Globe icon"
|
||||
width={16}
|
||||
height={16}
|
||||
/>
|
||||
Go to nextjs.org →
|
||||
</a>
|
||||
</footer>
|
||||
{(session.firstName || session.lastName) && (
|
||||
<div className="text-xl font-bold text-gray-800">
|
||||
{[session.firstName, session.lastName].filter(Boolean).join(" ")}
|
||||
</div>
|
||||
)}
|
||||
{typeof session.age === "number" && (
|
||||
<div className="text-3xl font-extrabold text-blue-700 mt-2">
|
||||
{session.age} år
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
async function lookupSession(sessionId: string) {
|
||||
const res = await fetch("/api/lookup", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sessionId }),
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
function HomeInner() {
|
||||
const [session, setSession] = useState<SessionResponse | null>(null);
|
||||
const [scannedCodes, setScannedCodes] = useState<Set<string>>(new Set());
|
||||
const pendingCodeRef = useRef<string | null>(null);
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: lookupSession,
|
||||
onSuccess: (data, variables) => {
|
||||
console.log("Got lookup result:", data);
|
||||
if (data && typeof data.age === "number") {
|
||||
setSession(data);
|
||||
setScannedCodes((prev) => new Set(prev).add(variables));
|
||||
}
|
||||
pendingCodeRef.current = null;
|
||||
},
|
||||
onError: () => {
|
||||
pendingCodeRef.current = null;
|
||||
},
|
||||
});
|
||||
|
||||
const handleScan = (decodedText: string) => {
|
||||
if (!decodedText.startsWith("VisLeg-")) {
|
||||
window.alert("Ugyldig QR-kode");
|
||||
return;
|
||||
}
|
||||
if (
|
||||
scannedCodes.has(decodedText) ||
|
||||
mutation.isPending ||
|
||||
pendingCodeRef.current === decodedText
|
||||
) {
|
||||
// Already scanned or lookup in progress for this code
|
||||
return;
|
||||
}
|
||||
pendingCodeRef.current = decodedText;
|
||||
mutation.mutate(decodedText);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Scanner onScan={handleScan} />
|
||||
{session && <SessionInfo session={session} />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Provide react-query context
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<HomeInner />
|
||||
</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user