fix: re stock

This commit is contained in:
ferdiansyah783
2025-08-14 03:09:00 +07:00
parent 1d5ff78fc2
commit 274cecd1cb
12 changed files with 718 additions and 395 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
import axios from 'axios'
import { toast } from 'react-toastify'
const getToken = () => {
return localStorage.getItem('authToken')
@@ -40,7 +41,7 @@ api.interceptors.response.use(
}
if (status >= 500) {
console.error('Server error:', error.response?.data?.message || 'Terjadi kesalahan server.')
toast.error(error.response?.data?.errors[0].cause || 'Terjadi kesalahan server.')
}
return Promise.reject(error)
+20 -2
View File
@@ -1,7 +1,7 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'
import { api } from '../api'
import { toast } from 'react-toastify'
import { InventoryAdjustRequest, InventoryRequest } from '../../types/services/inventory'
import { InventoryAdjustRequest, InventoryRequest, InventoryRestockRequest } from '../../types/services/inventory'
export const useInventoriesMutation = () => {
const queryClient = useQueryClient()
@@ -34,6 +34,24 @@ export const useInventoriesMutation = () => {
}
})
const restockInventory = useMutation({
mutationFn: async (newInventory: InventoryRestockRequest) => {
newInventory.items.map(item => {
item.quantity = Number(item.quantity)
})
const response = await api.post('/inventory/restock', newInventory)
return response.data
},
onSuccess: () => {
toast.success('Inventory restock successfully!')
queryClient.invalidateQueries({ queryKey: ['inventories'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
}
})
const deleteInventory = useMutation({
mutationFn: async (id: string) => {
const response = await api.delete(`/inventory/${id}`)
@@ -48,5 +66,5 @@ export const useInventoriesMutation = () => {
}
})
return { createInventory, adjustInventory, deleteInventory }
return { createInventory, adjustInventory, deleteInventory, restockInventory }
}