Skip to content

Commit 11e3c8f

Browse files
authored
changes to display bundle forms correctly (#1038)
1 parent e33d2c9 commit 11e3c8f

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

cats-frontend/src/app/features/applications/application/applicationTabs/application/Application.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getApplicationForm,
55
getApplicationFormData,
66
getBundleForms,
7+
getExecuteRules,
78
getFormDetails,
89
} from './FormioEndpoints';
910
import { getUser } from '../../../../../helpers/utility';
@@ -15,6 +16,7 @@ import {
1516
import LoadingOverlay from '../../../../../components/loader/LoadingOverlay';
1617
import { Form } from '@formio/react';
1718
import '@formio/js/dist/formio.full.min.css';
19+
import { set } from 'date-fns';
1820

1921
type ApplicationDetails =
2022
GetApplicationByIdQuery['getApplicationDetailsById']['data'];
@@ -52,9 +54,54 @@ export const Application: React.FC<ApplicationProps> = () => {
5254
});
5355
const [formType, setFormType] = useState<string | null>(null);
5456
const [selectedForms, setSelectedForms] = useState<ApplicationForm[]>([]);
57+
const [formsToBeFiltered, setFormsToBeFiltered] = useState<ApplicationForm[]>(
58+
[],
59+
);
5560
const [activeStep, setActiveStep] = useState<number>(0);
5661
const [error, setError] = useState<string | null>(null);
5762
const [isLoading, setIsLoading] = useState(true);
63+
const [rules, setRules] = useState<any[]>([]);
64+
65+
const evaluateRule = (rule: any, data: any) => {
66+
// Normalize operators
67+
let expr = rule
68+
.replace(/\s+or\s+/gi, ' || ')
69+
.replace(/\s+and\s+/gi, ' && ')
70+
.replace(/==/g, '===')
71+
.replace(/!=/g, '!==');
72+
73+
// Replace object keys with actual values
74+
expr = expr.replace(/\b\w+\b/g, (match: any) => {
75+
if (Object.prototype.hasOwnProperty.call(data, match)) {
76+
const val = data[match];
77+
return typeof val === 'string' ? `"${val}"` : val;
78+
}
79+
return match;
80+
});
81+
82+
try {
83+
// eslint-disable-next-line no-new-func
84+
return new Function(`return (${expr});`)();
85+
} catch (e) {
86+
return false;
87+
}
88+
};
89+
90+
useEffect(() => {
91+
if (rules.length > 0 && formsToBeFiltered.length > 0) {
92+
const filteredForms = formsToBeFiltered.filter((form) => {
93+
const formRule = rules.find((rule) => rule.formId === form.formId);
94+
95+
if (formRule) {
96+
return formRule.rules.every((rule: any) => {
97+
return evaluateRule(rule, formData?.data);
98+
});
99+
}
100+
});
101+
setSelectedForms(filteredForms);
102+
setFormJson(filteredForms[0]?.formJson);
103+
}
104+
}, [rules, formsToBeFiltered]);
58105

59106
const applicationId = parseInt(id ?? '', 10);
60107
const {
@@ -106,7 +153,12 @@ export const Application: React.FC<ApplicationProps> = () => {
106153
});
107154

108155
const forms = await Promise.all(formPromises);
156+
getExecuteRules(id, formData?.data).then((response) => {
157+
setRules(response?.data ?? []);
158+
});
159+
109160
setSelectedForms(forms);
161+
setFormsToBeFiltered(forms);
110162
setFormJson(forms[0]?.formJson);
111163
}
112164

cats-frontend/src/app/features/applications/application/applicationTabs/application/FormioEndpoints.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ export const getApplicationForm = async (formId?: string) => {
6060
}
6161
};
6262

63+
export const getExecuteRules = async (bundleId?: string, data?: any) => {
64+
try {
65+
const ENDPOINT = `${COMS_ENDPOINTS.FORM}/${bundleId}${COMS_ENDPOINTS.BUNDLES}/execute-rules`;
66+
const response = await getAxiosInstance(FORM_API).post(ENDPOINT, {
67+
...data,
68+
});
69+
return response;
70+
} catch (error) {
71+
console.error('Error in getting getExecuteRules:', error);
72+
throw error;
73+
}
74+
};
75+
6376
/**
6477
* @description Fetches the form data for a given form ID and submission ID from the FormFlow API.
6578
* @param {string} formId - The ID of the form to retrieve the data of

0 commit comments

Comments
 (0)