23 lines
750 B
TypeScript
23 lines
750 B
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|||
|
|
import { toast } from 'react-toastify'
|
||
|
|
import { api } from '../api'
|
||
|
|
import { PurchaseOrderRequest } from '@/types/services/purchaseOrder'
|
||
|
|
|
||
|
|
export const useVendorsMutation = () => {
|
||
|
|
const queryClient = useQueryClient()
|
||
|
|
|
||
|
|
const createVendor = useMutation({
|
||
|
|
mutationFn: async (newVendor: PurchaseOrderRequest) => {
|
||
|
|
const response = await api.post('/vendors', newVendor)
|
||
|
|
return response.data
|
||
|
|
},
|
||
|
|
onSuccess: () => {
|
||
|
|
toast.success('Vendor created successfully!')
|
||
|
|
queryClient.invalidateQueries({ queryKey: ['vendors'] })
|
||
|
|
},
|
||
|
|
onError: (error: any) => {
|
||
|
|
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|