eeg_certs/frontend/src/certGenerator/index.ts

83 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-08-29 16:14:33 +00:00
import { saveAs } from "file-saver";
import { _4X4Cert } from "./certs/4X4";
import { CertData } from "./certs/CertData";
import { matpelCert } from "./certs/MATPEL";
import { Matpel } from "./certs/utils";
import { manejoDefensivoCert } from "./certs/MANEJO_DEFENSIVO";
import { mecanicaBasicaCert } from "./certs/MECANICA_BASICA";
import { segOpMaqPesCert } from "./certs/SEG_OP_MAQ_PES";
import { supervisorEscolta } from "./certs/SUPERVISOR_ESCOLTA";
import { ipercCert } from "./certs/IPERC";
import { excavadoraHidraulica } from "./certs/EXCAVADORA_HIDRAULICA";
2023-08-29 16:14:33 +00:00
declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
docx: any,
certs: CertGenObj,
2023-08-29 16:14:33 +00:00
}
}
async function download(document: Document, filename: string = "filename.docx") {
const blob = await window.docx.Packer.toBlob(document);
saveAs(blob, filename);
}
2023-08-29 16:44:07 +00:00
const certGeneratorGenerator = <T>(generatorFn: (options: CertData<T>) => Promise<Document>) => (filename: string, options: CertData<T>) => {
2023-08-29 16:14:33 +00:00
generatorFn(options)
.then((doc) => {
download(doc, filename);
});
};
2023-08-29 16:44:07 +00:00
type CertGenObj = {
[key: string]: (filename: string, options: CertData<null>) => void
}
export const certGenerator: CertGenObj = Object.freeze({
2023-08-30 23:28:11 +00:00
"Matpel 1": (filename: string, options: CertData<null>) => {
2023-08-29 16:14:33 +00:00
const options_f: CertData<Matpel> = {
...options,
matpel: Matpel._1,
};
matpelCert(options_f)
.then((doc) => {
download(doc, filename);
});
},
2023-08-30 23:28:11 +00:00
"Matpel 2": (filename: string, options: CertData<null>) => {
2023-08-29 16:14:33 +00:00
const options_f: CertData<Matpel> = {
...options,
matpel: Matpel._2,
};
matpelCert(options_f)
.then((doc) => {
download(doc, filename);
});
},
2023-08-30 23:28:11 +00:00
"Matpel 3": (filename: string, options: CertData<null>) => {
2023-08-29 16:14:33 +00:00
const options_f: CertData<Matpel> = {
...options,
matpel: Matpel._3,
};
matpelCert(options_f)
.then((doc) => {
download(doc, filename);
});
},
2023-08-30 23:28:11 +00:00
"Manejo Defensivo": certGeneratorGenerator(manejoDefensivoCert),
"Mecanica Basica": certGeneratorGenerator(mecanicaBasicaCert),
2023-08-29 16:14:33 +00:00
"4x4": certGeneratorGenerator(_4X4Cert),
2023-08-30 23:28:11 +00:00
"Seg. Op. Maquinaria Pesada": certGeneratorGenerator(segOpMaqPesCert),
"Sup. Escolta": certGeneratorGenerator(supervisorEscolta),
"IPERC": certGeneratorGenerator(ipercCert),
"Excavadora": certGeneratorGenerator(excavadoraHidraulica),
2023-08-29 16:14:33 +00:00
});
if (import.meta.env.DEV) {
console.log("Dev time!!");
window.certs = certGenerator;
}