Skip to content

Commit

Permalink
feat: pagination enfants (#585)
Browse files Browse the repository at this point in the history
* fix: rework of table and formulaire enfant (demandeurs)

* fix: fix desynchro enfants form

* fix: finish nex implementation form enfants
  • Loading branch information
YoannNumericite authored Oct 12, 2023
1 parent 9601557 commit 5ed6796
Show file tree
Hide file tree
Showing 13 changed files with 1,981 additions and 56 deletions.
70 changes: 70 additions & 0 deletions formulaire/pages/api/enfants/dossier/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { NextApiHandler, NextApiRequest } from "next";
import { Enfant, PrismaClient } from "@prisma/client";
import { getSession } from "next-auth/react";

const handler: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
if (!session) {
res.status(401).end();
return;
}

if (req.method == "GET") {
await get(req, res);
} else {
res.status(405).end();
return;
}
};

function getId(req: NextApiRequest): number {
return Number(req.query.id as string);
}

const get: NextApiHandler = async (req, res) => {
const session = await getSession({ req });
const prisma = new PrismaClient();
console.log('order : ', req.query.order)
try {
const dossierId = getId(req);
const dossierConcerned = await prisma.dossier.findUnique({
where: {
id: dossierId
}
})

if (
dossierConcerned?.userId === session?.dbUser.id ||
dossierConcerned?.collaboratorIds.includes(session?.dbUser.id)
) {
const enfantsByDossier = await prisma.enfant.findMany({
take: req.query.numberByPage ? Number(req.query.numberByPage) : 25,
skip: 25 * Number(req.query.page),
include: {
piecesDossier: true,
remuneration: true,
Comments: true
},
where: {
dossierId: dossierId
},
orderBy: {
[req.query.termToOrder as string]: req.query.order as string
}
})
const countEnfants = await prisma.enfant.count({
where: {
dossierId: dossierId
}
}
)
res.status(200).json({enfants: enfantsByDossier, count: countEnfants});
} else {
res.status(401).json({ message: "Unauthorized" });
}
} catch(e) {
console.log(e);
}
};

