added route code based on that it will filter

This commit is contained in:
suryacp23 2026-07-29 14:04:53 +05:30
parent f34302d14e
commit d2498ee68d
2 changed files with 141 additions and 13 deletions

View File

@ -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); setValues(defaultValues);
setLoading(false); setLoading(false);
} }
@ -197,17 +247,35 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
const clickedActionRef = useRef<string | null>(null); const clickedActionRef = useRef<string | null>(null);
const handleFieldChange = (fieldId: string, newVal: unknown) => { const handleFieldChange = (fieldId: string, newVal: unknown, rawRow?: any) => {
setValues(prev => { setValues(prev => {
const next = { ...prev, [fieldId]: newVal }; const next = { ...prev, [fieldId]: newVal };
// Auto-calculate order_details totals
const getBaseIdForField = (id: string) => id.replace(/_\d+$/, ''); const getBaseIdForField = (id: string) => id.replace(/_\d+$/, '');
const fieldDef = schema?.fields.find(f => f.id === fieldId); const fieldDef = schema?.fields.find(f => f.id === fieldId);
// Auto-fill dataset keys from _raw for select/multiselect fields if (rawRow && typeof rawRow === 'object') {
if (fieldDef && (fieldDef.data_type === 'select' || fieldDef.data_type === 'multiselect') && fieldDef.properties?.options) { const getBaseId = (id: string) => id.replace(/_\d+$/, '');
const option = fieldDef.properties.options.find(o => String(o.value) === String(newVal)); 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) { if (option && option._raw) {
const rawData = option._raw; const rawData = option._raw;
const getBaseId = (id: string) => id.replace(/_\d+$/, ''); 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'; const isOrderDetails = fieldId === 'order_details' || getBaseIdForField(fieldId) === 'order_details' || fieldDef?.mapped_workflow_field === 'order_details';
if (isOrderDetails && Array.isArray(newVal)) { if (isOrderDetails && Array.isArray(newVal)) {
@ -495,6 +588,20 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
if (isPlaceOrder && isOrderId) { if (isPlaceOrder && isOrderId) {
return null; 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 = () => { const renderField = () => {
if (type === 'wf_lookup') { if (type === 'wf_lookup') {
@ -508,7 +615,7 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
activityId={currentActivityId} activityId={currentActivityId}
fieldId={f.id} fieldId={f.id}
formData={values} formData={values}
onChange={(newVal) => handleFieldChange(f.id, newVal)} onChange={(newVal, rawRow) => handleFieldChange(f.id, newVal, rawRow)}
/> />
); );
} }

View File

@ -6,7 +6,7 @@ export interface WfLookupFieldProps {
label: string; label: string;
required?: boolean; required?: boolean;
value: string | number; value: string | number;
onChange: (val: string) => void; onChange: (val: string, rawRow?: any) => void;
client: ZinoClient; client: ZinoClient;
config: any; // wf_lookup_config config: any; // wf_lookup_config
activityId: string; activityId: string;
@ -15,13 +15,20 @@ export interface WfLookupFieldProps {
} }
export function WfLookupField({ label, required, value, onChange, client, config, activityId, fieldId, formData }: 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 [loading, setLoading] = useState(true);
const formDataStr = JSON.stringify(formData);
useEffect(() => { useEffect(() => {
let mounted = true; let mounted = true;
const wfUuid = config?.workflow_uuid; const isStoreLookup = fieldId === 'select_store' || fieldId === 'field_1783057892381' || label.toLowerCase().includes('select store');
if (!wfUuid) { 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); setLoading(false);
return; return;
} }
@ -52,11 +59,20 @@ export function WfLookupField({ label, required, value, onChange, client, config
return { return {
value: String(row.instance_id || row.id), value: String(row.instance_id || row.id),
label: labelText label: labelText,
_raw: row
}; };
}); });
setOptions(opts); 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 => { .catch(err => {
console.error("Failed to load wf_lookup records:", 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; }; 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 ( return (
<SelectField <SelectField
label={loading ? `${label} (Loading...)` : label} label={loading ? `${label} (Loading...)` : label}
required={required} required={required}
value={String(value || '')} value={String(value || '')}
onChange={onChange} onChange={handleSelectChange}
options={options} options={options}
/> />
); );