feat: rename UpdateInventoryItemInput to EditInventoryItemInput and enhance error handling in inventory update logic

This commit is contained in:
THIS ONE IS A LITTLE BIT TRICKY KRUB 2025-04-03 22:39:14 +07:00
parent 63d38fb99d
commit 7b70f9ef4b
4 changed files with 43 additions and 17 deletions

View File

@ -4,7 +4,7 @@ import type {
InventoryStatus,
InventoryItemCategory,
CreateInventoryItemInput,
UpdateInventoryItemInput,
EditInventoryItemInput,
} from "@/types";
import { AxiosError } from "axios";
@ -122,16 +122,40 @@ export async function deleteInventoryItem(id: string) {
}
export async function updateInventoryItem(
id: string,
item: UpdateInventoryItemInput
item: EditInventoryItemInput
) {
// console.log(id);
try {
const response = await axiosInstance.put<InventoryItem>(
"/inventory/" + id,
`/inventory/${id}`,
item
);
return response.data;
} catch (error) {
console.error("Error while updating Inventory Item! " + error);
throw new Error("Failed to updating inventory item: " + error);
} catch (error: unknown) {
// Cast error to AxiosError to safely access response properties
if (error instanceof AxiosError && error.response) {
// Log the detailed error message
console.error("Error while deleting Inventory Item!");
console.error("Response Status:", error.response.status); // e.g., 422
console.error("Error Detail:", error.response.data?.detail); // Custom error message from backend
console.error("Full Error Response:", error.response.data); // Entire error object (including details)
// Throw a new error with a more specific message
throw new Error(
`Failed to delete inventory item: ${
error.response.data?.detail || error.message
}`
);
} else {
// Handle other errors (e.g., network errors or unknown errors)
console.error(
"Error while deleting Inventory Item, unknown error:",
error
);
throw new Error(
"Failed to delete inventory item: " +
(error instanceof Error ? error.message : "Unknown error")
);
}
}
}

View File

@ -32,6 +32,7 @@ import {
InventoryItemCategory,
HarvestUnits,
UpdateInventoryItemInput,
EditInventoryItemInput,
} from "@/types";
import { updateInventoryItem } from "@/api/inventory";
@ -46,7 +47,8 @@ export function EditInventoryItem({
fetchedInventoryCategory: InventoryItemCategory[];
fetchedHarvestUnits: HarvestUnits[];
}) {
console.table(item);
// console.table(item);
// console.log(item.id);
const [open, setOpen] = useState(false);
const [itemName, setItemName] = useState(item.name);
const [itemCategory, setItemCategory] = useState(
@ -67,8 +69,7 @@ export function EditInventoryItem({
const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: (item: UpdateInventoryItemInput) =>
updateInventoryItem(item.id, item),
mutationFn: (x: EditInventoryItemInput) => updateInventoryItem(item.id, x),
onSuccess: () => {
// invalidate queries to refresh inventory data.
queryClient.invalidateQueries({ queryKey: ["inventoryItems"] });
@ -101,17 +102,18 @@ export function EditInventoryItem({
);
return;
}
// console.log("Mutate called");
console.log(item.id);
mutation.mutate({
name: itemName,
categoryId: item.categoryId,
quantity: itemQuantity,
categoryId: item.categoryId ?? 0,
quantity: itemQuantity ?? 0,
unitId:
fetchedHarvestUnits.find((unit) => unit.name === itemUnit)?.id ?? 0,
statusId:
fetchedInventoryStatus.find((status) => status.name === itemStatus)
?.id ?? 0,
dateAdded: new Date().toISOString(),
id: "",
});
};

View File

@ -94,7 +94,7 @@ export default function InventoryPage() {
staleTime: 60 * 1000,
});
//////////////////////////////
// console.table(inventoryItems);
console.table(inventoryItems);
// console.table(inventoryStatus);
// console.table(harvestUnits);
@ -182,11 +182,11 @@ export default function InventoryPage() {
item={{
id: row.original.id,
name: row.original.name,
categoryId: row.original.categoryId,
categoryId: row.original.category.id,
quantity: row.original.quantity,
unitId: row.original.unitId,
unitId: row.original.unit.id,
dateAdded: row.original.dateAdded,
statusId: row.original.statusId,
statusId: row.original.status.id,
}}
fetchedInventoryStatus={inventoryStatus}
fetchedInventoryCategory={inventoryCategory}

View File

@ -161,7 +161,7 @@ export type CreateInventoryItemInput = {
};
// export type UpdateInventoryItemInput = CreateInventoryItemInput & {};
// export type EditInventoryItemInput = CreateInventoryItemInput & { id: number };
export type EditInventoryItemInput = CreateInventoryItemInput;
export type UpdateInventoryItemInput = Partial<CreateInventoryItemInput> & {
id: string;