export default handler;
1 change: 1 addition & 0 deletions formulaire/pages/api/enfants/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const update: NextApiHandler = async (req, res) => {

delete parsed.piecesDossier;
delete parsed.remuneration;
delete parsed.Comments;

const enfantUpdated = await prisma.enfant.update({
data: parsed as Enfant,
Expand Down
75 changes: 41 additions & 34 deletions formulaire/pages/dossier/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ResDossier,
} from "../../src/fetching/dossiers";
import { getAgentByDossierId } from "src/fetching/instructeur";
import IconLoader from "src/components/IconLoader";

const DossierPage: React.FC = () => {
const router = useRouter();
Expand Down Expand Up @@ -64,40 +65,46 @@ const DossierPage: React.FC = () => {
}

return (
<Layout windowTitle="Mes dossiers">
{dossier && (
<>
<HeadingDossier
dossier={dossier.dossier}
setShowDialogue={setShowDialogue}
agent={agent}
commissionDate={commissionDate}
></HeadingDossier>
<ShareDossierModal
dossier={dossier.dossier}
showDialogue={showDialogue}
setShowDialogue={setShowDialogue}
setCollaboratorId={handleCollaboratorIdsChange}
/>
</>
)}
<Container>
<Link href="/">Retour aux dossiers</Link>
{dossier && (
<>
<StateProvider>
{!loading && (
<DossierForm
dossier={dossier.dossier}
docs={dossier.docs}
comments={comments}
></DossierForm>
)}
</StateProvider>
</>
)}
</Container>
</Layout>
<>
{dossier ?
<Layout windowTitle="Mes dossiers">
{dossier && (
<>
<HeadingDossier
dossier={dossier.dossier}
setShowDialogue={setShowDialogue}
agent={agent}
commissionDate={commissionDate}
></HeadingDossier>
<ShareDossierModal
dossier={dossier.dossier}
showDialogue={showDialogue}
setShowDialogue={setShowDialogue}
setCollaboratorId={handleCollaboratorIdsChange}
/>
</>
)}
<Container>
<Link href="/">Retour aux dossiers</Link>
{dossier && (
<>
<StateProvider>
{!loading && (
<DossierForm
dossier={dossier.dossier}
docs={dossier.docs}
comments={comments}
></DossierForm>
)}
</StateProvider>
</>
)}
</Container>
</Layout>
:
<div style={{textAlign: 'center', margin: '2rem'}}><IconLoader></IconLoader></div>
}
</>
);
};

Expand Down
44 changes: 33 additions & 11 deletions formulaire/src/components/Dossier/DossierForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Comments, Demandeur, Enfant, SocieteProduction } from "@prisma/client";
import React, { useContext } from "react";
import { DossierData, ResDocs, updateDossier } from "../../fetching/dossiers";
import { DossierData, EnfantData, ResDocs, updateDossier } from "../../fetching/dossiers";
import { ButtonLink } from "../../uiComponents/button";
import styles from "./DossierForm.module.scss";
import "react-datepicker/dist/react-datepicker.css";
Expand All @@ -16,6 +16,8 @@ import IconLoader from "../IconLoader";
import useStateContext from "src/context/StateContext";
import { updateCommentairesNotifications } from "src/fetching/commentaires";
import { sendEmail } from "src/fetching/email";
import EnfantListBis from "./EnfantListBis";
import { getEnfantsByDossierId } from "src/fetching/enfant";

interface Props {
dossier: DossierData;
Expand All @@ -27,7 +29,7 @@ const DossierForm: React.FC<Props> = ({ dossier, docs, comments }) => {
const router = useRouter();

const [toDisplay, setTodisplay] = React.useState<
"Demandeur" | "Projet" | "Enfants"
"Demandeur" | "Projet" | "Enfants" | "EnfantsBis"
>("Demandeur");
const [messageError, setMessageError] = React.useState<string>("");
const [messageSuccess, setMessageSuccess] = React.useState<string>("");
Expand Down Expand Up @@ -55,7 +57,8 @@ const DossierForm: React.FC<Props> = ({ dossier, docs, comments }) => {
let verif = true;
setMessageError("");

CHECKS.map((entity) => {
await Promise.all(CHECKS.map(async (entity) => {
console.log('entity : ', entity.entity)
switch (entity.entity) {
case "Demandeur":
entity.mandatory_fields.map((field) => {
Expand Down Expand Up @@ -113,15 +116,20 @@ const DossierForm: React.FC<Props> = ({ dossier, docs, comments }) => {
}
});
break;
case "Enfants":
contextDossier.enfants.map((enfant) => {
case "EnfantsBis":
console.log('in enfant')
const enfantsFetched = await getEnfantsByDossierId(contextDossier.dossier.id, 0, 250, 'nom', 'asc')
console.log('fetch : ', enfantsFetched.enfants)
enfantsFetched.enfants.map((enfant) => {
console.log('for enfant : ', enfant.nom + ' ' + enfant.prenom)
entity.mandatory_fields.map((field) => {
console.log('checking field : ', field.code)
if (
!enfant[field.code as keyof Enfant] ||
enfant[field.code as keyof Enfant] === ""
) {
setTodisplay(
entity.entity as "Demandeur" | "Projet" | "Enfants"
entity.entity as "Demandeur" | "Projet" | "EnfantsBis"
);
setMessageError(
`Le champ '${field.label}' est necessaire pour l'enfant ${enfant.nom} ${enfant.prenom}`
Expand All @@ -134,15 +142,18 @@ const DossierForm: React.FC<Props> = ({ dossier, docs, comments }) => {
default:
return false;
}
});
}));

console.log("verif: ", verif);
setMessageSuccess("");
return verif;
};

const handleDepotDossier = async () => {
console.log("trying depot : ", contextDossier);

setMessageSuccess(
"Vérification du dossier en cours ..."
);
if (await processChecks()) {
setMessageSuccess(
"Votre dossier est en cours d'envoi aux services d'instructions"
Expand All @@ -152,7 +163,7 @@ const DossierForm: React.FC<Props> = ({ dossier, docs, comments }) => {
contextDossier.dossier,
contextDossier.demandeur,
contextDossier.societeProduction,
contextDossier.enfants
(await getEnfantsByDossierId(contextDossier.dossier.id, 0, 250, 'nom', 'asc')).enfants
);
if (!dossierSent.error) {
if (contextDossier.dossier.statut === "BROUILLON") {
Expand Down Expand Up @@ -236,8 +247,8 @@ const DossierForm: React.FC<Props> = ({ dossier, docs, comments }) => {
Projet
</ButtonLink>
<ButtonLink
light={toDisplay !== "Enfants"}
onClick={() => setTodisplay("Enfants")}
light={toDisplay !== "EnfantsBis"}
onClick={() => setTodisplay("EnfantsBis")}
>
Enfants
</ButtonLink>
Expand Down Expand Up @@ -304,6 +315,17 @@ const DossierForm: React.FC<Props> = ({ dossier, docs, comments }) => {
</>
)}

{toDisplay === "EnfantsBis" && (
<>
<EnfantListBis
allowChanges={
dossier.statut === "BROUILLON" ||
dossier.statut === "CONSTRUCTION"
}
></EnfantListBis>
</>
)}

{(dossier.statut === "BROUILLON" ||
dossier.statut === "CONSTRUCTION") && (
<div className={styles.saveBar}>
Expand Down
1 change: 0 additions & 1 deletion formulaire/src/components/Dossier/EnfantForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ const EnfantForm: React.FC<Props> = ({ enfant, allowChanges, refresh }) => {
if (initialRender) {
setInitialRender(false);
} else {
saveEnfant();
}
}, [enfantTmp]);

Expand Down
Loading

0 comments on commit 5ed6796

Please sign in to comment.