Skip to content

Commit

Permalink
[tra-15333]Permettre au créateur d'Effacer le composant transporteur …
Browse files Browse the repository at this point in the history
…lorsqu'il n'y a qu'un seul transporteur de visé (#3887)
  • Loading branch information
JulianaJM authored Jan 9, 2025
1 parent 464d4e4 commit a57083a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("TransporterAccordion", () => {
onTransporterShiftUp={onTransporterShiftUp}
onTransporterShiftDown={onTransporterShiftDown}
onExpanded={onExpanded}
deleteLabel="Supprimer"
>
{foldableContent}
</TransporterAccordion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type TransporterAccordionProps = {
disableDown?: boolean;
expanded?: boolean;
children: NonNullable<React.ReactNode>;
deleteLabel: string;
};

/**
Expand All @@ -38,7 +39,8 @@ export function TransporterAccordion({
disableDelete = false,
disableUp = false,
disableDown = false,
children
children,
deleteLabel
}: TransporterAccordionProps) {
const collapseElementId = `transporter__${numero}__form`;

Expand Down Expand Up @@ -82,11 +84,14 @@ export function TransporterAccordion({
priority="tertiary"
iconPosition="right"
iconId="ri-delete-bin-line"
title="Supprimer"
title={deleteLabel}
onClick={onTransporterDelete}
disabled={disableDelete}
nativeButtonProps={{
"data-testid": collapseElementId
}}
>
Supprimer
{deleteLabel}
</Button>
<Button
type="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe("<TransporterList />", () => {
"delete button is disabled when there is only one transporter and bsdType is %p",
bsdType => {
render(component([getInitialTransporter(bsdType)], bsdType));
const addButton = screen.getByTitle("Supprimer");
const addButton = screen.getByTestId("transporter__1__form");
expect(addButton).toBeDisabled();
}
);
Expand Down Expand Up @@ -159,8 +159,9 @@ describe("<TransporterList />", () => {
);
expect(await screen.findByText("1 - Transporteur")).toBeInTheDocument();
expect(await screen.findByText("2 - Transporteur")).toBeInTheDocument();
const deleteButtons = screen.getAllByTitle("Supprimer");
fireEvent.click(deleteButtons[1]);
const deleteButtonTransporteur2 =
screen.getByTestId(`transporter__2__form`);
fireEvent.click(deleteButtonTransporteur2);
expect(await screen.findByText("1 - Transporteur")).toBeInTheDocument();
await expect(() =>
screen.findByText("2 - Transporteur")
Expand Down Expand Up @@ -316,8 +317,9 @@ describe("<TransporterList />", () => {
).toBeInTheDocument();

expect(screen.getByText("Date de prise en charge")).toBeInTheDocument();
const deleteButtons = screen.getAllByTitle("Supprimer");
expect(deleteButtons[0]).toBeDisabled();
const deleteButton1 = screen.getByTestId("transporter__1__form");

expect(deleteButton1).toBeDisabled();
const upButtons = screen.getAllByTitle("Remonter");
expect(upButtons[0]).toBeDisabled();
// on ne peut pas permuter un transporteur qui a déjà signé
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,20 @@ export function TransporterList<TransporterInput extends AnyTransporterInput>({
// Supprime le transporteur en base et dans le state Formik
return deleteFormTransporter({
variables: { id: t.id },
onCompleted: () => arrayHelpers.remove(idx)
onCompleted: () => {
arrayHelpers.remove(idx);
if (transporters.length === 1) {
arrayHelpers.insert(0, initialTransporterData);
}
}
});
} else {
// Supprime le transporteur uniquement dans le state Formik
// puisqu'il n'existe pas encore en base
arrayHelpers.remove(idx);
if (transporters.length === 1) {
arrayHelpers.insert(0, initialTransporterData);
}
}
};

Expand All @@ -90,7 +98,11 @@ export function TransporterList<TransporterInput extends AnyTransporterInput>({

// Désactive la possibilité de supprimer le transporteur
// s'il est le seul dans la liste ou s'il a déjà pris en charge le déchet
const disableDelete = hasTakenOver || transporters.length === 1;
const disableDelete =
hasTakenOver || (transporters.length === 1 && !t.company?.name);

const deleteLabel =
transporters.length === 1 ? "Effacer" : "Supprimer";

// Désactive le bouton permettant de remonter le transporteur dans
// la liste s'il est le seul ou le premier, ou s'il a déjà pris en
Expand Down Expand Up @@ -151,6 +163,7 @@ export function TransporterList<TransporterInput extends AnyTransporterInput>({
disableUp={disableUp}
disableDown={disableDown}
expanded={idx === expandedIdx}
deleteLabel={deleteLabel}
>
{t.takenOverAt ? (
<TransporterDisplay transporter={t} />
Expand Down

0 comments on commit a57083a

Please sign in to comment.