diff --git a/src/components/TrashWithConfrim.tsx b/src/components/TrashWithConfrim.tsx index 1a712e9..63b5d6a 100644 --- a/src/components/TrashWithConfrim.tsx +++ b/src/components/TrashWithConfrim.tsx @@ -4,17 +4,18 @@ import ModalConfrim from './ModalConfrim' interface Props { onDelete: () => void, - isloading?: boolean + isloading?: boolean, + colorIcon?: string, } -const TrashWithConfrim: FC = ({ onDelete, isloading = false }) => { +const TrashWithConfrim: FC = ({ onDelete, isloading = false, colorIcon }) => { const [isConfirm, setIsConfirm] = useState(false) return (
setIsConfirm(true)} className='size-5' - color='#888' + color={colorIcon || '#888'} /> { - const { data: products } = useGetProducts() + const { data: products, refetch } = useGetProducts() + const { mutate: deleteProduct, isPending: isDeleting } = useDeleteProduct() + + const handleDelete = (id: number) => { + deleteProduct(id, { + onSuccess: () => { + refetch() + toast.success('با موفقیت حذف شد') + }, + onError: (error) => { + toast.error(extractErrorMessage(error)) + } + }) + } return (
@@ -80,9 +96,16 @@ const ProductList: FC = () => { title: '', render: (item) => { return ( - - - +
+ + + + handleDelete(item.id)} + isloading={isDeleting} + colorIcon='red' + /> +
) } }, diff --git a/src/pages/product/hooks/useProductData.ts b/src/pages/product/hooks/useProductData.ts index c2fa88c..d645611 100644 --- a/src/pages/product/hooks/useProductData.ts +++ b/src/pages/product/hooks/useProductData.ts @@ -80,3 +80,9 @@ export const useUpdateProduct = () => { }, }); }; + +export const useDeleteProduct = () => { + return useMutation({ + mutationFn: (id: number) => api.deleteProduct(id), + }); +}; diff --git a/src/pages/product/service/ProductService.ts b/src/pages/product/service/ProductService.ts index 9223058..77f65ef 100644 --- a/src/pages/product/service/ProductService.ts +++ b/src/pages/product/service/ProductService.ts @@ -44,4 +44,9 @@ export const createProduct = async (params:CreateProductType) => { export const updateProduct = async (id: number, params:CreateProductType) => { const { data } = await axios.patch(`/admin/products/${id}`, params); return data; +}; + +export const deleteProduct = async (id: number) => { + const { data } = await axios.delete(`/admin/products/${id}`); + return data; }; \ No newline at end of file