feat: product recipes
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { toast } from 'react-toastify'
|
||||
import { ProductRecipeRequest } from '../../types/services/productRecipe'
|
||||
import { api } from '../api'
|
||||
|
||||
export const useProductRecipesMutation = () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const createProductRecipe = useMutation({
|
||||
mutationFn: async (newProductRecipe: ProductRecipeRequest) => {
|
||||
const { variant_id, ...rest } = newProductRecipe
|
||||
|
||||
const cleanRequest = variant_id ? newProductRecipe : rest
|
||||
|
||||
const response = await api.post('/product-recipes', cleanRequest)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Product Recipe created successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['product-recipes'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||||
}
|
||||
})
|
||||
|
||||
const updateProductRecipe = useMutation({
|
||||
mutationFn: async ({ id, payload }: { id: string; payload: ProductRecipeRequest }) => {
|
||||
const response = await api.put(`/product-recipes/${id}`, payload)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Product Recipe updated successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['product-recipes'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
createProductRecipe,
|
||||
updateProductRecipe
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { ProductRecipe } from '../../types/services/productRecipe'
|
||||
import { api } from '../api'
|
||||
|
||||
export function useProductRecipesByProduct(productId: string) {
|
||||
return useQuery<ProductRecipe[]>({
|
||||
queryKey: ['product-recipes', productId],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/product-recipes/product/${productId}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Products } from '../../types/services/product'
|
||||
import { Product, Products } from '../../types/services/product'
|
||||
import { api } from '../api'
|
||||
import { ProductRecipe } from '../../types/services/productRecipe'
|
||||
|
||||
interface ProductsQueryParams {
|
||||
page?: number
|
||||
@@ -39,7 +40,7 @@ export function useProducts(params: ProductsQueryParams = {}) {
|
||||
}
|
||||
|
||||
export function useProductById(id: string) {
|
||||
return useQuery({
|
||||
return useQuery<Product>({
|
||||
queryKey: ['product', id],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/products/${id}`)
|
||||
|
||||
Reference in New Issue
Block a user