fix: user management & profit chart
This commit is contained in:
@@ -1,27 +1,52 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { CustomerRequest } from '../../types/services/customer'
|
||||
import { api } from '../api'
|
||||
import { User } from '../../types/services/user'
|
||||
import { toast } from 'react-toastify'
|
||||
|
||||
type CreateUserPayload = {
|
||||
name: string
|
||||
email: string
|
||||
}
|
||||
|
||||
const useUsersMutation = () => {
|
||||
export const useCustomersMutation = () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const createUser = useMutation<User, Error, CreateUserPayload>({
|
||||
mutationFn: async newUser => {
|
||||
const response = await api.post('/users', newUser)
|
||||
|
||||
const createCustomer = useMutation({
|
||||
mutationFn: async (newCustomer: CustomerRequest) => {
|
||||
const response = await api.post('/customers', newCustomer)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
// Optional: refetch 'users' list after success
|
||||
queryClient.invalidateQueries({ queryKey: ['users'] })
|
||||
toast.success('Customer created successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['customers'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||||
}
|
||||
})
|
||||
|
||||
return { createUser }
|
||||
}
|
||||
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')
|
||||
}
|
||||
})
|
||||
|
||||
export default useUsersMutation
|
||||
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 }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Orders } from '../../types/services/order'
|
||||
import { Order, Orders } from '../../types/services/order'
|
||||
import { api } from '../api'
|
||||
|
||||
interface OrdersQueryParams {
|
||||
@@ -34,3 +34,13 @@ export function useOrders(params: OrdersQueryParams = {}) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function useOrder(id: string) {
|
||||
return useQuery<Order>({
|
||||
queryKey: ['orders', id],
|
||||
queryFn: async () => {
|
||||
const res = await api.get(`/orders/${id}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,14 +1,36 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { Users } from '../../types/services/user'
|
||||
import { api } from '../api'
|
||||
import { User } from '../../types/services/user'
|
||||
|
||||
interface UsersQueryParams {
|
||||
page?: number
|
||||
limit?: number
|
||||
search?: string
|
||||
}
|
||||
|
||||
export function useUsers() {
|
||||
return useQuery<User[]>({
|
||||
queryKey: ['users'],
|
||||
export function useUsers(params: UsersQueryParams = {}) {
|
||||
const { page = 1, limit = 10, search = '', ...filters } = params
|
||||
|
||||
return useQuery<Users>({
|
||||
queryKey: ['users', { page, limit, search, ...filters }],
|
||||
queryFn: async () => {
|
||||
const res = await api.get('/users')
|
||||
return res.data
|
||||
},
|
||||
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(`/users?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user