Skip to content

Commit 6937170

Browse files
authored
🐛 Enable save on assessment change (#1858)
The change is detected by comparing form values with initial values. Properties checked: 1. stakeholders 2. stakeholder groups 3. questions Resolves: https://issues.redhat.com/browse/MTA-2655 Signed-off-by: Radoslaw Szwajkowski <[email protected]>
1 parent a6e9afb commit 6937170

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

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

+19-8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
114114
return comments;
115115
}, [assessment]);
116116

117+
const initialStakeholders = assessment?.stakeholders ?? [];
118+
const initialStakeholderGroups = assessment?.stakeholderGroups ?? [];
117119
const initialQuestions = useMemo(() => {
118120
const questions: { [key: string]: string | undefined } = {};
119121
if (assessment) {
@@ -146,8 +148,8 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
146148
resolver: yupResolver(validationSchema),
147149
mode: "all",
148150
defaultValues: {
149-
stakeholders: assessment?.stakeholders ?? [],
150-
stakeholderGroups: assessment?.stakeholderGroups ?? [],
151+
stakeholders: initialStakeholders,
152+
stakeholderGroups: initialStakeholderGroups,
151153

152154
[COMMENTS_KEY]: initialComments,
153155
[QUESTIONS_KEY]: initialQuestions,
@@ -160,7 +162,6 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
160162
const isValid = methods.formState.isValid;
161163
const isSubmitting = methods.formState.isSubmitting;
162164
const isValidating = methods.formState.isValidating;
163-
const watchAllFields = methods.watch();
164165

165166
const disableNavigation = !isValid || isSubmitting;
166167

@@ -439,11 +440,20 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
439440
}
440441
};
441442

442-
const haveAnyQuestionBeenAnswered = () => {
443-
const questions = values[QUESTIONS_KEY];
444-
return Object.values(questions).some(
445-
(answer) => answer !== null && answer !== ""
443+
const isAssessmentChanged = () => {
444+
const questionsChanged = Object.entries(values[QUESTIONS_KEY]).some(
445+
([name, answer]) => initialQuestions[name] !== answer
446446
);
447+
const stakeholdersChanged =
448+
initialStakeholders.length !== values.stakeholders.length ||
449+
initialStakeholderGroups.length !== values.stakeholderGroups.length ||
450+
!values.stakeholders.every(({ id, name }) =>
451+
initialStakeholders.find((it) => it.id === id && it.name === name)
452+
) ||
453+
!values.stakeholderGroups.every(({ id, name }) =>
454+
initialStakeholderGroups.find((it) => it.id === id && it.name === name)
455+
);
456+
return questionsChanged || stakeholdersChanged;
447457
};
448458

449459
const handleCancelAssessment = () => {
@@ -486,6 +496,7 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
486496
isLastStep={step === sortedSections.length}
487497
onNext={() => setCurrentStep(step + 1)}
488498
onBack={() => setCurrentStep(step - 1)}
499+
isAssessmentChanged={isAssessmentChanged()}
489500
isDisabled={
490501
isSubmitting ||
491502
isValidating ||
@@ -574,7 +585,7 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
574585
onClose={() => setAssessmentToCancel(null)}
575586
onConfirm={() => handleCancelAssessment()}
576587
message={
577-
haveAnyQuestionBeenAnswered()
588+
isAssessmentChanged()
578589
? t("message.unsavedChanges")
579590
: t("message.noAnswers")
580591
}

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+
isAssessmentChanged?: 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+
isAssessmentChanged,
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+
!isAssessmentChanged
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+
!isAssessmentChanged
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+
isAssessmentChanged={true}
9495
enableNext={true}
9596
onSave={onSaveSpy}
9697
onSaveAsDraft={jest.fn()}

0 commit comments

Comments
 (0)