fix: inventory
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { api } from '../api'
|
||||
import { toast } from 'react-toastify'
|
||||
import { CategoryRequest } from '../../types/services/category'
|
||||
|
||||
export const useCategoriesMutation = {
|
||||
createCategory: () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (newCategory: CategoryRequest) => {
|
||||
const response = await api.post('/categories', newCategory)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Category created successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['categories'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
updateCategory: () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, payload }: { id: string; payload: CategoryRequest }) => {
|
||||
const response = await api.put(`/categories/${id}`, payload)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Category updated successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['categories'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
deleteCategory: () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
const response = await api.delete(`/categories/${id}`)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Category deleted successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['categories'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { api } from '../api'
|
||||
import { toast } from 'react-toastify'
|
||||
import { InventoryAdjustRequest, InventoryRequest } from '../../types/services/inventory'
|
||||
|
||||
export const useInventoriesMutation = {
|
||||
createInventory: () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (newInventory: InventoryRequest) => {
|
||||
const response = await api.post('/inventory', newInventory)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Inventory created successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['inventories'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
adjustInventory: () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (newInventory: InventoryAdjustRequest) => {
|
||||
const response = await api.post('/inventory/adjust', newInventory)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Inventory adjusted successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['inventories'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
deleteInventory: () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
const response = await api.delete(`/inventory/${id}`)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Inventory deleted successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['inventories'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { api } from '../api'
|
||||
import { toast } from 'react-toastify'
|
||||
import { ProductRequest } from '../../types/services/product'
|
||||
@@ -10,11 +10,44 @@ export const useProductsMutation = {
|
||||
const response = await api.post('/products', newProduct)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: data => {
|
||||
onSuccess: () => {
|
||||
toast.success('Product created successfully!')
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response.data.errors[0].cause)
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
updateProduct: () => {
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, payload }: { id: string; payload: ProductRequest }) => {
|
||||
const response = await api.put(`/products/${id}`, payload)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Product updated successfully!')
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
deleteProduct: () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
const response = await api.delete(`/products/${id}`)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success('Product deleted successfully!')
|
||||
queryClient.invalidateQueries({ queryKey: ['products'] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -34,8 +34,6 @@ export const useCategoriesQuery = {
|
||||
const res = await api.get(`/categories?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
},
|
||||
// Cache for 5 minutes
|
||||
staleTime: 5 * 60 * 1000
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { Inventories } from "../../types/services/inventory"
|
||||
import { api } from "../api"
|
||||
|
||||
interface InventoriesQueryParams {
|
||||
page?: number
|
||||
limit?: number
|
||||
search?: string
|
||||
}
|
||||
|
||||
export const useInventoriesQuery = {
|
||||
getInventories: (params: InventoriesQueryParams = {}) => {
|
||||
const { page = 1, limit = 10, search = '', ...filters } = params
|
||||
|
||||
return useQuery<Inventories>({
|
||||
queryKey: ['inventories', { 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)
|
||||
}
|
||||
|
||||
// Add other filters
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
queryParams.append(key, value.toString())
|
||||
}
|
||||
})
|
||||
|
||||
const res = await api.get(`/inventory?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { Orders } from "../../types/services/order"
|
||||
import { api } from "../api"
|
||||
|
||||
interface OrdersQueryParams {
|
||||
page?: number
|
||||
limit?: number
|
||||
search?: string
|
||||
}
|
||||
|
||||
export const useOrdersQuery = {
|
||||
getOrders: (params: OrdersQueryParams = {}) => {
|
||||
const { page = 1, limit = 10, search = '', ...filters } = params
|
||||
|
||||
return useQuery<Orders>({
|
||||
queryKey: ['orders', { 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)
|
||||
}
|
||||
|
||||
// Add other filters
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
queryParams.append(key, value.toString())
|
||||
}
|
||||
})
|
||||
|
||||
const res = await api.get(`/orders?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { Outlets } from "../../types/services/outlet"
|
||||
import { api } from "../api"
|
||||
|
||||
interface OutletsQueryParams {
|
||||
page?: number
|
||||
limit?: number
|
||||
search?: string
|
||||
}
|
||||
|
||||
export const useOutletsQuery = {
|
||||
getOutlets: (params: OutletsQueryParams = {}) => {
|
||||
const { page = 1, limit = 10, search = '', ...filters } = params
|
||||
|
||||
return useQuery<Outlets>({
|
||||
queryKey: ['outlets', { 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)
|
||||
}
|
||||
|
||||
// Add other filters
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
queryParams.append(key, value.toString())
|
||||
}
|
||||
})
|
||||
|
||||
const res = await api.get(`/outlets/list?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,17 @@ export const useProductsQuery = {
|
||||
const res = await api.get(`/products?${queryParams.toString()}`)
|
||||
return res.data.data
|
||||
},
|
||||
})
|
||||
},
|
||||
|
||||
getProductById: (id: string) => {
|
||||
return useQuery({
|
||||
queryKey: ['product', id],
|
||||
queryFn: async ({ queryKey: [, id] }) => {
|
||||
const res = await api.get(`/products/${id}`)
|
||||
return res.data.data
|
||||
},
|
||||
|
||||
// Cache for 5 minutes
|
||||
staleTime: 5 * 60 * 1000
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user