feat: payment & customer

This commit is contained in:
ferdiansyah783
2025-08-07 14:31:42 +07:00
parent a72a64215a
commit a5d22db27b
35 changed files with 2033 additions and 599 deletions
+52
View File
@@ -0,0 +1,52 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { api } from '../api'
import { toast } from 'react-toastify'
import { PaymentMethodRequest } from '../../types/services/paymentMethod'
export const usePaymentMethodsMutation = () => {
const queryClient = useQueryClient()
const createPaymentMethod = useMutation({
mutationFn: async (newPaymentMethod: PaymentMethodRequest) => {
const response = await api.post('/payment-methods', newPaymentMethod)
return response.data
},
onSuccess: () => {
toast.success('PaymentMethod created successfully!')
queryClient.invalidateQueries({ queryKey: ['payment-methods'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
}
})
const updatePaymentMethod = useMutation({
mutationFn: async ({ id, payload }: { id: string; payload: PaymentMethodRequest }) => {
const response = await api.put(`/payment-methods/${id}`, payload)
return response.data
},
onSuccess: () => {
toast.success('PaymentMethod updated successfully!')
queryClient.invalidateQueries({ queryKey: ['payment-methods'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
}
})
const deletePaymentMethod = useMutation({
mutationFn: async (id: string) => {
const response = await api.delete(`/payment-methods/${id}`)
return response.data
},
onSuccess: () => {
toast.success('PaymentMethod deleted successfully!')
queryClient.invalidateQueries({ queryKey: ['payment-methods'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
}
})
return { createPaymentMethod, updatePaymentMethod, deletePaymentMethod }
}
+2
View File
@@ -13,6 +13,7 @@ export const useProductsMutation = () => {
},
onSuccess: () => {
toast.success('Product created successfully!')
queryClient.invalidateQueries({ queryKey: ['products'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
@@ -26,6 +27,7 @@ export const useProductsMutation = () => {
},
onSuccess: () => {
toast.success('Product updated successfully!')
queryClient.invalidateQueries({ queryKey: ['products'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
+36
View File
@@ -0,0 +1,36 @@
import { useQuery } from '@tanstack/react-query'
import { PaymentMethods } from '../../types/services/paymentMethod'
import { api } from '../api'
interface PaymentMethodsQueryParams {
page?: number
limit?: number
search?: string
}
export function usePaymentMethods(params: PaymentMethodsQueryParams = {}) {
const { page = 1, limit = 10, search = '', ...filters } = params
return useQuery<PaymentMethods>({
queryKey: ['payment-methods', { 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(`/payment-methods?${queryParams.toString()}`)
return res.data.data
}
})
}