Forráskód Böngészése

feat: implement delete post

Mohammad Mahdi Salimi 1 hónapja
szülő
commit
52ce6573e3

+ 16 - 10
src/features/posts/components/posts-delete-dialog.tsx

@@ -2,6 +2,8 @@ import { AlertTriangle } from 'lucide-react'
 import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'
 import { ConfirmDialog } from '@/components/confirm-dialog'
 import { type Post } from '@/types/posts.types'
+import { useDeletePost } from '@/hooks/posts/useDeletePost'
+import { toast } from 'sonner'
 
 interface Props {
   open: boolean
@@ -10,23 +12,26 @@ interface Props {
 }
 
 export function PostsDeleteDialog({ open, onOpenChange, currentRow }: Props) {
-  // const { mutate, isPending } = useDeleteUser()
+  const { mutate, isPending } = useDeletePost()
 
   if (!currentRow) return null
 
-  // const handleDelete = () => {
-  //   mutate(currentRow.id, {
-  //     onSuccess: () => {
-  //       onOpenChange(false)
-  //     },
-  //   })
-  // }
+  const handleDelete = () => {
+    mutate(
+      { id: currentRow.id },
+      {
+        onSuccess: () => {
+          onOpenChange(false)
+          toast.success('پست مورد نظر با موفقیت حذف شد!')
+        },
+      }
+    )
+  }
 
   return (
     <ConfirmDialog
       open={open}
       onOpenChange={onOpenChange}
-      form='posts-delete-form'
       title={
         <span className='text-destructive'>
           <AlertTriangle
@@ -46,8 +51,9 @@ export function PostsDeleteDialog({ open, onOpenChange, currentRow }: Props) {
       }
       confirmText='حذف'
       cancelBtnText='انصراف'
-      // isLoading={isPending}
+      isLoading={isPending}
       destructive
+      handleConfirm={handleDelete}
     />
   )
 }

+ 21 - 0
src/hooks/posts/useDeletePost.tsx

@@ -0,0 +1,21 @@
+import { queryKeys } from '@/core/react-query/keys'
+import { postsService } from '@/services/posts.service'
+import { useMutation, useQueryClient } from '@tanstack/react-query'
+
+type DeletePostVariables = {
+  id: number | string
+}
+
+export function useDeletePost() {
+  const queryClient = useQueryClient()
+
+  return useMutation({
+    mutationFn: ({ id }: DeletePostVariables) => postsService.delete(id),
+
+    onSuccess: () => {
+      queryClient.invalidateQueries({
+        queryKey: queryKeys.posts.lists(),
+      })
+    },
+  })
+}