44 getApplicationForm ,
55 getApplicationFormData ,
66 getBundleForms ,
7+ getExecuteRules ,
78 getFormDetails ,
89} from './FormioEndpoints' ;
910import { getUser } from '../../../../../helpers/utility' ;
@@ -15,6 +16,7 @@ import {
1516import LoadingOverlay from '../../../../../components/loader/LoadingOverlay' ;
1617import { Form } from '@formio/react' ;
1718import '@formio/js/dist/formio.full.min.css' ;
19+ import { set } from 'date-fns' ;
1820
1921type 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 + o r \s + / gi, ' || ' )
69+ . replace ( / \s + a n d \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
0 commit comments