Skip to content

Commit 895619b

Browse files
Use ApiClient
1 parent d2c6646 commit 895619b

2 files changed

Lines changed: 21 additions & 41 deletions

File tree

‎src/components/ItemRequestTable.tsx‎

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { UnallocatedItemData } from "@/screens/AdminUnallocatedItemsScreen";
22
import MultiSelectDropdown from "./MultiSelectDropdown";
33
import { useState } from "react";
44
import toast from "react-hot-toast";
5+
import { useApiClient } from "@/hooks/useApiClient";
56

67
export 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) => {

‎src/screens/AdminUnallocatedItemsScreen.tsx‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import AdvancedBaseTable, {
1010
FilterList,
1111
} from "@/components/baseTable/AdvancedBaseTable";
1212
import ItemRequestTable from "@/components/ItemRequestTable";
13+
import { useApiClient } from "@/hooks/useApiClient";
1314

1415
export interface UnallocatedItemData {
1516
id: number;
@@ -46,6 +47,8 @@ export default function AdminUnallocatedItemsScreen() {
4647

4748
const tableRef = useRef<AdvancedBaseTableHandle<UnallocatedItemData>>(null);
4849

50+
const { apiClient } = useApiClient();
51+
4952
const fetchTableData = useCallback(
5053
async (
5154
pageSize: number,
@@ -58,22 +61,16 @@ export default function AdminUnallocatedItemsScreen() {
5861
filters: JSON.stringify(filters),
5962
});
6063

61-
const res = await fetch(
62-
`/api/generalItems/unallocated?${params.toString()}`,
63-
{
64-
cache: "no-store",
65-
}
66-
);
67-
68-
if (!res.ok) {
69-
throw new Error("Failed to fetch table data");
70-
}
71-
72-
const body = await res.json();
64+
const res = await apiClient.get<{
65+
items: UnallocatedItemData[];
66+
total: number;
67+
}>(`/api/generalItems/unallocated?${params.toString()}`, {
68+
cache: "no-store",
69+
});
7370

7471
return {
75-
data: body.items,
76-
total: body.items?.length ?? 0,
72+
data: res.items,
73+
total: res.items?.length ?? 0,
7774
};
7875
},
7976
[]

0 commit comments

Comments
 (0)