From bfeda8ce13d2f805029586bc75a0f324d0194278 Mon Sep 17 00:00:00 2001 From: THIS ONE IS A LITTLE BIT TRICKY KRUB Date: Tue, 1 Apr 2025 18:53:18 +0700 Subject: [PATCH] feat: add fetchHarvestUnits API and integrate into inventory management --- frontend/api/harvest.ts | 12 ++++++ frontend/api/inventory.ts | 11 +++--- .../inventory/edit-inventory-item.tsx | 38 +++++++++++------- frontend/app/(sidebar)/inventory/page.tsx | 39 +++++++++++++++++-- frontend/types.ts | 4 ++ 5 files changed, 80 insertions(+), 24 deletions(-) create mode 100644 frontend/api/harvest.ts diff --git a/frontend/api/harvest.ts b/frontend/api/harvest.ts new file mode 100644 index 0000000..4961212 --- /dev/null +++ b/frontend/api/harvest.ts @@ -0,0 +1,12 @@ +import axiosInstance from "./config"; +import type { HarvestUnits } from "@/types"; + +export async function fetchHarvestUnits(): Promise { + try { + const response = await axiosInstance.get("/harvest/units"); + return response.data; + } catch (error) { + console.error("Error fetching inventory status:", error); + return []; + } +} diff --git a/frontend/api/inventory.ts b/frontend/api/inventory.ts index 35c1dd2..9d06b01 100644 --- a/frontend/api/inventory.ts +++ b/frontend/api/inventory.ts @@ -1,7 +1,6 @@ import axiosInstance from "./config"; import type { InventoryItem, - CreateInventoryItemInput, InventoryItemStatus, InventoryItemCategory, } from "@/types"; @@ -50,7 +49,7 @@ export async function fetchInventoryItems(): Promise { name: "Tomato Seeds", category: "1", quantity: 500, - unit: "packets", + unit: "1", lastUpdated: "2023-03-01", status: "1", }, @@ -59,7 +58,7 @@ export async function fetchInventoryItems(): Promise { name: "NPK Fertilizer", category: "3", quantity: 200, - unit: "kg", + unit: "2", lastUpdated: "2023-03-05", status: "2", }, @@ -68,7 +67,7 @@ export async function fetchInventoryItems(): Promise { name: "Corn Seeds", category: "1", quantity: 300, - unit: "packets", + unit: "1", lastUpdated: "2023-03-10", status: "3", }, @@ -77,7 +76,7 @@ export async function fetchInventoryItems(): Promise { name: "Organic Compost", category: "3", quantity: 150, - unit: "kg", + unit: "2", lastUpdated: "2023-03-15", status: "1", }, @@ -86,7 +85,7 @@ export async function fetchInventoryItems(): Promise { name: "Wheat Seeds", category: "1", quantity: 250, - unit: "packets", + unit: "2", lastUpdated: "2023-03-20", status: "2", }, diff --git a/frontend/app/(sidebar)/inventory/edit-inventory-item.tsx b/frontend/app/(sidebar)/inventory/edit-inventory-item.tsx index 05a0c9e..4261dd2 100644 --- a/frontend/app/(sidebar)/inventory/edit-inventory-item.tsx +++ b/frontend/app/(sidebar)/inventory/edit-inventory-item.tsx @@ -18,11 +18,6 @@ import { } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; -import { - Popover, - PopoverContent, - PopoverTrigger, -} from "@/components/ui/popover"; import { Select, SelectContent, @@ -32,8 +27,11 @@ import { SelectTrigger, SelectValue, } from "@/components/ui/select"; -import { cn } from "@/lib/utils"; -import { InventoryItemStatus, InventoryItemCategory } from "@/types"; +import { + InventoryItemStatus, + InventoryItemCategory, + HarvestUnits, +} from "@/types"; // import { updateInventoryItem } from "@/api/inventory"; // import type { UpdateInventoryItemInput } from "@/types"; @@ -46,6 +44,7 @@ export interface EditInventoryItemProps { quantity: number; fetchedInventoryStatus: InventoryItemStatus[]; fetchedInventoryCategory: InventoryItemCategory[]; + fetchedHarvestUnits: HarvestUnits[]; } export function EditInventoryItem({ @@ -57,6 +56,7 @@ export function EditInventoryItem({ quantity, fetchedInventoryStatus, fetchedInventoryCategory, + fetchedHarvestUnits, }: EditInventoryItemProps) { const [open, setOpen] = useState(false); const [itemName, setItemName] = useState(name); @@ -172,16 +172,24 @@ export function EditInventoryItem({ />
-
diff --git a/frontend/app/(sidebar)/inventory/page.tsx b/frontend/app/(sidebar)/inventory/page.tsx index 02d2d7c..749a757 100644 --- a/frontend/app/(sidebar)/inventory/page.tsx +++ b/frontend/app/(sidebar)/inventory/page.tsx @@ -29,6 +29,7 @@ import { } from "@/components/ui/pagination"; import { Search } from "lucide-react"; import { FaSort, FaSortUp, FaSortDown } from "react-icons/fa"; +import { fetchHarvestUnits } from "@/api/harvest"; import { Badge } from "@/components/ui/badge"; import { @@ -49,7 +50,8 @@ export default function InventoryPage() { pageIndex: 0, pageSize: 10, }); - + ////////////////////////////// + // query the necessary data for edit and etc. const { data: inventoryItems = [], isLoading: isItemLoading, @@ -78,6 +80,16 @@ export default function InventoryPage() { queryFn: fetchInventoryCategory, staleTime: 60 * 1000, }); + const { + data: harvestUnits = [], + isLoading: isLoadingHarvestUnits, + isError: isErrorHarvestUnits, + } = useQuery({ + queryKey: ["harvestUnits"], + queryFn: fetchHarvestUnits, + staleTime: 60 * 1000, + }); + ////////////////////////////// // console.table(inventoryItems); // console.table(inventoryStatus); const [searchTerm, setSearchTerm] = useState(""); @@ -93,8 +105,12 @@ export default function InventoryPage() { inventoryCategory.find( (categoryItem) => categoryItem.id.toString() === item.category )?.name || "", + unit: + harvestUnits.find((unit) => unit.id.toString() === item.unit)?.name || + "", fetchedInventoryStatus: inventoryStatus, fetchedInventoryCategory: inventoryCategory, + fetchedHarvestUnits: harvestUnits, id: String(item.id), })) .filter((item) => @@ -102,6 +118,8 @@ export default function InventoryPage() { ); }, [inventoryItems, searchTerm]); + // prepare columns for table + const columns = [ { accessorKey: "name", header: "Name" }, { accessorKey: "category", header: "Category" }, @@ -159,14 +177,29 @@ export default function InventoryPage() { onSortingChange: setSorting, onPaginationChange: setPagination, }); + const loadingStates = [ + isItemLoading, + isLoadingStatus, + isLoadingCategory, + isLoadingHarvestUnits, + ]; + const errorStates = [ + isItemError, + isErrorStatus, + isErrorCategory, + isErrorHarvestUnits, + ]; - if (isItemLoading || isLoadingStatus || isLoadingCategory) + const isLoading = loadingStates.some((loading) => loading); + const isError = errorStates.some((error) => error); + + if (isLoading) return (
Loading...
); - if (isItemError || isErrorStatus || isErrorCategory) + if (isError) return (
Error loading inventory data. diff --git a/frontend/types.ts b/frontend/types.ts index ca0d27b..387e232 100644 --- a/frontend/types.ts +++ b/frontend/types.ts @@ -77,6 +77,10 @@ export type InventoryItemCategory = { id: number; name: string; }; +export type HarvestUnits = { + id: number; + name: string; +}; export type CreateInventoryItemInput = Omit< InventoryItem,