-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export groupé des fiches actions au format pdf #3443
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 8 additions & 8 deletions
16
...onDescription/ExportFicheActionButton.tsx → ...ons/ExportPdf/ExportFicheActionButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...act/src/app/pages/collectivite/PlansActions/ExportPdf/ExportFicheActionGroupeesButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { useEffect, useState } from 'react'; | ||
import ExportPDFButton from 'ui/export-pdf/ExportPDFButton'; | ||
import { useCurrentCollectivite } from 'core-logic/hooks/useCurrentCollectivite'; | ||
import { useFicheAction } from '../FicheAction/data/useFicheAction'; | ||
import { FicheActionPdfContent } from './ExportFicheActionButton'; | ||
import { useEventTracker } from '@tet/ui'; | ||
|
||
type FicheActionPdfWrapperProps = { | ||
ficheId: number; | ||
generateContent: (content: JSX.Element) => void; | ||
}; | ||
|
||
const FicheActionPdfWrapper = ({ | ||
ficheId, | ||
generateContent, | ||
}: FicheActionPdfWrapperProps) => { | ||
const { data: fiche } = useFicheAction(ficheId.toString()); | ||
|
||
return ( | ||
fiche && ( | ||
<FicheActionPdfContent fiche={fiche} generateContent={generateContent} /> | ||
) | ||
); | ||
}; | ||
|
||
const ExportFicheActionGroupeesButton = ({ | ||
fichesIds, | ||
}: { | ||
fichesIds: number[]; | ||
}) => { | ||
const collectivite = useCurrentCollectivite()!; | ||
const tracker = useEventTracker('app/actions-groupees-fiches-action'); | ||
|
||
const [isDataRequested, setIsDataRequested] = useState(false); | ||
const [content, setContent] = useState<JSX.Element[] | undefined>(undefined); | ||
|
||
const fileName = `fiches-actions-${collectivite.collectivite_id}`; | ||
|
||
useEffect(() => { | ||
if (content?.length === fichesIds.length) { | ||
setIsDataRequested(false); | ||
} | ||
}, [content?.length, fichesIds.length]); | ||
|
||
useEffect(() => setContent(undefined), [isDataRequested]); | ||
|
||
return ( | ||
<> | ||
<ExportPDFButton | ||
{...{ fileName }} | ||
content={content?.length === fichesIds.length ? content : undefined} | ||
requestData={() => setIsDataRequested(true)} | ||
icon="file-pdf-line" | ||
variant="outlined" | ||
onClick={() => | ||
tracker('export_PDF_telechargement_groupe', { | ||
collectivite_id: collectivite.collectivite_id, | ||
}) | ||
} | ||
> | ||
Exporter au format PDF | ||
</ExportPDFButton> | ||
|
||
{isDataRequested && | ||
fichesIds.map((id) => ( | ||
<FicheActionPdfWrapper | ||
key={id} | ||
ficheId={id} | ||
generateContent={(newContent) => { | ||
setContent((prevState) => [...(prevState ?? []), newContent]); | ||
}} | ||
/> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export default ExportFicheActionGroupeesButton; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J'imagine que c'est un peu tard pour faire ça, car c'était déjà comme ça avec l'export PDF simple d'une FA, donc c'est plus pour partage / réflexion, mais je pense qu'il aurait été plus simple de séparer la logique de
FicheActionPdfContent
duExportFicheActionButton
, et faire que leFicheActionPdfContent
retourne vraiment du JSX à la place de l'utilisation plus complexe du callbackgenerateContent
et d'un "faux" retour de composant vide<></>
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
J'avais initialement généré le contenu du PDF avec un fichier qui renvoyait du JSX (
FicheActionPdf.tsx
), et que j'injectais directement dansupdateInstance
dereact-pdf
.Mon problème a été ensuite pour ajouter le contenu qui n'est pas déjà dans l'object
fiche
, et qui doit donc être fetch. Si j'utilise les hooks existant permettant de récupérer cette donnée depuis le composantFicheActionPdf.tsx
, je me retrouve avec des erreurs de la part dereact-pdf
. Et je ne voulais pas charger toute la donnée de la fiche en amont si l'utilisateur ne faisait pas la demande de téléchargement du PDF. Cette approche me permettait du coup d'appeler les hooks de manière conditionnelle, uniquement si on a besoin de construire le PDF.Au final, le rôle de
FicheActionPdfContent
est uniquement de charger la donnée nécessaire pour construire le composantFicheActionPdf
.