Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Save as Draft not working #1401

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as yup from "yup";
import React, { useEffect, useMemo, useState } from "react";
import React, { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { FieldErrors, FormProvider, useForm } from "react-hook-form";
Expand Down Expand Up @@ -131,18 +131,6 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
return questions;
}, [assessment]);

useEffect(() => {
console.log("asssessment.stakeholders", assessment?.stakeholders);
methods.reset({
stakeholders: assessment?.stakeholders?.map((sh) => sh.name).sort() ?? [],
stakeholderGroups:
assessment?.stakeholderGroups?.map((sg) => sg.name).sort() ?? [],
// comments: initialComments,
questions: initialQuestions,
[SAVE_ACTION_KEY]: SAVE_ACTION_VALUE.SAVE_AS_DRAFT,
});
}, [initialQuestions, assessment]);

const validationSchema = yup.object().shape({
stakeholders: yup.array().of(yup.string()),
stakeholderGroups: yup.array().of(yup.string()),
Expand All @@ -169,7 +157,6 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
const isValid = methods.formState.isValid;
const isSubmitting = methods.formState.isSubmitting;
const isValidating = methods.formState.isValidating;

const watchAllFields = methods.watch();

const disableNavigation = !isValid || isSubmitting;
Expand Down Expand Up @@ -204,17 +191,32 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
// return value !== null && value !== undefined && value.length > 0;
// };

const shouldNextBtnBeEnabled = (section: Section): boolean => {
const allQuestionsValid = section?.questions.every((question) =>
isQuestionValid(question)
const areAllQuestionsAnswered = (section: Section): boolean => {
return (
section?.questions.every((question) => {
return questionHasValue(question);
}) ?? false
);
const allQuestionsAnswered = section?.questions.every((question) => {
};

const hasPartialAnswers = (section: Section): boolean => {
const someQuestionsAnswered = section?.questions.some((question) => {
return questionHasValue(question);
});
return (
allQuestionsAnswered && allQuestionsValid
// && isCommentValid(category)

const allQuestionsAnswered = areAllQuestionsAnswered(section);

return someQuestionsAnswered && !allQuestionsAnswered;
};

const shouldNextBtnBeEnabled = (section: Section): boolean => {
const allQuestionsValid = section?.questions.every((question) =>
isQuestionValid(question)
);

const allQuestionsAnswered = areAllQuestionsAnswered(section);

return allQuestionsAnswered && allQuestionsValid;
};

const maxCategoryWithData = [...sortedSections].reverse().find((section) => {
Expand Down Expand Up @@ -541,6 +543,7 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
(currentStep === sortedSections.length &&
!shouldNextBtnBeEnabled(sortedSections[currentStep - 1]))
}
hasAnswers={hasPartialAnswers(sortedSections[currentStep - 1])}
isFormInvalid={!isValid}
onSave={(review) => {
const saveActionValue = review
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface CustomWizardFooterProps {
isDisabled: boolean;
isFormInvalid: boolean;
isArchetype?: boolean;
hasAnswers?: boolean;
onSave: (review: boolean) => void;
onSaveAsDraft: () => void;
}
Expand All @@ -23,6 +24,7 @@ export const CustomWizardFooter: React.FC<CustomWizardFooterProps> = ({
isDisabled,
isFormInvalid,
isArchetype,
hasAnswers,
onSave,
onSaveAsDraft,
}) => {
Expand Down Expand Up @@ -84,7 +86,7 @@ export const CustomWizardFooter: React.FC<CustomWizardFooterProps> = ({
<Button
variant="link"
onClick={onSaveAsDraft}
isDisabled={isFormInvalid}
isDisabled={isFormInvalid || isFirstStep || !hasAnswers}
cy-data="save-as-draft"
>
{t("actions.saveAsDraft")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe("AppPlaceholder", () => {
isLastStep={false}
isDisabled={false}
isFormInvalid={false}
hasAnswers={true}
onSave={jest.fn()}
onSaveAsDraft={onSaveAsDraftSpy}
/>
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/queries/assessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@
};

export const useFetchAssessmentById = (id?: number | string) => {
const { data, isLoading, error } = useQuery({
const { data, isLoading, error, isFetching } = useQuery({

Check warning on line 110 in client/src/app/queries/assessments.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/assessments.ts#L110

Added line #L110 was not covered by tests
queryKey: [assessmentQueryKey, id],
queryFn: () => (id ? getAssessmentById(id) : undefined),
onError: (error: AxiosError) => console.log("error, ", error),
enabled: !!id,
});
return {
assessment: data,
isFetching: isLoading,
isFetching: isLoading || isFetching,
fetchError: error,
};
};
Expand Down
Loading