added action button and fix potential mining api

This commit is contained in:
suryacp23 2026-07-22 11:16:54 +05:30
parent bdea45b6da
commit bcd169e194

View File

@ -1,4 +1,4 @@
import { useState, useEffect } from 'react'; import { useState, useEffect, useRef } from 'react';
import type { ZinoClient } from '../../api/client'; import type { ZinoClient } from '../../api/client';
import type { FormScreenResponse } from '../../api/types'; import type { FormScreenResponse } from '../../api/types';
import { Button } from '../buttons/Button'; import { Button } from '../buttons/Button';
@ -17,7 +17,7 @@ import {
WfLookupField, WfLookupField,
RadioField, RadioField,
} from './fields'; } from './fields';
import { ORDER_BOOKING } from '../../api/config'; import { ORDER_BOOKING, STORE } from '../../api/config';
export interface DynamicFormProps { export interface DynamicFormProps {
client: ZinoClient; client: ZinoClient;
@ -158,6 +158,8 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
const [submitting, setSubmitting] = useState(false); const [submitting, setSubmitting] = useState(false);
const [submitError, setSubmitError] = useState<string | null>(null); const [submitError, setSubmitError] = useState<string | null>(null);
const clickedActionRef = useRef<string | null>(null);
const handleFieldChange = (fieldId: string, newVal: unknown) => { const handleFieldChange = (fieldId: string, newVal: unknown) => {
setValues(prev => { setValues(prev => {
const next = { ...prev, [fieldId]: newVal }; const next = { ...prev, [fieldId]: newVal };
@ -207,7 +209,11 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
return <div className="p-4 text-ruby-600">Failed to load form: {error}</div>; return <div className="p-4 text-ruby-600">Failed to load form: {error}</div>;
} }
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) => { const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
@ -215,9 +221,13 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
setSubmitError(null); setSubmitError(null);
try { try {
const payload: Record<string, unknown> = {}; const payload: Record<string, unknown> = {};
const finalValues = { ...values };
if (actionField && clickedActionRef.current) {
finalValues[actionField.id] = clickedActionRef.current;
}
for (const f of fields) { for (const f of fields) {
const val = values[f.id]; const val = finalValues[f.id];
if (val == null) continue; if (val == null) continue;
if (f.data_type === 'phone' && typeof val === 'string') { if (f.data_type === 'phone' && typeof val === 'string') {
@ -278,11 +288,52 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
if (nextActivity) { if (nextActivity) {
let nextPrefillData = undefined; let nextPrefillData = undefined;
if (nextActivity.activity_uid === ORDER_BOOKING.activities.POTENTIAL_MINING.uid) { 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 { try {
const pmRes = await client.request<{ potential: { potential: any[] } }>( const pmRes = await client.request<{ potential: { potential: any[] } }>(
'POST', 'POST',
'/api/papi2/potential-mining', '/api/papi2/potential-mining',
{ instance_id: String(res.instance_id ?? currentInstanceId) }, {
instance_id: String(res.instance_id ?? currentInstanceId),
store_code: storeCodeToSend
},
{ 'TemplateID': '146' } { 'TemplateID': '146' }
); );
const rawPotential = pmRes.potential?.potential || []; const rawPotential = pmRes.potential?.potential || [];
@ -303,11 +354,11 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
} }
} }
if (nextPrefillData) { setChainedPrefillData(prev => ({
setChainedPrefillData(nextPrefillData); ...prev,
} else { ...values,
setChainedPrefillData(undefined); ...(nextPrefillData || {})
} }));
setChainQueue(pending); setChainQueue(pending);
setCurrentActivityId(nextActivity.activity_uid); setCurrentActivityId(nextActivity.activity_uid);
@ -325,7 +376,7 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
return ( return (
<form onSubmit={handleSubmit} className="flex flex-col gap-4"> <form onSubmit={handleSubmit} className="flex flex-col gap-4">
{fields.map(f => { {normalFields.map(f => {
const type = f.data_type; const type = f.data_type;
const val = values[f.id]; const val = values[f.id];
const isDisabled = schema.field_defaults?.[f.id]?.disabled || f.properties?.disabled; const isDisabled = schema.field_defaults?.[f.id]?.disabled || f.properties?.disabled;
@ -497,9 +548,23 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId:
Cancel Cancel
</Button> </Button>
)} )}
<Button type="submit" variant="primary" disabled={submitting}> {actionField && actionField.properties?.options ? (
{submitting ? 'Submitting...' : 'Submit'} actionField.properties.options.map((opt: any) => (
</Button> <Button
key={opt.value}
type="submit"
variant="primary"
disabled={submitting}
onClick={() => { clickedActionRef.current = opt.value; }}
>
{submitting && clickedActionRef.current === opt.value ? 'Submitting...' : opt.label}
</Button>
))
) : (
<Button type="submit" variant="primary" disabled={submitting}>
{submitting ? 'Submitting...' : 'Submit'}
</Button>
)}
</div> </div>
</form> </form>
); );