import { Show, createEffect, createSignal } from "solid-js"; import { JSX } from "solid-js/jsx-runtime"; import QR from "qrcode"; import { CopyIcon } from "../icons/CopyIcon"; import { XIcon } from "../icons/XIcon"; import { Person } from "../types/Person"; type HTMLButtonEvent = JSX.EventHandlerUnion; /* Form that retrieves a user from the DB given an ID */ export function Search(props: {setPerson: (p: Person | null) => void}) { const [dni, setDni] = createSignal(""); const [loading, setLoading] = createSignal(false); const [error, setError] = createSignal(""); const [qrBase64, setQrBase64] = createSignal(null); const [person, setPerson] = createSignal(null); // Update QR and automatically search when DNI is changed createEffect(() => { const dniT = dni(); if (dniT.length >= 8) { search(); QR.toDataURL(`https://eegsac.com/certificado/${dniT}`, {margin: 1}, (err, res) => { if (err) { console.error("Error creating QR code"); return; } setQrBase64(res); }); } else { setQrBase64(null); setPerson(null); props.setPerson(null); } }); /* Get the user data from the DB */ const search = async() => { setLoading(true); setError(""); try { const response = await fetch(`${import.meta.env.VITE_BACKEND_URL}/api/person/${dni()}`); const body = await response.json(); if (response.ok) { setPerson(body); props.setPerson(body); } else if (response.status === 404) { console.error(body); setError("No encontrado. Ingresar datos manualmente."); props.setPerson(null); } else { setError(body); } } catch (e) { setError(JSON.stringify(e)); } setLoading(false); }; const namesAndSurnames = () => { const p = person(); if (p === null) { return ""; } return `${p.person_names} ${p.person_paternal_surname} ${p.person_maternal_surname}`; }; const surnamesAndNames = () => { const p = person(); if (p === null) { return ""; } return `${p.person_paternal_surname} ${p.person_maternal_surname} ${p.person_names}`; }; const surnames = () => { const p = person(); if (p === null) { return ""; } return `${p.person_paternal_surname} ${p.person_maternal_surname}`; }; const personNames = () => { const p = person(); if (p === null) { return ""; } return p.person_names; }; return (
ev.preventDefault()} class="px-4">

Error: {error()}

  Nombres y Apellidos   Apellidos y Nombres
  Apellidos   Nombres
{/* */}
); } function InputBox(props: { loading: boolean, dni: string, setDni: (v: string) => void, }) { let inputElement: HTMLInputElement | undefined; const copyToClipboard: HTMLButtonEvent = (ev) => { ev.preventDefault(); if (props.dni.length === 8) { navigator.clipboard.writeText(props.dni); } }; const clearDni: HTMLButtonEvent = (ev) => { ev.preventDefault(); props.setDni(""); (inputElement as HTMLInputElement).focus(); }; return (
props.setDni(e.target.value)} disabled={props.loading} />
); } function MaterialLabel(props: {text: string, resource: string}) { const copyToClipboard: HTMLButtonEvent = (ev) => { ev.preventDefault(); if (props.text !== "") { navigator.clipboard.writeText(props.text); } }; return (
{props.text}  
); } function CopyButton(props: {copyText: string, children: Array | JSX.Element}) { const [successAnimation, setSuccessAnimation] = createSignal(false); const onclick = () => { if (props.copyText !== "") { navigator.clipboard.writeText(props.copyText); setSuccessAnimation(true); setTimeout(() => setSuccessAnimation(false), 500); } }; return ( ); }