diff --git a/frontend/src/certs/Registers/RegisterElement.tsx b/frontend/src/certs/Registers/RegisterElement.tsx index cd11f83..04afac6 100644 --- a/frontend/src/certs/Registers/RegisterElement.tsx +++ b/frontend/src/certs/Registers/RegisterElement.tsx @@ -1,4 +1,4 @@ -import { Show } from "solid-js"; +import { Show, createMemo } from "solid-js"; import { certGenerator } from "../../certGenerator"; import { Person } from "../../types/Person"; import { Register } from "../../types/Register"; @@ -6,6 +6,15 @@ import { courseMap } from "../../utils/allCourses"; import { customLabelsMap } from "../../utils/allCustomLabels"; import { DownloadSimpleIcon } from "../../icons/DownloadSimpleIcon"; import { CaretRight } from "../../icons/CaretRight"; +import { IdentificationCardIcon } from "../../icons/IdentificationCardIcon"; +import { numberToMonth } from "../../utils/functions"; +import { genCarnet } from "../../utils/carnetGenerator"; +import { Canvg } from "canvg"; +import QR from "qrcode"; +import saveAs from "file-saver"; + +// TODO: use +// const carnetEnabledIds = [12]; export function RegisterElement(props: {register: Register, person: Person, onClick: () => void}) { @@ -42,6 +51,65 @@ export function RegisterElement(props: {register: Register, person: Person, onCl return certGenerator[courseN] !== undefined; }; + // TODO: Check if the course is enabled for carnet generation + // with the list above + const canGenerateCarnet = () => props.register.register_course_id === 12; + + const gridClass = () => (canGenerateCarnet() ? "grid-cols-[11rem_1.5rem_1.5rem_1.25rem]" : "grid-cols-[12.5rem_1.5rem_1.25rem]"); + + const paddedCertCode = createMemo(() => { + const code = props.register.register_code.toString(); + return code.padStart(4, "0"); + }); + + const generateCarnet = () => { + const canvas = document.createElement("canvas"); + const ctx = canvas.getContext("2d") as CanvasRenderingContext2D; + + const p = props.person; + const r = props.register; + + const [expiryYear, month] = r.register_display_date.split("-"); + + const expiryMonth = numberToMonth(parseInt(month, 10)); + + if (expiryMonth === null) { + alert(`El cert. tiene un mes invalido (${month}), no se puede generar carnet`); + return; + } + + // QR code + QR.toDataURL(`https://eegsac.com/certificado/${p.person_dni}`, {margin: 1}, (err, base64) => { + if (err) { + alert("Error creando codigo QR para el carnet."); + console.error("Error creating QR code"); + console.error(err); + return; + } + + const fullname = `${p.person_names} ${p.person_paternal_surname} ${p.person_maternal_surname}`; + const svgHtml = genCarnet({ + fullname, + dni: p.person_dni.toString(), + code: r.register_code.toString().padStart(4, "0"), + expiryMonth, + expiryYear, + qrBase64: base64, + }); + const v = Canvg.fromString(ctx, svgHtml); + v.start(); + v.ready().then(() => { + canvas.toBlob((blob) => { + if (blob !== null) { + saveAs(blob, `CARNET MATPEL - ${fullname}.png`); + } else { + console.log("blob is null... :c"); + } + }); + }); + }); + }; + const createdTodayClasses = () => { // current dat in YYYY-MM-DD format const today = new Date().toISOString() @@ -52,7 +120,7 @@ export function RegisterElement(props: {register: Register, person: Person, onCl }; return ( -
+

{ - const code = props.register.register_code.toString(); - const paddedCode = code.padStart(4, "0"); - navigator.clipboard.writeText(paddedCode); + navigator.clipboard.writeText(paddedCertCode()); }} > - {props.register.register_code} + {paddedCertCode()}

+ + + -
diff --git a/frontend/src/icons/IdentificationCardIcon.tsx b/frontend/src/icons/IdentificationCardIcon.tsx new file mode 100644 index 0000000..6020ede --- /dev/null +++ b/frontend/src/icons/IdentificationCardIcon.tsx @@ -0,0 +1,15 @@ + +export function IdentificationCardIcon(props: {fill: string, size?: number}) { + return ( + + + + ); +}