diff --git a/src/components/forms/DynamicForm.tsx b/src/components/forms/DynamicForm.tsx index 0f10a51..e0381e0 100644 --- a/src/components/forms/DynamicForm.tsx +++ b/src/components/forms/DynamicForm.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useRef } from 'react'; import type { ZinoClient } from '../../api/client'; import type { FormScreenResponse } from '../../api/types'; import { Button } from '../buttons/Button'; @@ -17,7 +17,7 @@ import { WfLookupField, RadioField, } from './fields'; -import { ORDER_BOOKING } from '../../api/config'; +import { ORDER_BOOKING, STORE } from '../../api/config'; export interface DynamicFormProps { client: ZinoClient; @@ -157,6 +157,8 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: const [values, setValues] = useState>({}); const [submitting, setSubmitting] = useState(false); const [submitError, setSubmitError] = useState(null); + + const clickedActionRef = useRef(null); const handleFieldChange = (fieldId: string, newVal: unknown) => { setValues(prev => { @@ -207,7 +209,11 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: return
Failed to load form: {error}
; } - const fields = schema.fields; + // Filter out disabled fields (usually server-generated IDs) + const fields = schema.fields.filter(f => !f.properties?.disabled); + + const actionField = fields.find(f => f.name.toLowerCase() === 'action' && f.data_type === 'radio'); + const normalFields = fields.filter(f => f !== actionField); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); @@ -215,9 +221,13 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: setSubmitError(null); try { const payload: Record = {}; + const finalValues = { ...values }; + if (actionField && clickedActionRef.current) { + finalValues[actionField.id] = clickedActionRef.current; + } for (const f of fields) { - const val = values[f.id]; + const val = finalValues[f.id]; if (val == null) continue; if (f.data_type === 'phone' && typeof val === 'string') { @@ -278,11 +288,52 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: if (nextActivity) { let nextPrefillData = undefined; if (nextActivity.activity_uid === ORDER_BOOKING.activities.POTENTIAL_MINING.uid) { + let storeCodeToSend = String((values['select_store_row'] as any)?.store_code || (chainedPrefillData?.['select_store_row'] as any)?.store_code || ''); + + if (!storeCodeToSend) { + let storeId = values['select_store'] || chainedPrefillData?.['select_store'] || (schema?.prefill_data as any)?.select_store || (schema?.data as any)?.select_store; + if (typeof storeId === 'object' && storeId !== null) { + storeCodeToSend = (storeId as any).store_code || storeCodeToSend; + storeId = (storeId as any).value || (storeId as any).instance_id || String(storeId); + } + if (!storeCodeToSend && storeId) { + try { + const detailRes = await client.detailView(STORE.detailViews.STORE, storeId); + if (detailRes.data && detailRes.data.store_code) { + storeCodeToSend = String(detailRes.data.store_code); + } + } catch(e) { + try { + const lookupRes = await client.wfLookupRecords({ + activityId: ORDER_BOOKING.activities.LOG_VISIT.uid, + fieldId: ORDER_BOOKING.activities.LOG_VISIT.fields.selectStore, + formData: { ...(chainedPrefillData as any || {}), ...values, ...(schema?.prefill_data as any || {}) }, + limit: 500 + }); + const arr = Array.isArray(lookupRes) ? lookupRes : (lookupRes.data || lookupRes.records || []); + const row = arr.find((r: any) => String(r.instance_id || r.id) === String(storeId)); + if (row && row.store_code) { + storeCodeToSend = String(row.store_code); + } + } catch (err) { + console.error("Failed to fetch store code:", err); + } + } + } + } + + if (!storeCodeToSend) { + storeCodeToSend = String(values['select_store'] || chainedPrefillData?.['select_store'] || (schema?.prefill_data as any)?.select_store || ''); + } + try { const pmRes = await client.request<{ potential: { potential: any[] } }>( 'POST', '/api/papi2/potential-mining', - { instance_id: String(res.instance_id ?? currentInstanceId) }, + { + instance_id: String(res.instance_id ?? currentInstanceId), + store_code: storeCodeToSend + }, { 'TemplateID': '146' } ); const rawPotential = pmRes.potential?.potential || []; @@ -303,11 +354,11 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: } } - if (nextPrefillData) { - setChainedPrefillData(nextPrefillData); - } else { - setChainedPrefillData(undefined); - } + setChainedPrefillData(prev => ({ + ...prev, + ...values, + ...(nextPrefillData || {}) + })); setChainQueue(pending); setCurrentActivityId(nextActivity.activity_uid); @@ -325,7 +376,7 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: return (
- {fields.map(f => { + {normalFields.map(f => { const type = f.data_type; const val = values[f.id]; const isDisabled = schema.field_defaults?.[f.id]?.disabled || f.properties?.disabled; @@ -497,9 +548,23 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: Cancel )} - + {actionField && actionField.properties?.options ? ( + actionField.properties.options.map((opt: any) => ( + + )) + ) : ( + + )}
);