[FE][Certs] Button for downloading all certs of the current date

master
Araozu 2023-09-23 12:16:18 -05:00
parent 81be2fd79f
commit a39059d74d
1 changed files with 57 additions and 28 deletions

View File

@ -74,9 +74,33 @@ export function Registers(props: {person: Person | null, count: number}) {
const inputCheck = () => setShowHidden((x) => !x); const inputCheck = () => setShowHidden((x) => !x);
const todayRegisters = createMemo(() => {
const today = new Date().toISOString()
.split("T")[0];
return registerSortedList()[0][0]
?.filter((r) => r.register_creation_date === today) ?? [];
});
const downloadTodayCerts = () => {
if (props.person === null) return;
todayRegisters().forEach((register) => {
generateCert(props.person!, register);
});
};
return ( return (
<div class="m-4 p-4 rounded-md relative"> <div class="m-4 p-4 rounded-md relative">
<h3 class="text-xl font-medium"> <h3 class="text-xl font-medium">
<button
class="bg-c-primary inline-block w-8 h-8 mr-2 rounded-full
disabled:opacity-25 disabled:cursor-not-allowed"
disabled={todayRegisters().length === 0}
title="Descargar todos los cert. creados hoy"
onclick={downloadTodayCerts}
>
<DownloadSimpleIcon fill="var(--c-on-primary)" size={24} />
</button>
{props.person?.person_names}&nbsp; {props.person?.person_names}&nbsp;
{props.person?.person_paternal_surname}&nbsp; {props.person?.person_paternal_surname}&nbsp;
{props.person?.person_maternal_surname} {props.person?.person_maternal_surname}
@ -141,37 +165,10 @@ function RegisterEl(props: {register: Register, person: Person, onClick: () => v
}; };
const genCert = () => { const genCert = () => {
const courseN = courseName();
const person = props.person; const person = props.person;
const register = props.register; const register = props.register;
const generator = certGenerator[courseN]; generateCert(person, register);
if (generator === undefined) {
alert(`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}`;
// Manage custom label
const certCustomLabel = register.register_custom_label === 1
? ""
: customLabelsMap()[register.register_custom_label]?.custom_label_value ?? "";
const certCode = register.register_is_preview ? "0000" : register.register_code.toString().padStart(4, "0");
generator(`${courseN} - ${personFullName}.docx`, {
matpel: null,
personFullName,
personDni: person.person_dni.toString(),
certCode,
certYear: year,
certMonth: month,
certDay: day,
certIId: register.register_code,
certCustomLabel,
});
}; };
const generateable = () => { const generateable = () => {
@ -240,3 +237,35 @@ function RegisterEl(props: {register: Register, person: Person, onClick: () => v
</div> </div>
); );
} }
function generateCert(person: Person, register: Register) {
const courseN = courseMap()[register.register_course_id]?.course_name ?? "Curso no encontrado";
const generator = certGenerator[courseN];
if (generator === undefined) {
alert(`Generator function with key \`${courseN}\` not found`);
return;
}
const [, year, month, day] = /(\d{4})-(\d{2})-(\d{2})/.exec(register.register_display_date) ?? [];
const personFullName = `${person.person_names} ${person.person_paternal_surname} ${person.person_maternal_surname}`;
// Manage custom label
const certCustomLabel = register.register_custom_label === 1
? ""
: customLabelsMap()[register.register_custom_label]?.custom_label_value ?? "";
const certCode = register.register_is_preview ? "0000" : register.register_code.toString().padStart(4, "0");
generator(`${courseN} - ${personFullName}.docx`, {
matpel: null,
personFullName,
personDni: person.person_dni.toString(),
certCode,
certYear: year,
certMonth: month,
certDay: day,
certIId: register.register_code,
certCustomLabel,
});
}