Generateurv2/frontend/pages/fiches.jsx

138 lines
4.1 KiB
React
Raw Normal View History

2022-05-18 10:15:54 +02:00
import { useEffect } from "react";
import { useState } from "react";
import { useQuery } from "react-query";
import Layout from "../components/Layout.js";
import Modal from "../components/Modal.jsx";
import PdfForm from "../components/pdf_gen/PdfForm.jsx";
import SelectExo from "../components/pdf_gen/selectExo.jsx";
2022-06-24 13:42:16 +02:00
import {
getPublicExos,
getTags,
getUserExos,
pdfGen,
} from "../requests/requests.exos.js";
import { notificationService } from "../services/notification.service.js";
2022-05-18 10:15:54 +02:00
import styles from "../styles/pdf_gen/fiche.module.scss";
import { parseClassName } from "../utils/utils.js";
2022-06-24 13:42:16 +02:00
export default function Fiche() {
/* const { isLoading, isError, data, isFetching } = useQuery(
2022-05-18 10:15:54 +02:00
["exo-data"],
2022-06-24 13:42:16 +02:00
async () =>
await getUserExos("pdf").then((res) => {
return {
...res,
results: [
...res.results.map((r) => {
return { ...r, numberInExo: 10, numberOfExo: 1 };
}),
],
};
}),
2022-05-18 10:15:54 +02:00
{
refetchOnWindowFocus: false,
keepPreviousData: true,
}
2022-06-24 13:42:16 +02:00
); */
2022-05-18 10:15:54 +02:00
const generateFile = (content, fileName) => {
const byteCharacters = atob(content);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
const blob = new Blob([byteArray], { type: "application/pdf" });
const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
};
const [exosList, setExos] = useState([]);
2022-06-24 13:42:16 +02:00
2022-05-18 10:15:54 +02:00
const [selected, setSelected] = useState([]);
const [charging, setCharging] = useState(false);
2022-06-24 13:42:16 +02:00
const [empty, setEmpty] = useState(false);
2022-05-18 10:15:54 +02:00
return (
<Layout page="Composition de fiches">
2022-06-24 13:42:16 +02:00
<div className={styles["main-container"]}>
<SelectExo
exos={exosList}
setExos={setExos}
emptyError={empty}
/>
<div className={styles.form}>
<PdfForm
submit={(param) => {
/* var exosSend = exosList
.filter((e) => {
return selected.includes(e.id_code);
})
.sort((a, b) => {
return (
selected.indexOf(a.id_code) - selected.indexOf(b.id_code)
2022-05-18 10:15:54 +02:00
);
2022-06-24 13:42:16 +02:00
}); */
setCharging(true);
/* exosSend = exosSend.map((s) => {
return {
numberInExo: s.numberInExo,
numberOfExo: s.numberOfExo,
exo_model: s.exo_model,
model_type: s.model_type,
consigne: s.consigne,
id_code: s.id_code,
};
}); */
var exosSend = exosList.reduce((res, current) => {
return res.concat(
Array(parseInt(current.numberOfExo)).fill(current)
);
}, []);
console.log('EXOS RO', exosSend)
if (exosSend.length == 0) {
setEmpty(true);
setCharging(false);
return;
}
pdfGen({
exos: exosSend,
file: param.name !== "" ? param.name : "untitled.pdf",
police: param.police != "" ? param.police + "pt" : 14 + "pt",
title: param.entete,
}).then((res) => {
generateFile(res.data.pdf, res.data.filename);
setCharging(false);
}).catch((err) => {
setCharging(false)
notificationService.error('Erreur', 'Erreur lors de la génération !')
});
}}
/>{" "}
2022-05-18 10:15:54 +02:00
</div>
2022-06-24 13:42:16 +02:00
</div>
2022-05-18 10:15:54 +02:00
{charging && (
<>
<div className="overlay"></div>
<div className="loader-big"></div>
</>
)}
</Layout>
);
}
2022-06-24 13:42:16 +02:00
/* export async function getStaticProps() {
2022-05-18 10:15:54 +02:00
const exos = await getExos('pdf');
const tags = await getTags();
return {
props: {
exos: exos,
tags: tags,
},
revalidate: 60,
};
}
2022-06-24 13:42:16 +02:00
*/