feat: add updateInventoryItem function and integrate with EditInventoryItem component

This commit is contained in:
THIS ONE IS A LITTLE BIT TRICKY KRUB 2025-04-02 22:49:30 +07:00
parent c76b04a32c
commit 00b392876b
3 changed files with 85 additions and 30 deletions

View File

@ -4,6 +4,7 @@ import type {
InventoryItemStatus, InventoryItemStatus,
InventoryItemCategory, InventoryItemCategory,
CreateInventoryItemInput, CreateInventoryItemInput,
UpdateInventoryItemInput,
} from "@/types"; } from "@/types";
/** /**
@ -113,12 +114,25 @@ export async function createInventoryItem(
export async function deleteInventoryItem(id: string) { export async function deleteInventoryItem(id: string) {
try { try {
const response = await axiosInstance.delete<InventoryItem>( const response = await axiosInstance.delete("/inventory/" + id);
"/inventory/" + id
);
return response.data; return response.data;
} catch (error) { } catch (error) {
console.error("Error while deleting Inventory Item! " + error); console.error("Error while deleting Inventory Item! " + error);
throw new Error("Failed to deleting inventory item: " + error); throw new Error("Failed to deleting inventory item: " + error);
} }
} }
export async function updateInventoryItem(
id: string,
item: UpdateInventoryItemInput
) {
try {
const response = await axiosInstance.put<InventoryItem>(
"/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);
}
}

View File

@ -32,8 +32,8 @@ import {
InventoryItemCategory, InventoryItemCategory,
HarvestUnits, HarvestUnits,
} from "@/types"; } from "@/types";
// import { updateInventoryItem } from "@/api/inventory"; import { updateInventoryItem } from "@/api/inventory";
// import type { UpdateInventoryItemInput } from "@/types"; import type { UpdateInventoryItemInput } from "@/types";
export interface EditInventoryItemProps { export interface EditInventoryItemProps {
id: string; id: string;
@ -76,35 +76,73 @@ export function EditInventoryItem({
(statusItem) => statusItem.id.toString() === statusId (statusItem) => statusItem.id.toString() === statusId
)?.name )?.name
); );
const [error, setError] = useState<string | null>(null);
// const queryClient = useQueryClient(); const queryClient = useQueryClient();
// const mutation = useMutation({ const mutation = useMutation({
// mutationFn: (item: UpdateInventoryItemInput) => UpdateInventoryItem(item), mutationFn: (item: UpdateInventoryItemInput) =>
// onSuccess: () => { updateInventoryItem(id, item),
// // Invalidate queries to refresh inventory data. onSuccess: () => {
// queryClient.invalidateQueries({ queryKey: ["inventoryItems"] }); // invalidate queries to refresh inventory data.
// // Reset form fields and close dialog. queryClient.invalidateQueries({ queryKey: ["inventoryItems"] });
// setItemName(""); // reset form fields and close dialog.
// setItemType(""); setItemName("");
// setItemCategory(""); setItemCategory("");
// setItemQuantity(0); setItemQuantity(0);
// setItemUnit(""); setItemUnit("");
// setDate(undefined); setOpen(false);
// setOpen(false); setItemStatus("");
// }, },
// }); });
// send edit request
const handleEdit = () => { const handleEdit = () => {
// // Basic validation (you can extend this as needed) if (!itemName || !itemCategory || !itemUnit) {
// if (!itemName || !itemType || !itemCategory || !itemUnit) return; setError("All fields are required. Please fill in missing details.");
// mutation.mutate({ return;
}
const category = fetchedInventoryCategory.find(
(c) => c.name === itemCategory
);
const unit = fetchedHarvestUnits.find((u) => u.name === itemUnit);
const status = fetchedInventoryStatus.find((s) => s.name === itemStatus);
if (!category || !unit || !status) {
setError(
"Invalid category, unit, or status. Please select a valid option."
);
return;
}
// console.table({
// name: itemName, // name: itemName,
// type: itemType, // categoryId:
// category: itemCategory, // fetchedInventoryCategory.find(
// (category) => category.name === itemCategory
// )?.id ?? 0,
// quantity: itemQuantity, // quantity: itemQuantity,
// unit: itemUnit, // unitId:
// fetchedHarvestUnits.find((unit) => unit.name === itemUnit)?.id ?? 0,
// statusId:
// fetchedInventoryStatus.find((status) => status.name === itemStatus)
// ?.id ?? 0,
// lastUpdated: new Date().toISOString(),
// }); // });
mutation.mutate({
name: itemName,
categoryId:
fetchedInventoryCategory.find(
(category) => category.name === itemCategory
)?.id ?? 0,
quantity: itemQuantity,
unitId:
fetchedHarvestUnits.find((unit) => unit.name === itemUnit)?.id ?? 0,
statusId:
fetchedInventoryStatus.find((status) => status.name === itemStatus)
?.id ?? 0,
lastUpdated: new Date().toISOString(),
});
}; };
return ( return (
@ -205,6 +243,7 @@ export function EditInventoryItem({
</div> </div>
</div> </div>
<DialogFooter> <DialogFooter>
{error && <p className="text-red-500 text-sm">{error}</p>}
<Button type="submit" onClick={handleEdit}> <Button type="submit" onClick={handleEdit}>
Save Save
</Button> </Button>

View File

@ -91,6 +91,8 @@ export type CreateInventoryItemInput = {
statusId: number; statusId: number;
}; };
export type UpdateInventoryItemInput = CreateInventoryItemInput & {};
export interface Blog { export interface Blog {
id: number; id: number;
title: string; title: string;