delete attribute
This commit is contained in:
@@ -3,13 +3,29 @@ import { Paths } from '@/config/Paths'
|
|||||||
import { AddSquare, Edit } from 'iconsax-react'
|
import { AddSquare, Edit } from 'iconsax-react'
|
||||||
import { type FC } from 'react'
|
import { type FC } from 'react'
|
||||||
import { Link, useParams } from 'react-router-dom'
|
import { Link, useParams } from 'react-router-dom'
|
||||||
import { useGetAttributes } from '../hooks/useProductData'
|
import { useDeleteAttribute, useGetAttributes } from '../hooks/useProductData'
|
||||||
import Table from '@/components/Table'
|
import Table from '@/components/Table'
|
||||||
|
import TrashWithConfrim from '@/components/TrashWithConfrim'
|
||||||
|
import { toast } from 'react-toastify'
|
||||||
|
import { extractErrorMessage } from '@/config/func'
|
||||||
|
|
||||||
const AttributeList: FC = () => {
|
const AttributeList: FC = () => {
|
||||||
|
|
||||||
const { id } = useParams()
|
const { id } = useParams()
|
||||||
const { data } = useGetAttributes(Number(id))
|
const { mutate: deleteAttribute, isPending } = useDeleteAttribute()
|
||||||
|
const { data, refetch } = useGetAttributes(Number(id))
|
||||||
|
|
||||||
|
const handleDelete = (id: number) => {
|
||||||
|
deleteAttribute(id, {
|
||||||
|
onSuccess: () => {
|
||||||
|
refetch()
|
||||||
|
},
|
||||||
|
onError: (error) => {
|
||||||
|
toast.error(extractErrorMessage(error))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='mt-5'>
|
<div className='mt-5'>
|
||||||
@@ -62,6 +78,12 @@ const AttributeList: FC = () => {
|
|||||||
size={20}
|
size={20}
|
||||||
/>
|
/>
|
||||||
</Link>
|
</Link>
|
||||||
|
|
||||||
|
<TrashWithConfrim
|
||||||
|
isloading={isPending}
|
||||||
|
colorIcon='red'
|
||||||
|
onDelete={() => handleDelete(item.id)}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,9 +97,9 @@ export const useCreateAttribute = () => {
|
|||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: ({ id, params }: { id: number; params: CreateAttributeType }) =>
|
mutationFn: ({ id, params }: { id: number; params: CreateAttributeType }) =>
|
||||||
api.createAttribute(id, params),
|
api.createAttribute(id, params),
|
||||||
onSuccess: () => {
|
onSuccess: (_, params) => {
|
||||||
queryClient.invalidateQueries({
|
queryClient.refetchQueries({
|
||||||
queryKey: ["product-attributes"],
|
queryKey: ["product-attributes", params.id],
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -133,3 +133,9 @@ export const useUpdateAttribute = () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useDeleteAttribute = () => {
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: (id: number) => api.deleteAttributes(id),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -70,3 +70,8 @@ export const updateAttribute = async (id: number, params:CreateAttributeType) =>
|
|||||||
const { data } = await axios.patch(`/admin/products/attributes/${id}`, params);
|
const { data } = await axios.patch(`/admin/products/attributes/${id}`, params);
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deleteAttributes = async (id: number) => {
|
||||||
|
const { data } = await axios.delete(`/admin/products/attributes/${id}`);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user