Skip to content

Commit

Permalink
🐛 Adjust save as draft logic (#1497)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 authored Oct 28, 2023
1 parent 6179d70 commit 319f8d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,16 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
);
};

const hasPartialAnswers = (section: Section): boolean => {
const someQuestionsAnswered = section?.questions.some((question) => {
return questionHasValue(question);
const shouldDisableSaveAsDraft = (sections: Section[]): boolean => {
const noAnswers = sections.every((section) => {
return section.questions.every((question) => !questionHasValue(question));
});

const allQuestionsAnswered = areAllQuestionsAnswered(section);
const allQuestionsAnswered = sections.every((section) =>
areAllQuestionsAnswered(section)
);

return someQuestionsAnswered && !allQuestionsAnswered;
return noAnswers || allQuestionsAnswered;
};

const shouldNextBtnBeEnabled = (section: Section): boolean => {
Expand Down Expand Up @@ -500,7 +502,7 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
(step === sortedSections.length &&
!shouldNextBtnBeEnabled(sortedSections[step - 1]))
}
hasAnswers={hasPartialAnswers(sortedSections[step - 1])}
isSaveAsDraftDisabled={shouldDisableSaveAsDraft(sortedSections)}
isFormInvalid={!isValid}
onSave={(review) => {
const saveActionValue = review
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface CustomWizardFooterProps {
isLastStep: boolean;
isDisabled: boolean;
isFormInvalid: boolean;
hasAnswers?: boolean;
isSaveAsDraftDisabled?: boolean;
enableNext?: boolean;
onNext?: () => void;
onBack?: () => void;
Expand All @@ -26,7 +26,7 @@ export const CustomWizardFooter: React.FC<CustomWizardFooterProps> = ({
isLastStep,
isDisabled,
isFormInvalid,
hasAnswers,
isSaveAsDraftDisabled,
enableNext,
onNext,
onBack,
Expand Down Expand Up @@ -97,7 +97,7 @@ export const CustomWizardFooter: React.FC<CustomWizardFooterProps> = ({
<Button
variant="link"
onClick={onSaveAsDraft}
isDisabled={isFormInvalid || isFirstStep || !hasAnswers}
isDisabled={isFormInvalid || isFirstStep || isSaveAsDraftDisabled}
cy-data="save-as-draft"
>
{t("actions.saveAsDraft")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("AppPlaceholder", () => {
isLastStep={false}
isDisabled={false}
isFormInvalid={false}
hasAnswers={true}
isSaveAsDraftDisabled={false}
onSave={jest.fn()}
onSaveAsDraft={onSaveAsDraftSpy}
/>
Expand Down

0 comments on commit 319f8d2

Please sign in to comment.