feat: refacotor ui

This commit is contained in:
ferdiansyah783
2025-08-08 01:49:00 +07:00
parent 687f59a9fa
commit 7beee4c3a1
19 changed files with 196 additions and 91 deletions
+52
View File
@@ -0,0 +1,52 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { CustomerRequest } from '../../types/services/customer'
import { api } from '../api'
import { toast } from 'react-toastify'
export const useIngredientsMutation = () => {
const queryClient = useQueryClient()
const createCustomer = useMutation({
mutationFn: async (newCustomer: CustomerRequest) => {
const response = await api.post('/customers', newCustomer)
return response.data
},
onSuccess: () => {
toast.success('Customer created successfully!')
queryClient.invalidateQueries({ queryKey: ['customers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
}
})
const updateCustomer = useMutation({
mutationFn: async ({ id, payload }: { id: string; payload: CustomerRequest }) => {
const response = await api.put(`/customers/${id}`, payload)
return response.data
},
onSuccess: () => {
toast.success('Customer updated successfully!')
queryClient.invalidateQueries({ queryKey: ['customers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
}
})
const deleteCustomer = useMutation({
mutationFn: async (id: string) => {
const response = await api.delete(`/customers/${id}`)
return response.data
},
onSuccess: () => {
toast.success('Customer deleted successfully!')
queryClient.invalidateQueries({ queryKey: ['customers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
}
})
return { createCustomer, updateCustomer, deleteCustomer }
}
+36
View File
@@ -0,0 +1,36 @@
import { useQuery } from '@tanstack/react-query'
import { Ingredients } from '../../types/services/ingredient'
import { api } from '../api'
interface IngredientsQueryParams {
page?: number
limit?: number
search?: string
}
export function useIngredients(params: IngredientsQueryParams = {}) {
const { page = 1, limit = 10, search = '', ...filters } = params
return useQuery<Ingredients>({
queryKey: ['ingredients', { page, limit, search, ...filters }],
queryFn: async () => {
const queryParams = new URLSearchParams()
queryParams.append('page', page.toString())
queryParams.append('limit', limit.toString())
if (search) {
queryParams.append('search', search)
}
Object.entries(filters).forEach(([key, value]) => {
if (value !== undefined && value !== null && value !== '') {
queryParams.append(key, value.toString())
}
})
const res = await api.get(`/ingredients?${queryParams.toString()}`)
return res.data.data
}
})
}