From d2498ee68d0f0309b8387c2c5f24228602d6af13 Mon Sep 17 00:00:00 2001 From: suryacp23 Date: Wed, 29 Jul 2026 14:04:53 +0530 Subject: [PATCH] added route code based on that it will filter --- src/components/forms/DynamicForm.tsx | 119 +++++++++++++++++- src/components/forms/fields/WfLookupField.tsx | 35 ++++-- 2 files changed, 141 insertions(+), 13 deletions(-) diff --git a/src/components/forms/DynamicForm.tsx b/src/components/forms/DynamicForm.tsx index 42c64aa..f0a2a35 100644 --- a/src/components/forms/DynamicForm.tsx +++ b/src/components/forms/DynamicForm.tsx @@ -178,6 +178,56 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: } } + if (currentActivityId === ORDER_BOOKING.activities.LOG_VISIT.uid || currentActivityId === '42b7f47d-96f0-4289-a6d9-38f455ad57dd') { + const dateVal = (defaultValues['date_of_visit'] as string) || (defaultValues['date_of_visit_1'] as string) || new Date().toISOString().split('T')[0]; + const timeVal = (defaultValues['time_of_visit'] as string) || (defaultValues['time_of_visit_1'] as string) || new Date().toTimeString().split(' ')[0].substring(0, 5); + + if (!defaultValues['date_of_visit']) defaultValues['date_of_visit'] = dateVal; + if (!defaultValues['time_of_visit']) defaultValues['time_of_visit'] = timeVal; + + const dailyLogField = res.fields.find( + f => f.id === 'daily_log' || f.uid === 'field_1785225403902' || f.name.toLowerCase() === 'daily log' + ); + const dailyLogFieldId = dailyLogField?.uid || dailyLogField?.id || 'field_1785225403902'; + + try { + const dailyLogLookupRes = await client.wfLookupRecords({ + activityId: currentActivityId, + fieldId: dailyLogFieldId, + formData: { + date_of_visit: dateVal, + time_of_visit: timeVal, + ...defaultValues + }, + limit: 200 + }); + const arr = Array.isArray(dailyLogLookupRes) ? dailyLogLookupRes : (dailyLogLookupRes?.data || dailyLogLookupRes?.records || []); + if (arr.length > 0) { + const firstLog = arr[0]; + const dailyLogInstanceId = String(firstLog.instance_id || firstLog.id || ''); + const routeCode = firstLog.route_code || firstLog.route_code_1 || firstLog.route_code_2 || firstLog.route_code_3 || firstLog.route || ''; + + if (dailyLogInstanceId) { + defaultValues[dailyLogFieldId] = dailyLogInstanceId; + defaultValues['daily_log'] = dailyLogInstanceId; + defaultValues['field_1785225403902'] = dailyLogInstanceId; + } + + if (routeCode) { + console.log("[Log Visit] Initial daily_log lookup route_code fetched:", routeCode); + defaultValues['route_code'] = routeCode; + defaultValues['field_1785311859486'] = routeCode; + const routeField = res.fields.find(f => f.id === 'route_code' || f.uid === 'field_1785311859486'); + if (routeField) { + defaultValues[routeField.id] = routeCode; + } + } + } + } catch (e) { + console.warn('Failed to load initial daily_log lookup for Log Visit', e); + } + } + setValues(defaultValues); setLoading(false); } @@ -197,17 +247,35 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: const clickedActionRef = useRef(null); - const handleFieldChange = (fieldId: string, newVal: unknown) => { + const handleFieldChange = (fieldId: string, newVal: unknown, rawRow?: any) => { setValues(prev => { const next = { ...prev, [fieldId]: newVal }; - // Auto-calculate order_details totals const getBaseIdForField = (id: string) => id.replace(/_\d+$/, ''); const fieldDef = schema?.fields.find(f => f.id === fieldId); - // Auto-fill dataset keys from _raw for select/multiselect fields - if (fieldDef && (fieldDef.data_type === 'select' || fieldDef.data_type === 'multiselect') && fieldDef.properties?.options) { - const option = fieldDef.properties.options.find(o => String(o.value) === String(newVal)); + if (rawRow && typeof rawRow === 'object') { + const getBaseId = (id: string) => id.replace(/_\d+$/, ''); + for (const key of Object.keys(rawRow)) { + const lowerKey = key.toLowerCase(); + let targetCol = schema?.fields.find(c => + c.id.toLowerCase() === lowerKey || + (c.mapped_workflow_field || '').toLowerCase() === lowerKey || + getBaseId(c.id).toLowerCase() === lowerKey || + c.name.toLowerCase().replace(/\s+/g, '') === lowerKey.replace(/_/g, '') + ); + if (targetCol && targetCol.id !== fieldId) { + next[targetCol.id] = rawRow[key]; + } else if (!targetCol && key !== fieldId) { + next[key] = rawRow[key]; + } + } + } + + // Auto-fill dataset keys from _raw for select/multiselect/wf_lookup fields + if (fieldDef && (fieldDef.data_type === 'select' || fieldDef.data_type === 'multiselect' || fieldDef.data_type === 'wf_lookup')) { + const fieldOptions = fieldDef.properties?.options || (fieldDef as any).options; + const option = fieldOptions?.find((o: any) => String(o.value) === String(newVal)); if (option && option._raw) { const rawData = option._raw; const getBaseId = (id: string) => id.replace(/_\d+$/, ''); @@ -275,6 +343,31 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: } } + if ((currentActivityId === ORDER_BOOKING.activities.LOG_VISIT.uid || currentActivityId === '42b7f47d-96f0-4289-a6d9-38f455ad57dd') && (fieldId.includes('date_of_visit') || fieldId.includes('time_of_visit'))) { + const updatedFormData = { ...next }; + client.wfLookupRecords({ + activityId: currentActivityId, + fieldId: 'field_1', + formData: updatedFormData, + limit: 200 + }).then(lookupRes => { + const arr = Array.isArray(lookupRes) ? lookupRes : (lookupRes?.data || lookupRes?.records || []); + if (arr.length > 0) { + const firstRow = arr[0]; + const routeCode = firstRow.route_code || firstRow.route_code_1 || firstRow.route_code_2 || firstRow.route_code_3 || firstRow.route; + if (routeCode) { + setValues(p => ({ + ...p, + route_code: routeCode, + route_code_1: routeCode, + route_code_2: routeCode, + route_code_3: routeCode, + })); + } + } + }).catch(e => console.warn('Dynamic route_code lookup failed:', e)); + } + const isOrderDetails = fieldId === 'order_details' || getBaseIdForField(fieldId) === 'order_details' || fieldDef?.mapped_workflow_field === 'order_details'; if (isOrderDetails && Array.isArray(newVal)) { @@ -495,6 +588,20 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: if (isPlaceOrder && isOrderId) { return null; } + + const isLogVisit = currentActivityId === ORDER_BOOKING.activities.LOG_VISIT.uid || currentActivityId === '42b7f47d-96f0-4289-a6d9-38f455ad57dd'; + const isHiddenLogVisitField = isLogVisit && ( + f.id === 'daily_log' || + f.uid === 'field_1785225403902' || + f.id === 'route_code' || + f.uid === 'field_1785311859486' || + f.name.toLowerCase() === 'daily log' || + f.name.toLowerCase() === 'route code' + ); + + if (isHiddenLogVisitField) { + return null; + } const renderField = () => { if (type === 'wf_lookup') { @@ -508,7 +615,7 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: activityId={currentActivityId} fieldId={f.id} formData={values} - onChange={(newVal) => handleFieldChange(f.id, newVal)} + onChange={(newVal, rawRow) => handleFieldChange(f.id, newVal, rawRow)} /> ); } diff --git a/src/components/forms/fields/WfLookupField.tsx b/src/components/forms/fields/WfLookupField.tsx index 91db08b..f49baad 100644 --- a/src/components/forms/fields/WfLookupField.tsx +++ b/src/components/forms/fields/WfLookupField.tsx @@ -6,7 +6,7 @@ export interface WfLookupFieldProps { label: string; required?: boolean; value: string | number; - onChange: (val: string) => void; + onChange: (val: string, rawRow?: any) => void; client: ZinoClient; config: any; // wf_lookup_config activityId: string; @@ -15,13 +15,20 @@ export interface WfLookupFieldProps { } export function WfLookupField({ label, required, value, onChange, client, config, activityId, fieldId, formData }: WfLookupFieldProps) { - const [options, setOptions] = useState<{ label: string; value: string }[]>([]); + const [options, setOptions] = useState<{ label: string; value: string; _raw?: any }[]>([]); const [loading, setLoading] = useState(true); + const formDataStr = JSON.stringify(formData); + useEffect(() => { let mounted = true; - const wfUuid = config?.workflow_uuid; - if (!wfUuid) { + const isStoreLookup = fieldId === 'select_store' || fieldId === 'field_1783057892381' || label.toLowerCase().includes('select store'); + const hasRouteCode = Boolean( + formData.route_code || formData.route_code_1 || formData.route_code_2 || formData.route_code_3 || formData.field_1785311859486 + ); + + if (isStoreLookup && !hasRouteCode) { + setOptions([]); setLoading(false); return; } @@ -52,11 +59,20 @@ export function WfLookupField({ label, required, value, onChange, client, config return { value: String(row.instance_id || row.id), - label: labelText + label: labelText, + _raw: row }; }); setOptions(opts); + + // Auto-select first option if no value is selected yet (e.g. for daily_log) + const isDailyLogField = fieldId === 'daily_log' || fieldId === 'field_1785225403902' || label.toLowerCase().includes('daily log'); + if ((!value || value === '') && opts.length > 0 && isDailyLogField) { + const firstOpt = opts[0]; + console.log("[WfLookupField] Auto-selecting first daily_log option:", firstOpt); + onChange(firstOpt.value, firstOpt._raw); + } }) .catch(err => { console.error("Failed to load wf_lookup records:", err); @@ -66,14 +82,19 @@ export function WfLookupField({ label, required, value, onChange, client, config }); return () => { mounted = false; }; - }, [client, config]); + }, [client, config, activityId, fieldId, formDataStr]); + + const handleSelectChange = (newVal: string) => { + const selectedOpt = options.find(o => String(o.value) === String(newVal)); + onChange(newVal, selectedOpt?._raw); + }; return ( );