Skip to content

Commit 5415164

Browse files
committed
fix: Show confirmation modal before deleting laboratory blocks
1 parent 5088082 commit 5415164

File tree

5 files changed

+235
-174
lines changed

5 files changed

+235
-174
lines changed

e2e/laboratories/edit-laboratory.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ test.describe.serial("Edit laboratory workflow", () => {
421421

422422
// Assert an alert is shown
423423
await expect(
424-
page.getByText("The markdown block has been deleted successfully")
424+
page.getByText("The block has been deleted successfully")
425425
).toBeVisible();
426426

427427
// Assert the block is not shown
@@ -443,7 +443,7 @@ test.describe.serial("Edit laboratory workflow", () => {
443443

444444
// Assert an alert is shown
445445
await expect(
446-
page.getByText("The test block has been deleted successfully")
446+
page.getByText("The block has been deleted successfully")
447447
).toBeVisible();
448448

449449
// Assert the block is not shown

src/screens/edit-laboratory/components/markdown-block/MarkdownBlockDropDown.tsx

Lines changed: 68 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { EditLaboratoryContext } from "@/context/laboratories/EditLaboratoryContext";
22
import { EditLaboratoryActionType } from "@/hooks/laboratories/editLaboratoryTypes";
3-
import { deleteMarkdownBlockService } from "@/services/blocks/delete-markdown-block.service";
43
import { swapBlocksIndexService } from "@/services/blocks/swap-blocks-index.service";
54
import { updateMarkdownBlockContentService } from "@/services/blocks/update-markdown-block-content.service";
65
import {
@@ -9,7 +8,7 @@ import {
98
} from "@/types/entities/laboratory-entities";
109
import { useMutation, useQueryClient } from "@tanstack/react-query";
1110
import { ArrowDown, ArrowUp, MoreVertical, Save, Trash2 } from "lucide-react";
12-
import { useContext } from "react";
11+
import { useContext, useState } from "react";
1312
import { toast } from "sonner";
1413

1514
import {
@@ -20,6 +19,7 @@ import {
2019
DropdownMenuSeparator,
2120
DropdownMenuTrigger
2221
} from "../../../../components/ui/dropdown-menu";
22+
import { DeleteBlockDialog } from "../../dialogs/DeleteBlockDialog";
2323

2424
interface MarkdownBlockDropDown {
2525
blockUUID: string;
@@ -40,6 +40,9 @@ export const MarkdownBlockDropDown = ({
4040
(b) => b.uuid === blockUUID
4141
) as MarkdownBlock;
4242

43+
// Delete block dialog state
44+
const [isDeleteBlockDialogOpen, setIsDeleteBlockDialogOpen] = useState(false);
45+
4346
// Update markdown block mutation
4447
const queryClient = useQueryClient();
4548
const { mutate: updateMarkdownBlockMutation } = useMutation({
@@ -71,39 +74,8 @@ export const MarkdownBlockDropDown = ({
7174
}
7275
});
7376

74-
// Delete markdown block mutation
75-
const { mutate: deleteMarkdownBlockMutation } = useMutation({
76-
mutationFn: deleteMarkdownBlockService,
77-
onError: (error) => {
78-
toast.error(error.message);
79-
},
80-
onSuccess: () => {
81-
// Update global state
82-
laboratoryStateDispatcher({
83-
type: EditLaboratoryActionType.DELETE_BLOCK,
84-
payload: {
85-
uuid: blockUUID
86-
}
87-
});
88-
89-
// Show a success message
90-
toast.success("The markdown block has been deleted successfully");
91-
92-
// Update the laboratory query
93-
queryClient.setQueryData(
94-
["laboratory", laboratory!.uuid],
95-
(oldData: Laboratory) => {
96-
return {
97-
...oldData,
98-
blocks: oldData.blocks.filter((b) => b.uuid !== blockUUID)
99-
};
100-
}
101-
);
102-
}
103-
});
104-
10577
// Move markdown block up mutation
106-
const { mutate: moteTestBlockUpMutation } = useMutation({
78+
const { mutate: moveTestBlockUpMutation } = useMutation({
10779
mutationFn: swapBlocksIndexService,
10880
onError: (error) => {
10981
toast.error(error.message);
@@ -142,7 +114,7 @@ export const MarkdownBlockDropDown = ({
142114
});
143115

144116
// Move markdown block down mutation
145-
const { mutate: moteTestBlockDownMutation } = useMutation({
117+
const { mutate: moveTestBlockDownMutation } = useMutation({
146118
mutationFn: swapBlocksIndexService,
147119
onError: (error) => {
148120
toast.error(error.message);
@@ -181,60 +153,66 @@ export const MarkdownBlockDropDown = ({
181153
});
182154

183155
return (
184-
<DropdownMenu>
185-
<DropdownMenuTrigger asChild>
186-
<button
187-
className="h-min px-2"
188-
aria-label={`Toggle options for block number ${blockIndex + 1}`}
189-
>
190-
<MoreVertical />
191-
</button>
192-
</DropdownMenuTrigger>
193-
<DropdownMenuContent>
194-
<DropdownMenuLabel>Block options</DropdownMenuLabel>
195-
<DropdownMenuSeparator />
196-
<DropdownMenuItem
197-
disabled={blockIndex === 0}
198-
onClick={() =>
199-
moteTestBlockUpMutation({
200-
first_block_uuid: laboratory!.blocks[blockIndex - 1].uuid,
201-
second_block_uuid: blockUUID
202-
})
203-
}
204-
>
205-
<ArrowUp className="mr-2 aspect-square h-5" />
206-
Move up
207-
</DropdownMenuItem>
208-
<DropdownMenuItem
209-
disabled={blockIndex === laboratory!.blocks.length - 1}
210-
onClick={() =>
211-
moteTestBlockDownMutation({
212-
first_block_uuid: blockUUID,
213-
second_block_uuid: laboratory!.blocks[blockIndex + 1].uuid
214-
})
215-
}
216-
>
217-
<ArrowDown className="mr-2 aspect-square h-5" />
218-
Move down
219-
</DropdownMenuItem>
220-
<DropdownMenuItem
221-
onClick={() =>
222-
updateMarkdownBlockMutation({
223-
markdownBlockUUID: blockUUID,
224-
content: block.content
225-
})
226-
}
227-
>
228-
<Save className="mr-2 aspect-square h-5" />
229-
Save changes
230-
</DropdownMenuItem>
231-
<DropdownMenuItem
232-
onClick={() => deleteMarkdownBlockMutation(blockUUID)}
233-
>
234-
<Trash2 className="mr-2 aspect-square h-5" />
235-
Delete block
236-
</DropdownMenuItem>
237-
</DropdownMenuContent>
238-
</DropdownMenu>
156+
<>
157+
<DropdownMenu>
158+
<DropdownMenuTrigger asChild>
159+
<button
160+
className="h-min px-2"
161+
aria-label={`Toggle options for block number ${blockIndex + 1}`}
162+
>
163+
<MoreVertical />
164+
</button>
165+
</DropdownMenuTrigger>
166+
<DropdownMenuContent>
167+
<DropdownMenuLabel>Block options</DropdownMenuLabel>
168+
<DropdownMenuSeparator />
169+
<DropdownMenuItem
170+
disabled={blockIndex === 0}
171+
onClick={() =>
172+
moveTestBlockUpMutation({
173+
first_block_uuid: laboratory!.blocks[blockIndex - 1].uuid,
174+
second_block_uuid: blockUUID
175+
})
176+
}
177+
>
178+
<ArrowUp className="mr-2 aspect-square h-5" />
179+
Move up
180+
</DropdownMenuItem>
181+
<DropdownMenuItem
182+
disabled={blockIndex === laboratory!.blocks.length - 1}
183+
onClick={() =>
184+
moveTestBlockDownMutation({
185+
first_block_uuid: blockUUID,
186+
second_block_uuid: laboratory!.blocks[blockIndex + 1].uuid
187+
})
188+
}
189+
>
190+
<ArrowDown className="mr-2 aspect-square h-5" />
191+
Move down
192+
</DropdownMenuItem>
193+
<DropdownMenuItem
194+
onClick={() =>
195+
updateMarkdownBlockMutation({
196+
markdownBlockUUID: blockUUID,
197+
content: block.content
198+
})
199+
}
200+
>
201+
<Save className="mr-2 aspect-square h-5" />
202+
Save changes
203+
</DropdownMenuItem>
204+
<DropdownMenuItem onClick={() => setIsDeleteBlockDialogOpen(true)}>
205+
<Trash2 className="mr-2 aspect-square h-5" />
206+
Delete block
207+
</DropdownMenuItem>
208+
</DropdownMenuContent>
209+
</DropdownMenu>
210+
<DeleteBlockDialog
211+
blockType="markdown"
212+
blockUUID={blockUUID}
213+
isDialogOpen={isDeleteBlockDialogOpen}
214+
setIsDialogOpen={setIsDeleteBlockDialogOpen}
215+
/>
216+
</>
239217
);
240218
};

0 commit comments

Comments
 (0)