From 8647d3dd3e303528fb8c1c1bd27dbc98597b6489 Mon Sep 17 00:00:00 2001 From: davidels1 <87242587+davidels1@users.noreply.github.com> Date: Thu, 25 Sep 2025 23:29:58 +0200 Subject: [PATCH] Refactor purchase order form and stock columns Updated PurchaseOrderForm to use useFieldArray for managing items, simplifying add/remove logic and improving reactivity. Adjusted stock columns to conditionally render cells based on screen size for better responsive design, and fixed currency code casing in sellingPrice column. --- .../purchase-orders/PurchaseOrderForm.tsx | 23 +++++++++++-------- src/components/stock/columns.tsx | 6 +++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/components/purchase-orders/PurchaseOrderForm.tsx b/src/components/purchase-orders/PurchaseOrderForm.tsx index 920ac87..8bef0a5 100644 --- a/src/components/purchase-orders/PurchaseOrderForm.tsx +++ b/src/components/purchase-orders/PurchaseOrderForm.tsx @@ -2,7 +2,7 @@ import * as React from "react" import { zodResolver } from "@hookform/resolvers/zod" -import { useForm } from "react-hook-form" +import { useForm, useFieldArray } from "react-hook-form" import * as z from "zod" import { useState, useEffect, useCallback } from "react" // Import useState and useEffect @@ -124,23 +124,26 @@ export function PurchaseOrderForm({ setOpen, refreshData }: PurchaseOrderFormPro }, }) + const { fields, append, remove } = useFieldArray({ + control: form.control, + name: "items", + }); + + // Function to add a new item row const addItem = () => { - const currentItems = form.getValues("items"); - form.setValue("items", [...currentItems, { + append({ stockItemId: "", itemName: "", quantity: 1, sku: "", costPrice: 0, - }]); + }); } // Function to remove an item row const removeItem = (index: number) => { - const currentItems = form.getValues("items"); - const newItems = currentItems.filter((_, i) => i !== index); - form.setValue("items", newItems); + remove(index); } // Function to handle changes in item fields @@ -266,8 +269,8 @@ export function PurchaseOrderForm({ setOpen, refreshData }: PurchaseOrderFormPro {/* Items Section */}

Order Items

- {form.getValues("items").map((item, index) => ( -
+ {fields.map((item, index) => ( +
- {form.getValues("items").length > 1 && ( + {fields.length > 1 && ( diff --git a/src/components/stock/columns.tsx b/src/components/stock/columns.tsx index 0483f56..baa798d 100644 --- a/src/components/stock/columns.tsx +++ b/src/components/stock/columns.tsx @@ -45,10 +45,12 @@ export const createColumns = (props: ActionsColumnProps): ColumnDef[] { accessorKey: "sku", header: "SKU", + cell: ({ row }) =>
{row.getValue("sku")}
, }, { accessorKey: "category", header: "Category", + cell: ({ row }) =>
{row.getValue("category")}
, }, { accessorKey: "quantity", @@ -63,7 +65,7 @@ export const createColumns = (props: ActionsColumnProps): ColumnDef[] style: "currency", currency: "ZAR", }).format(amount) - return
{formatted}
+ return
{formatted}
}, }, { @@ -73,7 +75,7 @@ export const createColumns = (props: ActionsColumnProps): ColumnDef[] const amount = parseFloat(row.getValue("sellingPrice")) const formatted = new Intl.NumberFormat("en-ZA", { style: "currency", - currency: "ZAR", + currency: "Zar", }).format(amount) return
{formatted}
},