From e0d79ffbc131de908485071218580bc89e6d8438 Mon Sep 17 00:00:00 2001 From: suryacp23 Date: Wed, 22 Jul 2026 11:49:37 +0530 Subject: [PATCH] added required for potential and order details --- .../forms/fields/SmartGridField.tsx | 40 +++++++++++++++++-- src/components/reusable/Select.tsx | 9 +++-- src/screens/DailySalesReportPage.tsx | 4 -- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/components/forms/fields/SmartGridField.tsx b/src/components/forms/fields/SmartGridField.tsx index 8907be4..6c00754 100644 --- a/src/components/forms/fields/SmartGridField.tsx +++ b/src/components/forms/fields/SmartGridField.tsx @@ -20,6 +20,8 @@ export function SmartGridField({ const formRef = useRef(null); const [newRow, setNewRow] = useState>({}); const [editingIdx, setEditingIdx] = useState(null); + const [errors, setErrors] = useState>({}); + const visibleColumns = columns.filter(c => { const isOrderDetails = label.toLowerCase().includes('order'); @@ -56,6 +58,8 @@ export function SmartGridField({ const updateNewRowField = (fieldId: string, val: unknown) => { let row = { ...newRow, [fieldId]: val }; + setErrors(prev => ({ ...prev, [fieldId]: '' })); + const colDef = columns.find(c => c.id === fieldId); @@ -105,6 +109,26 @@ export function SmartGridField({ }; const submitNewRow = () => { + const newErrors: Record = {}; + const getBaseId = (id: string) => id.replace(/_\d+$/, ''); + const catColId = columns.find(c => c.id === 'product_category' || c.name === 'Product Category' || c.mapped_workflow_field === 'product_category' || getBaseId(c.id) === 'product_category')?.id; + const isCatSelected = catColId ? !!newRow[catColId] : false; + + for (const col of visibleColumns) { + const isProductName = col.id === 'product_name' || col.name === 'Product Name' || col.mapped_workflow_field === 'product_name' || getBaseId(col.id) === 'product_name'; + const isBags = col.id === 'bags' || col.name === 'Bags' || col.mapped_workflow_field === 'bags' || getBaseId(col.id) === 'bags' || col.id.toLowerCase().includes('quantity') || col.name.toLowerCase().includes('quantity') || col.mapped_workflow_field?.toLowerCase().includes('quantity'); + const isRequired = col.mandatory || (isCatSelected && (isProductName || isBags)); + + if (isRequired && !newRow[col.id]) { + newErrors[col.id] = 'This field is required'; + } + } + + if (Object.keys(newErrors).length > 0) { + setErrors(newErrors); + return; + } + if (editingIdx !== null) { const next = [...value]; next[editingIdx] = newRow; @@ -206,7 +230,7 @@ export function SmartGridField({ {productName}
{(!isPotentialMining || hasBagsCol) && ( - {bags} Bags + {bags} {isPotentialMining ? 'Kgs' : 'Bags'} )} {isPotentialMining && diffVal !== null && !isNaN(diffVal) && diffVal !== 0 && ( @@ -268,6 +292,14 @@ export function SmartGridField({
{visibleColumns.map(col => { const val = newRow[col.id]; + + const getBaseId = (id: string) => id.replace(/_\d+$/, ''); + const catColId = columns.find(c => c.id === 'product_category' || c.name === 'Product Category' || c.mapped_workflow_field === 'product_category' || getBaseId(c.id) === 'product_category')?.id; + const isCatSelected = catColId ? !!newRow[catColId] : false; + const isProductName = col.id === 'product_name' || col.name === 'Product Name' || col.mapped_workflow_field === 'product_name' || getBaseId(col.id) === 'product_name'; + const isBags = col.id === 'bags' || col.name === 'Bags' || col.mapped_workflow_field === 'bags' || getBaseId(col.id) === 'bags' || col.id.toLowerCase().includes('quantity') || col.name.toLowerCase().includes('quantity') || col.mapped_workflow_field?.toLowerCase().includes('quantity'); + const isRequired = col.mandatory || (isCatSelected && (isProductName || isBags)); + if (col.data_type === 'select' || col.data_type === 'multiselect') { const allOpts = col.properties?.options || []; let filteredOpts = allOpts; @@ -301,7 +333,8 @@ export function SmartGridField({ updateNewRowField(col.id, col.data_type === 'number' ? Number(e.target.value) : e.target.value)} diff --git a/src/components/reusable/Select.tsx b/src/components/reusable/Select.tsx index ef5a122..23870e0 100644 --- a/src/components/reusable/Select.tsx +++ b/src/components/reusable/Select.tsx @@ -10,6 +10,7 @@ export interface SelectOption { export interface SelectProps extends SelectHTMLAttributes { label?: string; hint?: string; + error?: string; /** Either strings or {value,label} objects. */ options?: Array; /** Class for the outer label wrapper. */ @@ -17,7 +18,7 @@ export interface SelectProps extends SelectHTMLAttributes { } /** Labeled native select styled to match Input. */ -export function Select({ label, hint, options = [], required, className, ...rest }: SelectProps) { +export function Select({ label, hint, error, options = [], required, className, ...rest }: SelectProps) { return (