From 2af4c196bdef9e336c285addb4d0fbc1163e221f Mon Sep 17 00:00:00 2001 From: atrincas Date: Thu, 26 Dec 2024 09:42:10 +0100 Subject: [PATCH] Refactor delete functionality in ActionsDropdown component --- client/src/containers/my-projects/columns.tsx | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/client/src/containers/my-projects/columns.tsx b/client/src/containers/my-projects/columns.tsx index 290c1086..2d42fa6d 100644 --- a/client/src/containers/my-projects/columns.tsx +++ b/client/src/containers/my-projects/columns.tsx @@ -9,6 +9,7 @@ import { } from "@radix-ui/react-icons"; import { ACTIVITY } from "@shared/entities/activity.enum"; import { CustomProject as CustomProjectEntity } from "@shared/entities/custom-project.entity"; +import { useQueryClient } from "@tanstack/react-query"; import { Table as TableInstance, Row, ColumnDef } from "@tanstack/react-table"; import { useSession } from "next-auth/react"; @@ -16,6 +17,8 @@ import { formatCurrency } from "@/lib/format"; import { client } from "@/lib/query-client"; import { cn, getAuthHeader } from "@/lib/utils"; +import { DEFAULT_CUSTOM_PROJECTS_QUERY_KEY } from "@/app/my-projects/url-store"; + import { useFeatureFlags } from "@/hooks/use-feature-flags"; import { Badge } from "@/components/ui/badge"; @@ -45,15 +48,16 @@ const ActionsDropdown = ({ const deleteLabel = isHeader ? "Delete selection" : "Delete project"; const { data: session } = useSession(); const { toast } = useToast(); - const deleteCustomProject = useCallback( - async (id: string): Promise => { + const queryClient = useQueryClient(); + const deleteCustomProjects = useCallback( + async (ids: string[]): Promise => { try { const { status } = await client.customProjects.deleteCustomProjects.mutation({ extraHeaders: { ...getAuthHeader(session?.accessToken as string), }, - body: { ids: [id] }, + body: { ids }, }); return status === 200; @@ -65,41 +69,44 @@ const ActionsDropdown = ({ ); const handleDelete = async () => { - const results: boolean[] = []; + let ids: string[] = []; if (isHeader) { const selectedRows = ( instance as TableInstance ).getSelectedRowModel().rows; - for (const row of selectedRows) { - const result = await deleteCustomProject(row.original.id as string); - results.push(result); - } + ids = selectedRows.map((row) => row.original.id as string); } else if (instance.original.id) { - const result = await deleteCustomProject(instance.original.id); - results.push(result); + ids = [instance.original.id]; } - const successCount = results.filter(Boolean).length; - const failureCount = results.length - successCount; + const success = await deleteCustomProjects(ids); - if (successCount > 0) { + if (success) { toast({ description: - successCount === 1 + ids.length === 1 ? "Project deleted successfully" - : `${successCount} projects deleted successfully`, + : `${ids.length} projects deleted successfully`, }); - } - if (failureCount > 0) { + await queryClient.invalidateQueries({ + queryKey: DEFAULT_CUSTOM_PROJECTS_QUERY_KEY, + }); + + if (isHeader) { + (instance as TableInstance).resetRowSelection(); + } else { + (instance as Row).toggleSelected(false); + } + } else { toast({ variant: "destructive", description: - failureCount === 1 + ids.length === 1 ? "Failed to delete project" - : `Failed to delete ${failureCount} projects`, + : `Failed to delete ${ids.length} projects`, }); } };