@@ -2,6 +2,7 @@ import { UnallocatedItemData } from "@/screens/AdminUnallocatedItemsScreen";
22import MultiSelectDropdown from "./MultiSelectDropdown" ;
33import { useState } from "react" ;
44import toast from "react-hot-toast" ;
5+ import { useApiClient } from "@/hooks/useApiClient" ;
56
67export default function ItemRequestTable ( {
78 generalItemData,
@@ -61,32 +62,25 @@ function ItemRequestTableRow({
6162 } ;
6263 } > ( { } ) ;
6364
65+ const { apiClient } = useApiClient ( ) ;
66+
6467 async function allocateItems ( lineItemIds : number [ ] ) {
6568 if ( lineItemIds . length === 0 ) return ;
6669
67- const response = await fetch ( "/api/allocations" , {
68- method : "POST" ,
69- headers : {
70- "Content-Type" : "application/json" ,
71- } ,
70+ const response = await apiClient . post < {
71+ allocations : { id : number ; itemId : number ; distributionId : number } [ ] ;
72+ } > ( "/api/allocations" , {
7273 body : JSON . stringify ( {
7374 partnerId : request . partnerId ,
7475 allocations : lineItemIds ,
7576 } ) ,
7677 } ) ;
7778
78- const data = await response . json ( ) ;
79-
80- if ( ! response . ok ) {
81- toast . error ( data . message ) ;
82- throw new Error ( data . message ) ;
83- }
84-
85- console . log ( "Allocation successful:" , data ) ;
79+ console . log ( "Allocation successful:" , response ) ;
8680 toast . success ( "Items allocated successfully!" ) ;
8781
8882 setAllocations ( ( prev ) => {
89- for ( const allocation of data . allocations ) {
83+ for ( const allocation of response . allocations ) {
9084 prev [ allocation . itemId ] = {
9185 allocationId : allocation . id ,
9286 distributionId : allocation . distributionId ,
@@ -110,27 +104,16 @@ function ItemRequestTableRow({
110104 ) ;
111105 }
112106
113- const response = await fetch (
107+ const response = await apiClient . delete (
114108 `/api/distributions/${ distributionIds . values ( ) . next ( ) . value } /allocations` ,
115109 {
116- method : "DELETE" ,
117- headers : {
118- "Content-Type" : "application/json" ,
119- } ,
120110 body : JSON . stringify ( {
121111 allocations : allocationsToDelete . map ( ( a ) => a . allocationId ) ,
122112 } ) ,
123113 }
124114 ) ;
125115
126- const data = await response . json ( ) ;
127-
128- if ( ! response . ok ) {
129- toast . error ( data . message ) ;
130- throw new Error ( data . message ) ;
131- }
132-
133- console . log ( "Unallocation successful:" , data ) ;
116+ console . log ( "Unallocation successful:" , response ) ;
134117 toast . success ( "Items unallocated successfully!" ) ;
135118
136119 setAllocations ( ( prev ) => {
0 commit comments