Skip to content

Commit

Permalink
🐛 Add useEffect for action state sync (#1600)
Browse files Browse the repository at this point in the history
Resolves https://issues.redhat.com/browse/MTA-1826

- Uses a useEffect hook to synchronize the state of the action based on
the assessment object. This way, whenever assessment changes, we can
ensure that the action is recalculated.

Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 committed Dec 11, 2023
1 parent ca600a3 commit d5bb144
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,21 @@ const DynamicAssessmentActionsRow: FunctionComponent<
onDeleteError
);

const determineAction = () => {
const determineAction = React.useCallback(() => {
if (!assessment) {
return AssessmentAction.Take;
} else if (assessment.status === "started") {
return AssessmentAction.Continue;
} else {
return AssessmentAction.Retake;
}
};
}, [assessment]);

const [action, setAction] = React.useState(determineAction());

React.useEffect(() => {
setAction(determineAction());
}, [determineAction, assessment]);

const determineButtonClassName = () => {
const action = determineAction();
Expand Down Expand Up @@ -213,7 +219,7 @@ const DynamicAssessmentActionsRow: FunctionComponent<
onHandleAssessmentAction();
}}
>
{determineAction()}
{action}
</Button>
) : (
<Spinner role="status" size="md">
Expand Down
1 change: 0 additions & 1 deletion client/src/app/queries/assessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export const useFetchAssessmentsByItemId = (
};
const assessmentsWithOrder: AssessmentWithSectionOrder[] =
data?.map(addSectionOrderToQuestions) || [];

return {
assessments: assessmentsWithOrder,
isFetching: isLoading,
Expand Down

0 comments on commit d5bb144

Please sign in to comment.