mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-19 22:14:08 +01:00
feat: rename UpdateInventoryItemInput to EditInventoryItemInput and enhance error handling in inventory update logic
This commit is contained in:
parent
63d38fb99d
commit
7b70f9ef4b
@ -4,7 +4,7 @@ import type {
|
|||||||
InventoryStatus,
|
InventoryStatus,
|
||||||
InventoryItemCategory,
|
InventoryItemCategory,
|
||||||
CreateInventoryItemInput,
|
CreateInventoryItemInput,
|
||||||
UpdateInventoryItemInput,
|
EditInventoryItemInput,
|
||||||
} from "@/types";
|
} from "@/types";
|
||||||
import { AxiosError } from "axios";
|
import { AxiosError } from "axios";
|
||||||
|
|
||||||
@ -122,16 +122,40 @@ export async function deleteInventoryItem(id: string) {
|
|||||||
}
|
}
|
||||||
export async function updateInventoryItem(
|
export async function updateInventoryItem(
|
||||||
id: string,
|
id: string,
|
||||||
item: UpdateInventoryItemInput
|
item: EditInventoryItemInput
|
||||||
) {
|
) {
|
||||||
|
// console.log(id);
|
||||||
try {
|
try {
|
||||||
const response = await axiosInstance.put<InventoryItem>(
|
const response = await axiosInstance.put<InventoryItem>(
|
||||||
"/inventory/" + id,
|
`/inventory/${id}`,
|
||||||
item
|
item
|
||||||
);
|
);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error: unknown) {
|
||||||
console.error("Error while updating Inventory Item! " + error);
|
// Cast error to AxiosError to safely access response properties
|
||||||
throw new Error("Failed to updating inventory item: " + error);
|
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")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,6 +32,7 @@ import {
|
|||||||
InventoryItemCategory,
|
InventoryItemCategory,
|
||||||
HarvestUnits,
|
HarvestUnits,
|
||||||
UpdateInventoryItemInput,
|
UpdateInventoryItemInput,
|
||||||
|
EditInventoryItemInput,
|
||||||
} from "@/types";
|
} from "@/types";
|
||||||
import { updateInventoryItem } from "@/api/inventory";
|
import { updateInventoryItem } from "@/api/inventory";
|
||||||
|
|
||||||
@ -46,7 +47,8 @@ export function EditInventoryItem({
|
|||||||
fetchedInventoryCategory: InventoryItemCategory[];
|
fetchedInventoryCategory: InventoryItemCategory[];
|
||||||
fetchedHarvestUnits: HarvestUnits[];
|
fetchedHarvestUnits: HarvestUnits[];
|
||||||
}) {
|
}) {
|
||||||
console.table(item);
|
// console.table(item);
|
||||||
|
// console.log(item.id);
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const [itemName, setItemName] = useState(item.name);
|
const [itemName, setItemName] = useState(item.name);
|
||||||
const [itemCategory, setItemCategory] = useState(
|
const [itemCategory, setItemCategory] = useState(
|
||||||
@ -67,8 +69,7 @@ export function EditInventoryItem({
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const mutation = useMutation({
|
const mutation = useMutation({
|
||||||
mutationFn: (item: UpdateInventoryItemInput) =>
|
mutationFn: (x: EditInventoryItemInput) => updateInventoryItem(item.id, x),
|
||||||
updateInventoryItem(item.id, item),
|
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
// invalidate queries to refresh inventory data.
|
// invalidate queries to refresh inventory data.
|
||||||
queryClient.invalidateQueries({ queryKey: ["inventoryItems"] });
|
queryClient.invalidateQueries({ queryKey: ["inventoryItems"] });
|
||||||
@ -101,17 +102,18 @@ export function EditInventoryItem({
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// console.log("Mutate called");
|
||||||
|
console.log(item.id);
|
||||||
mutation.mutate({
|
mutation.mutate({
|
||||||
name: itemName,
|
name: itemName,
|
||||||
categoryId: item.categoryId,
|
categoryId: item.categoryId ?? 0,
|
||||||
quantity: itemQuantity,
|
quantity: itemQuantity ?? 0,
|
||||||
unitId:
|
unitId:
|
||||||
fetchedHarvestUnits.find((unit) => unit.name === itemUnit)?.id ?? 0,
|
fetchedHarvestUnits.find((unit) => unit.name === itemUnit)?.id ?? 0,
|
||||||
statusId:
|
statusId:
|
||||||
fetchedInventoryStatus.find((status) => status.name === itemStatus)
|
fetchedInventoryStatus.find((status) => status.name === itemStatus)
|
||||||
?.id ?? 0,
|
?.id ?? 0,
|
||||||
dateAdded: new Date().toISOString(),
|
dateAdded: new Date().toISOString(),
|
||||||
id: "",
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -94,7 +94,7 @@ export default function InventoryPage() {
|
|||||||
staleTime: 60 * 1000,
|
staleTime: 60 * 1000,
|
||||||
});
|
});
|
||||||
//////////////////////////////
|
//////////////////////////////
|
||||||
// console.table(inventoryItems);
|
console.table(inventoryItems);
|
||||||
// console.table(inventoryStatus);
|
// console.table(inventoryStatus);
|
||||||
// console.table(harvestUnits);
|
// console.table(harvestUnits);
|
||||||
|
|
||||||
@ -182,11 +182,11 @@ export default function InventoryPage() {
|
|||||||
item={{
|
item={{
|
||||||
id: row.original.id,
|
id: row.original.id,
|
||||||
name: row.original.name,
|
name: row.original.name,
|
||||||
categoryId: row.original.categoryId,
|
categoryId: row.original.category.id,
|
||||||
quantity: row.original.quantity,
|
quantity: row.original.quantity,
|
||||||
unitId: row.original.unitId,
|
unitId: row.original.unit.id,
|
||||||
dateAdded: row.original.dateAdded,
|
dateAdded: row.original.dateAdded,
|
||||||
statusId: row.original.statusId,
|
statusId: row.original.status.id,
|
||||||
}}
|
}}
|
||||||
fetchedInventoryStatus={inventoryStatus}
|
fetchedInventoryStatus={inventoryStatus}
|
||||||
fetchedInventoryCategory={inventoryCategory}
|
fetchedInventoryCategory={inventoryCategory}
|
||||||
|
|||||||
@ -161,7 +161,7 @@ export type CreateInventoryItemInput = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// export type UpdateInventoryItemInput = CreateInventoryItemInput & {};
|
// export type UpdateInventoryItemInput = CreateInventoryItemInput & {};
|
||||||
// export type EditInventoryItemInput = CreateInventoryItemInput & { id: number };
|
export type EditInventoryItemInput = CreateInventoryItemInput;
|
||||||
|
|
||||||
export type UpdateInventoryItemInput = Partial<CreateInventoryItemInput> & {
|
export type UpdateInventoryItemInput = Partial<CreateInventoryItemInput> & {
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user