Skip to content
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 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { createElement, useEffect, useState } from 'react';
import { FicheAction } from '@tet/api/plan-actions';
import { useFicheActionChemins } from '../../PlanAction/data/usePlanActionChemin';
import { useIndicateurDefinitions } from '../../../Indicateurs/Indicateur/useIndicateurDefinition';
import { useFichesActionLiees } from '../data/useFichesActionLiees';
import { useActionListe } from '../data/options/useActionListe';
import { useAnnexesFicheActionInfos } from '../data/useAnnexesFicheActionInfos';
import { useFicheActionNotesSuivi } from '../data/useFicheActionNotesSuivi';
import { useFicheActionChemins } from '../PlanAction/data/usePlanActionChemin';
import { useIndicateurDefinitions } from '../../Indicateurs/Indicateur/useIndicateurDefinition';
import { useFichesActionLiees } from '../FicheAction/data/useFichesActionLiees';
import { useActionListe } from '../FicheAction/data/options/useActionListe';
import { useAnnexesFicheActionInfos } from '../FicheAction/data/useAnnexesFicheActionInfos';
import { useFicheActionNotesSuivi } from '../FicheAction/data/useFicheActionNotesSuivi';
import ExportPDFButton from 'ui/export-pdf/ExportPDFButton';
import FicheActionPdf from '../FicheActionPdf/FicheActionPdf';
import FicheActionPdf from './FicheActionPdf/FicheActionPdf';

type FicheActionPdfContentProps = {
fiche: FicheAction;
generateContent: (content: JSX.Element) => void;
};

