Skip to content

Commit

Permalink
Evite de dupliquer le dialogue de confirmation de la suppression
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-rutkowski committed Jan 21, 2025
1 parent cf2593e commit ea81d2c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { IndicateurDefinition } from '@/api/indicateurs/domain/definition.schema';
import { Button, HEAD_CELL_STYLE, Notification, TCell, Tooltip } from '@/ui';
import classNames from 'classnames';
import { useState } from 'react';
import { SourceType } from '../types';
import { ConfirmDelete } from './confirm-delete';
import { PreparedData, PreparedValue } from './prepare-data';

type CellAnneeListProps = {
Expand All @@ -19,13 +17,11 @@ type CellAnneeListProps = {
export const CellAnneeList = ({
confidentiel,
data,
definition,
readonly,
type,
onDelete,
}: CellAnneeListProps) => {
const { annees, anneeModePrive, valeursExistantes } = data;
const [toBeDeleted, setToBeDeleted] = useState<PreparedValue | null>(null);

return annees?.map((annee) => {
const valeur = valeursExistantes.find((v) => v.annee === annee);
Expand All @@ -50,35 +46,10 @@ export const CellAnneeList = ({
icon="delete-bin-6-line"
variant="outlined"
size="xs"
onClick={() => {
// demande confirmation avant de supprimer
if (
(valeur.objectif ?? false) ||
(valeur.resultat ?? false) ||
valeur.resultatCommentaire ||
valeur.objectifCommentaire
) {
setToBeDeleted(valeur);
} else {
// sauf pour les lignes n'ayant ni valeur ni commentaire
onDelete(valeur);
}
}}
onClick={() => onDelete(valeur)}
/>
)}
</div>
{toBeDeleted && (
<ConfirmDelete
valeur={toBeDeleted}
unite={definition.unite}
onDismissConfirm={(confirmed) => {
if (confirmed) {
onDelete(toBeDeleted);
}
setToBeDeleted(null);
}}
/>
)}
</TCell>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { SourceType } from '../types';
import { CellAnneeList } from './cell-annee-list';
import { CellSourceName } from './cell-source-name';
import { CellValue } from './cell-value';
import { ConfirmDelete } from './confirm-delete';
import { EditCommentaireModal } from './edit-commentaire-modal';
import { PreparedData } from './prepare-data';
import { PreparedData, PreparedValue } from './prepare-data';
import { useDeleteIndicateurValeur } from './use-delete-indicateur-valeur';
import { useUpsertIndicateurValeur } from './use-upsert-indicateur-valeur';

Expand Down Expand Up @@ -45,6 +46,8 @@ export const IndicateurValeursTable = ({
annee: number;
commentaire?: string | null;
}>(null);
const [toBeDeleted, setToBeDeleted] = useState<PreparedValue | null>(null);

const { mutate: upsertValeur } = useUpsertIndicateurValeur(definition);
const { mutate: deleteValeur } = useDeleteIndicateurValeur(definition);

Expand All @@ -63,11 +66,22 @@ export const IndicateurValeursTable = ({
readonly={readonly}
type={type}
onDelete={(valeur) => {
deleteValeur({
collectiviteId,
indicateurId: definition.id,
id: valeur.id,
});
// demande confirmation avant de supprimer
if (
(valeur.objectif ?? false) ||
(valeur.resultat ?? false) ||
valeur.resultatCommentaire ||
valeur.objectifCommentaire
) {
setToBeDeleted(valeur);
} else {
// sauf pour les lignes n'ayant ni valeur ni commentaire
deleteValeur({
collectiviteId,
indicateurId: definition.id,
id: valeur.id,
});
}
}}
/>
)}
Expand Down Expand Up @@ -155,6 +169,22 @@ export const IndicateurValeursTable = ({
}}
/>
)}
{toBeDeleted && (
<ConfirmDelete
valeur={toBeDeleted}
unite={definition.unite}
onDismissConfirm={(confirmed) => {
if (confirmed) {
deleteValeur({
collectiviteId,
indicateurId: definition.id,
id: toBeDeleted.id,
});
}
setToBeDeleted(null);
}}
/>
)}
</>
);
};
Expand Down

0 comments on commit ea81d2c

Please sign in to comment.