From c32e2ccb4a18c8dcb775944c724efb1749b5bb65 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 29 Aug 2023 11:44:07 -0500 Subject: [PATCH] [FE][Certs] Integrate cert generator --- frontend/src/certGenerator/index.ts | 16 ++++++---- frontend/src/certs/Registers/index.tsx | 44 +++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/frontend/src/certGenerator/index.ts b/frontend/src/certGenerator/index.ts index 38c961e..89e8fb9 100644 --- a/frontend/src/certGenerator/index.ts +++ b/frontend/src/certGenerator/index.ts @@ -22,15 +22,19 @@ async function download(document: Document, filename: string = "filename.docx") saveAs(blob, filename); } -const certGeneratorGenerator = (generatorFn: (options: CertData) => Promise) => (options: CertData, filename: string) => { +const certGeneratorGenerator = (generatorFn: (options: CertData) => Promise) => (filename: string, options: CertData) => { generatorFn(options) .then((doc) => { download(doc, filename); }); }; -export const certGenerator = Object.freeze({ - matpel1: (options: CertData, filename: string) => { +type CertGenObj = { + [key: string]: (filename: string, options: CertData) => void +} + +export const certGenerator: CertGenObj = Object.freeze({ + matpel1: (filename: string, options: CertData) => { const options_f: CertData = { ...options, matpel: Matpel._1, @@ -40,7 +44,7 @@ export const certGenerator = Object.freeze({ download(doc, filename); }); }, - matpel2: (options: CertData, filename: string) => { + matpel2: (filename: string, options: CertData) => { const options_f: CertData = { ...options, matpel: Matpel._2, @@ -50,7 +54,7 @@ export const certGenerator = Object.freeze({ download(doc, filename); }); }, - matpel3: (options: CertData, filename: string) => { + matpel3: (filename: string, options: CertData) => { const options_f: CertData = { ...options, matpel: Matpel._3, @@ -61,7 +65,7 @@ export const certGenerator = Object.freeze({ }); }, manejoDefensivo: certGeneratorGenerator(manejoDefensivoCert), - mecanicaBasica: certGeneratorGenerator(mecanicaBasicaCert), + "Mec. Basica": certGeneratorGenerator(mecanicaBasicaCert), "4x4": certGeneratorGenerator(_4X4Cert), segOpMaqPes: certGeneratorGenerator(segOpMaqPesCert), supervisorEscolta: certGeneratorGenerator(supervisorEscolta), diff --git a/frontend/src/certs/Registers/index.tsx b/frontend/src/certs/Registers/index.tsx index 3f46250..ce757eb 100644 --- a/frontend/src/certs/Registers/index.tsx +++ b/frontend/src/certs/Registers/index.tsx @@ -3,6 +3,7 @@ import { DownloadIcon } from "../../icons/DownloadIcon"; import { Person } from "../../types/Person"; import { Register } from "../../types/Register"; import { courseMap } from "../../utils/allCourses"; +import { certGenerator } from "../../certGenerator"; export function Registers(props: {person: Person | null}) { const [registers, setRegisters] = createSignal>([]); @@ -31,16 +32,22 @@ export function Registers(props: {person: Person | null}) {
- {(register) => } + {(register) => }
); } -function RegisterEl(props: {register: Register}) { - const displayDate = () => { +function RegisterEl(props: {register: Register, person: Person}) { + + const dateComponents = () => { const [, year, month, day] = /(\d{4})-(\d{2})-(\d{2})/.exec(props.register.register_display_date) ?? []; + return {year, month, day}; + }; + + const displayDate = () => { + const {year, month, day} = dateComponents(); return `${day}/${month}/${year}`; }; @@ -49,9 +56,38 @@ function RegisterEl(props: {register: Register}) { return course?.course_name ?? "Curso no encontrado"; }; + const genCert = () => { + const courseN = courseName(); + const person = props.person; + const register = props.register; + + const generator = certGenerator[courseN]; + if (generator === undefined) { + console.error(`Generator function with key \`${courseN}\` not found`); + return; + } + + const {year, month, day} = dateComponents(); + const personFullName = `${person.person_names} ${person.person_paternal_surname} ${person.person_maternal_surname}`; + + generator(`${courseN} - ${personFullName}.docx`, { + matpel: null, + personFullName, + personDni: person.person_dni.toString(), + certCode: register.register_code.toString().padStart(4, "0"), + certYear: year, + certMonth: month, + certDay: day, + certIId: register.register_code, + }); + }; + return (
-