const FicheActionPdfContent = ({
export const FicheActionPdfContent = ({
Copy link
Contributor

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 du ExportFicheActionButton, et faire que le FicheActionPdfContent retourne vraiment du JSX à la place de l'utilisation plus complexe du callback generateContent et d'un "faux" retour de composant vide <></>.

Copy link
Contributor Author

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 dans updateInstance de react-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 composant FicheActionPdf.tsx, je me retrouve avec des erreurs de la part de react-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 composant FicheActionPdf.

fiche,
generateContent,
}: FicheActionPdfContentProps) => {
Expand Down
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;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { preset } from '@tet/ui';
import { TAxeRow } from 'types/alias';
import { Paragraph, Stack } from 'ui/export-pdf/components';
import { ArrowRightIcon } from 'ui/export-pdf/assets/icons';
import { generateTitle } from '../data/utils';
import { generateTitle } from '../../FicheAction/data/utils';

const { colors } = preset.theme.extend;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
} from 'ui/export-pdf/components';
import { DiscussIcon, FileIcon, LinkIcon } from 'ui/export-pdf/assets/icons';
import { getAuthorAndDate } from 'ui/shared/preuves/Bibliotheque/utils';
import { AnnexeInfo } from '../data/useAnnexesFicheActionInfos';
import { generateTitle } from '../data/utils';
import { AnnexeInfo } from '../../FicheAction/data/useAnnexesFicheActionInfos';
import { generateTitle } from '../../FicheAction/data/utils';

const { colors } = preset.theme.extend;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@tet/api/plan-actions';
import { TActionStatutsRow, TAxeRow } from 'types/alias';
import { IndicateurDefinition } from '@tet/api/indicateurs/domain';
import { AnnexeInfo } from '../data/useAnnexesFicheActionInfos';
import { AnnexeInfo } from '../../FicheAction/data/useAnnexesFicheActionInfos';
import { Divider, Stack, Title } from 'ui/export-pdf/components';

import Acteurs from './Acteurs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
LoopLeftIcon,
UserIcon,
} from 'ui/export-pdf/assets/icons';
import { generateTitle } from '../data/utils';
import { generateTitle } from '../../FicheAction/data/utils';

const { colors } = preset.theme.extend;

Expand Down Expand Up @@ -80,7 +80,7 @@ const FicheLieeCard = ({ ficheLiee }: FicheLieeCardProps) => {
<Stack className="mt-auto" gap={1}>
{/* Pilotes et date de fin prévisionnelle */}
{(hasPilotes || hasDateDeFin || ameliorationContinue) && (
<Stack direction="row" gap={2}>
<Stack direction="row" gap={2} className="flex-wrap">
{/* Personnes pilote */}
{hasPilotes && (
<Stack gap={1} direction="row" className="items-center">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { format } from 'date-fns';
import { FicheActionNote } from '@tet/api/plan-actions';
import { preset } from '@tet/ui';
import { Card, Paragraph, Stack, Title } from 'ui/export-pdf/components';
import { format } from 'date-fns';
import { EditIcon, UserIcon } from 'ui/export-pdf/assets/icons';

const { colors } = preset.theme.extend;

type NotesDeSuiviCardProps = {
noteSuivi: FicheActionNote;
Expand All @@ -22,20 +26,25 @@ const NotesDeSuiviCard = ({ noteSuivi }: NotesDeSuiviCardProps) => {
<Title variant="h6">{new Date(dateNote).getFullYear()}</Title>

{/* Contenu */}
<Paragraph className="text-[0.65rem] text-grey-8 font-medium">
{note}
</Paragraph>
<Paragraph className="text-[0.65rem]">{note}</Paragraph>

{/* Créée par... / modifiée par... */}
<Paragraph className="text-[0.65rem]">
Créée le {format(new Date(createdAt), 'dd/MM/yyyy')} par {createdBy}
<Stack gap={1} direction="row" className="items-center">
<UserIcon fill={colors.grey[8]} />
<Paragraph className="text-[0.65rem] text-grey-8">
Créée le {format(new Date(createdAt), 'dd/MM/yyyy')} par {createdBy}
</Paragraph>
{modifiedAt !== createdAt && (
<>
| Modifiée le {format(new Date(modifiedAt), 'dd/MM/yyyy')} par{' '}
{modifiedBy}
<Paragraph className="text-[0.65rem] text-grey-8">| </Paragraph>
<EditIcon fill={colors.grey[8]} />
<Paragraph className="text-[0.65rem] text-grey-8">
Modifiée le {format(new Date(modifiedAt), 'dd/MM/yyyy')} par{' '}
{modifiedBy}
</Paragraph>
</>
)}
</Paragraph>
</Stack>
</Stack>
</Card>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Pilotes = ({ fiche }: FicheActionPdfProps) => {

return (
<Card className="justify-center">
<PersonnePilotePicto className="h-15 w-15 mx-auto" />
<PersonnePilotePicto className="h-14 w-14 mx-auto" />
<Stack gap={1} className="text-center items-center">
<Title variant="h6" className="uppercase text-center">
Personnes pilotes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FicheResume } from '@tet/api/plan-actions/domain';
import { Button, Card, Notification, Tooltip } from '@tet/ui';
import { Button, Card, Checkbox, Notification, Tooltip } from '@tet/ui';
import classNames from 'classnames';
import { useCurrentCollectivite } from 'core-logic/hooks/useCurrentCollectivite';
import { useState } from 'react';
Expand All @@ -24,8 +24,12 @@ type FicheActionCardProps = {
/** Pour invalider la liste des fiches d'un axe à la suppression de la fiche */
axeIdToInvalidate?: number;
editKeysToInvalidate?: QueryKey[];
/** Etat sélectionné ou non de la fiche */
isSelected?: boolean;
/** Dissociation de la fiche action */
onUnlink?: () => void;
/** Sélection de la fiche action */
onSelect?: (isSelected: boolean) => void;
};

const FicheActionCard = ({
Expand All @@ -35,7 +39,9 @@ const FicheActionCard = ({
isEditable = false,
axeIdToInvalidate,
editKeysToInvalidate,
isSelected = false,
onUnlink,
onSelect,
}: FicheActionCardProps) => {
const collectivite = useCurrentCollectivite();

Expand All @@ -60,7 +66,7 @@ const FicheActionCard = ({
onClick={onUnlink}
/>
)}
{isEditable && (
{isEditable && !onSelect && (
<>
<>
{isEditOpen && (
Expand Down Expand Up @@ -117,8 +123,10 @@ const FicheActionCard = ({
'hover:border-primary-3 hover:!bg-primary-1': !isNotClickable,
}
)}
href={link}
href={onSelect ? undefined : link}
onClick={onSelect ? () => onSelect(!isSelected) : undefined}
disabled={isNotClickable}
isSelected={isSelected}
external={openInNewTab}
header={
// Badges priorité et statut de la fiche
Expand Down Expand Up @@ -171,9 +179,12 @@ const FicheActionCard = ({
}
>
{/* Titre de la fiche action */}
<span className="text-base font-bold text-primary-9">
{generateTitle(ficheAction.titre)}
</span>
<div className="flex min-w-min">
{onSelect && <Checkbox checked={isSelected} />}
<span className="text-base font-bold text-primary-9">
{generateTitle(ficheAction.titre)}
</span>
</div>

{/* Plans d'action dans lesquels sont la fiche */}
<span title="Emplacements" className="text-sm font-medium">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FicheAction } from '@tet/api/plan-actions';
import ModaleDescription from './ModaleDescription';
import ModaleEmplacement from './EmplacementFiche/ModaleEmplacement';
import ModaleSuppression from './ModaleSuppression';
import ExportFicheActionButton from './ExportFicheActionButton';
import ExportFicheActionButton from '../../ExportPdf/ExportFicheActionButton';

type MenuDescriptionProps = {
isReadonly: boolean;
Expand Down
Loading
Loading