189 lines
4.2 KiB
TypeScript
189 lines
4.2 KiB
TypeScript
import {
|
|
QueryClient,
|
|
useMutation,
|
|
useQuery,
|
|
useQueryClient,
|
|
} from "@tanstack/react-query";
|
|
import * as api from "../service/ProductService";
|
|
import type {
|
|
CreateAttributeType,
|
|
CreateAttributeValueType,
|
|
CreateCategoryType,
|
|
CreateProductType,
|
|
} from "../types/Types";
|
|
|
|
export const useGetCategory = () => {
|
|
return useQuery({
|
|
queryKey: ["category"],
|
|
queryFn: api.getCategory,
|
|
});
|
|
};
|
|
|
|
export const useCreateCategory = () => {
|
|
return useMutation({
|
|
mutationFn: api.createCategory,
|
|
});
|
|
};
|
|
|
|
export const useDeleteCategory = () => {
|
|
return useMutation({
|
|
mutationFn: (id: string) => api.deleteCategory(id),
|
|
});
|
|
};
|
|
|
|
export const useUpdateCategory = () => {
|
|
return useMutation({
|
|
mutationFn: ({ id, params }: { id: string; params: CreateCategoryType }) =>
|
|
api.updateCategory(id, params),
|
|
});
|
|
};
|
|
|
|
export const useGetCategoryDetail = (id: string) => {
|
|
return useQuery({
|
|
queryKey: ["category", id],
|
|
queryFn: () => api.getCategoryDetail(id),
|
|
enabled: !!id,
|
|
});
|
|
};
|
|
|
|
export const useGetProducts = () => {
|
|
return useQuery({
|
|
queryKey: ["products"],
|
|
queryFn: api.getProducts,
|
|
});
|
|
};
|
|
|
|
export const useCreateProduct = () => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: api.createProduct,
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: ["products"],
|
|
});
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useGetProductDetail = (id: string) => {
|
|
return useQuery({
|
|
queryKey: ["product", id],
|
|
queryFn: () => api.getProductDetail(id),
|
|
enabled: !!id,
|
|
});
|
|
};
|
|
|
|
export const useUpdateProduct = () => {
|
|
const queryClient = new QueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, params }: { id: string; params: CreateProductType }) =>
|
|
api.updateProduct(id, params),
|
|
onSuccess: () => {
|
|
queryClient.refetchQueries({
|
|
queryKey: ["products"],
|
|
});
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useDeleteProduct = () => {
|
|
return useMutation({
|
|
mutationFn: (id: number) => api.deleteProduct(id),
|
|
});
|
|
};
|
|
|
|
export const useCreateAttribute = () => {
|
|
const queryClient = new QueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({ id, params }: { id: number; params: CreateAttributeType }) =>
|
|
api.createAttribute(id, params),
|
|
onSuccess: (_, params) => {
|
|
queryClient.refetchQueries({
|
|
queryKey: ["product-attributes", params.id],
|
|
});
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useGetAttributes = (id: number) => {
|
|
return useQuery({
|
|
queryKey: ["product-attributes", id],
|
|
queryFn: () => api.getAttributes(id),
|
|
enabled: !!id,
|
|
});
|
|
};
|
|
|
|
export const useGetAttributeDetail = (id: number) => {
|
|
return useQuery({
|
|
queryKey: ["product-attribute", id],
|
|
queryFn: () => api.getAttributeDetail(id),
|
|
enabled: !!id,
|
|
});
|
|
};
|
|
|
|
export const useUpdateAttribute = () => {
|
|
const queryClient = new QueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, params }: { id: number; params: CreateAttributeType }) =>
|
|
api.updateAttribute(id, params),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: ["product-attributes"],
|
|
});
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useDeleteAttribute = () => {
|
|
return useMutation({
|
|
mutationFn: (id: number) => api.deleteAttributes(id),
|
|
});
|
|
};
|
|
|
|
export const useGetAttributeValues = (id: number) => {
|
|
return useQuery({
|
|
queryKey: ["attrbute-values", id],
|
|
queryFn: () => api.getAttributeValues(id),
|
|
enabled: !!id,
|
|
});
|
|
};
|
|
|
|
export const useCreateAttributeValue = () => {
|
|
return useMutation({
|
|
mutationFn: ({
|
|
id,
|
|
params,
|
|
}: {
|
|
id: number;
|
|
params: CreateAttributeValueType;
|
|
}) => api.createAttributeValues(id, params),
|
|
});
|
|
};
|
|
|
|
export const useUpdateAttributeValue = () => {
|
|
return useMutation({
|
|
mutationFn: ({
|
|
id,
|
|
params,
|
|
}: {
|
|
id: number;
|
|
params: CreateAttributeValueType;
|
|
}) => api.updateAttributeValues(id, params),
|
|
});
|
|
};
|
|
|
|
export const useGetAttributeValueDetail = (id: number, enabled: boolean) => {
|
|
return useQuery({
|
|
queryKey: ["attrbute-values-detail", id],
|
|
queryFn: () => api.getAttributeValueDetail(id),
|
|
enabled: !!id && enabled,
|
|
});
|
|
};
|
|
|
|
export const useDeleteAttributeValue = () => {
|
|
return useMutation({
|
|
mutationFn: (id: number) => api.deleteAttributeValue(id),
|
|
});
|
|
};
|