Skip to content

Commit 32ba708

Browse files
committed
Enable save on assessment change
Signed-off-by: Radoslaw Szwajkowski <[email protected]>
1 parent 6e5cb97 commit 32ba708

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

client/src/app/pages/assessment/components/assessment-wizard/assessment-wizard.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
160160
const isValid = methods.formState.isValid;
161161
const isSubmitting = methods.formState.isSubmitting;
162162
const isValidating = methods.formState.isValidating;
163-
const watchAllFields = methods.watch();
164163

165164
const disableNavigation = !isValid || isSubmitting;
166165

@@ -439,10 +438,10 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
439438
}
440439
};
441440

442-
const haveAnyQuestionBeenAnswered = () => {
441+
const haveAnyNewQuestionBeenAnswered = () => {
443442
const questions = values[QUESTIONS_KEY];
444-
return Object.values(questions).some(
445-
(answer) => answer !== null && answer !== ""
443+
return Object.entries(questions).some(
444+
([name, answer]) => initialQuestions[name] !== answer
446445
);
447446
};
448447

@@ -486,6 +485,7 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
486485
isLastStep={step === sortedSections.length}
487486
onNext={() => setCurrentStep(step + 1)}
488487
onBack={() => setCurrentStep(step - 1)}
488+
haveAnyNewQuestionBeenAnswered={haveAnyNewQuestionBeenAnswered()}
489489
isDisabled={
490490
isSubmitting ||
491491
isValidating ||
@@ -574,7 +574,7 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
574574
onClose={() => setAssessmentToCancel(null)}
575575
onConfirm={() => handleCancelAssessment()}
576576
message={
577-
haveAnyQuestionBeenAnswered()
577+
haveAnyNewQuestionBeenAnswered()
578578
? t("message.unsavedChanges")
579579
: t("message.noAnswers")
580580
}

client/src/app/pages/assessment/components/custom-wizard-footer/custom-wizard-footer.tsx

+15-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface CustomWizardFooterProps {
1313
isDisabled: boolean;
1414
isFormInvalid: boolean;
1515
isSaveAsDraftDisabled?: boolean;
16+
haveAnyNewQuestionBeenAnswered?: boolean;
1617
enableNext?: boolean;
1718
onNext?: () => void;
1819
onBack?: () => void;
@@ -27,6 +28,7 @@ export const CustomWizardFooter: React.FC<CustomWizardFooterProps> = ({
2728
isDisabled,
2829
isFormInvalid,
2930
isSaveAsDraftDisabled,
31+
haveAnyNewQuestionBeenAnswered,
3032
enableNext,
3133
onNext,
3234
onBack,
@@ -35,7 +37,7 @@ export const CustomWizardFooter: React.FC<CustomWizardFooterProps> = ({
3537
onSaveAsDraft,
3638
}) => {
3739
const { t } = useTranslation();
38-
const { goToNextStep, goToPrevStep, close, activeStep } = useWizardContext();
40+
const { goToNextStep, goToPrevStep, close } = useWizardContext();
3941
return (
4042
<>
4143
<WizardFooterWrapper>
@@ -45,15 +47,25 @@ export const CustomWizardFooter: React.FC<CustomWizardFooterProps> = ({
4547
<Button
4648
variant="primary"
4749
onClick={() => onSave(false)}
48-
isDisabled={!enableNext || isDisabled || isFormInvalid}
50+
isDisabled={
51+
!enableNext ||
52+
isDisabled ||
53+
isFormInvalid ||
54+
!haveAnyNewQuestionBeenAnswered
55+
}
4956
cy-data="next"
5057
>
5158
{t("actions.save")}
5259
</Button>
5360
<Button
5461
variant="primary"
5562
onClick={() => onSave(true)}
56-
isDisabled={!enableNext || isDisabled || isFormInvalid}
63+
isDisabled={
64+
!enableNext ||
65+
isDisabled ||
66+
isFormInvalid ||
67+
!haveAnyNewQuestionBeenAnswered
68+
}
5769
cy-data="save-and-review"
5870
>
5971
{t("actions.saveAndReview")}

client/src/app/pages/assessment/components/custom-wizard-footer/tests/custom-wizard-footer.test.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ describe("AppPlaceholder", () => {
9191
isLastStep={true}
9292
isDisabled={false}
9393
isFormInvalid={false}
94+
haveAnyNewQuestionBeenAnswered={true}
9495
enableNext={true}
9596
onSave={onSaveSpy}
9697
onSaveAsDraft={jest.fn()}

0 commit comments

Comments
 (0)