diff --git a/src/components/rule-builder/rule-builder.component.tsx b/src/components/rule-builder/rule-builder.component.tsx index 6d98f18e..0046ee42 100644 --- a/src/components/rule-builder/rule-builder.component.tsx +++ b/src/components/rule-builder/rule-builder.component.tsx @@ -116,6 +116,9 @@ const RuleBuilder = React.memo( conditions: conditions, isNewRule: false, }); + const [isHistoryEnable, setIsHistoryEnable] = useState( + question?.historicalExpression?.length > 0 || false, + ); const prevPageIndex = useRef(-1); const prevSectionIndex = useRef(-1); const prevQuestionIndex = useRef(-1); @@ -173,6 +176,32 @@ const RuleBuilder = React.memo( onSchemaChange(updatedSchema); }, [checkIfDecimalValidatorExists, onSchemaChange, pageIndex, questionIndex, schema, sectionIndex]); + const checkIfHistoricalExpressionExists = useCallback(() => { + return !!schema?.pages[pageIndex]?.sections[sectionIndex].questions[questionIndex]?.historicalExpression; + }, [pageIndex, questionIndex, schema?.pages, sectionIndex]); + + const handleHistoryChange = useCallback(() => { + const isHistoricalExpressionExist = checkIfHistoricalExpressionExists(); + const updatedSchema = { ...schema }; + const historicalExpressionSchema = `HD.getObject('prevEnc').getValue('${question.questionOptions.concept}')`; + if (isHistoricalExpressionExist) { + delete schema.pages[pageIndex].sections[sectionIndex].questions[questionIndex].historicalExpression; + } else { + schema.pages[pageIndex].sections[sectionIndex].questions[questionIndex].historicalExpression = + historicalExpressionSchema; + } + setIsHistoryEnable((p) => !p); + onSchemaChange(updatedSchema); + }, [ + checkIfHistoricalExpressionExists, + onSchemaChange, + pageIndex, + question.questionOptions.concept, + questionIndex, + schema, + sectionIndex, + ]); + const shouldDeleteForHideAction = (action: Action) => { return action.calculateField?.length || action.errorMessage?.length || action.actionField?.length; }; @@ -855,9 +884,11 @@ const RuleBuilder = React.memo( isRequired={isRequired} isAllowFutureDate={isAllowFutureDate} isDisallowDecimals={isDisallowDecimals} + isHistoryEnable={isHistoryEnable} handleRequiredChange={handleRequiredChange} handleAllowFutureDateChange={handleAllowFutureDateChange} handleDisallowDecimalValueChange={handleDisallowDecimalValueChange} + handleHistoryChange={handleHistoryChange} ruleId={ruleId} question={question} /> @@ -926,9 +957,11 @@ interface RuleHeaderProps { isRequired: boolean; isAllowFutureDate: boolean; isDisallowDecimals: boolean; + isHistoryEnable: boolean; handleRequiredChange: () => void; handleAllowFutureDateChange: () => void; handleDisallowDecimalValueChange: () => void; + handleHistoryChange: () => void; ruleId: string; question: Question; } @@ -937,9 +970,11 @@ export const RuleHeader = React.memo( isRequired, isAllowFutureDate, isDisallowDecimals, + isHistoryEnable, handleRequiredChange, handleAllowFutureDateChange, handleDisallowDecimalValueChange, + handleHistoryChange, ruleId, question, }: RuleHeaderProps) => { @@ -978,6 +1013,15 @@ export const RuleHeader = React.memo( size="sm" /> )} + ); }, diff --git a/src/types.ts b/src/types.ts index 147a9c4a..cf34376d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -140,6 +140,7 @@ export interface Schema { validators?: Array>; disable?: DisableProps; hide?: HideProps; + historicalExpression?: string; }>; hide?: HideProps; }>; @@ -192,6 +193,7 @@ export interface Question { required?: string | boolean | RequiredFieldProps; validators?: Array>; disable?: DisableProps; + historicalExpression?: string; } export interface QuestionOptions {