Skip to content

Commit

Permalink
(feat): enable historical expression using toggle functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
senthil-athiban committed Jul 26, 2024
1 parent 471632a commit 371cd7e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/components/rule-builder/rule-builder.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ const RuleBuilder = React.memo(
conditions: conditions,
isNewRule: false,
});
const [isHistoryEnable, setIsHistoryEnable] = useState<boolean>(
question?.historicalExpression?.length > 0 || false,
);
const prevPageIndex = useRef(-1);
const prevSectionIndex = useRef(-1);
const prevQuestionIndex = useRef(-1);
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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}
/>
Expand Down Expand Up @@ -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;
}
Expand All @@ -937,9 +970,11 @@ export const RuleHeader = React.memo(
isRequired,
isAllowFutureDate,
isDisallowDecimals,
isHistoryEnable,
handleRequiredChange,
handleAllowFutureDateChange,
handleDisallowDecimalValueChange,
handleHistoryChange,
ruleId,
question,
}: RuleHeaderProps) => {
Expand Down Expand Up @@ -978,6 +1013,15 @@ export const RuleHeader = React.memo(
size="sm"
/>
)}
<Toggle
id={`toggle-enable-historical-expression-${ruleId}`}
aria-label={t('toggleEnableHistory', 'Toggle Enable History')}
labelText={t('enableHistory', 'Enable History')}
hideLabel
toggled={isHistoryEnable}
onToggle={handleHistoryChange}
size="sm"
/>
</div>
);
},
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export interface Schema {
validators?: Array<Record<string, string>>;
disable?: DisableProps;
hide?: HideProps;
historicalExpression?: string;
}>;
hide?: HideProps;
}>;
Expand Down Expand Up @@ -192,6 +193,7 @@ export interface Question {
required?: string | boolean | RequiredFieldProps;
validators?: Array<Record<string, string>>;
disable?: DisableProps;
historicalExpression?: string;
}

export interface QuestionOptions {
Expand Down

0 comments on commit 371cd7e

Please sign in to comment.