Create Unit Conventer

This commit is contained in:
efrilm
2025-09-12 18:49:01 +07:00
parent 40c417ec72
commit 54c7598e7a
7 changed files with 491 additions and 260 deletions
+52
View File
@@ -0,0 +1,52 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-toastify'
import { api } from '../api'
import { IngredientUnitConverterRequest } from '@/types/services/productRecipe'
export const useUnitConventorMutation = () => {
const queryClient = useQueryClient()
const createUnitConventer = useMutation({
mutationFn: async (newUnitConventer: IngredientUnitConverterRequest) => {
const response = await api.post('/unit-converters', newUnitConventer)
return response.data
},
onSuccess: () => {
toast.success('UnitConventer created successfully!')
queryClient.invalidateQueries({ queryKey: ['unitConventers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
}
})
const updateUnitConventer = useMutation({
mutationFn: async ({ id, payload }: { id: string; payload: IngredientUnitConverterRequest }) => {
const response = await api.put(`/unit-converters/${id}`, payload)
return response.data
},
onSuccess: () => {
toast.success('UnitConventer updated successfully!')
queryClient.invalidateQueries({ queryKey: ['unit-converters'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
}
})
const deleteUnitConventer = useMutation({
mutationFn: async (id: string) => {
const response = await api.delete(`/unit-converters/${id}`)
return response.data
},
onSuccess: () => {
toast.success('UnitConventer deleted successfully!')
queryClient.invalidateQueries({ queryKey: ['unitConventers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
}
})
return { createUnitConventer, updateUnitConventer, deleteUnitConventer }
}
+11
View File
@@ -1,6 +1,7 @@
import { useQuery } from '@tanstack/react-query'
import { Ingredients } from '../../types/services/ingredient'
import { api } from '../api'
import { Ingredient } from '@/types/services/productRecipe'
interface IngredientsQueryParams {
page?: number
@@ -34,3 +35,13 @@ export function useIngredients(params: IngredientsQueryParams = {}) {
}
})
}
export function useIngredientById(id: string) {
return useQuery<Ingredient>({
queryKey: ['ingredients', id],
queryFn: async () => {
const res = await api.get(`/ingredients/${id}`)
return res.data.data
}
})
}