Skip to content

Commit

Permalink
🐛 Save as Draft not working
Browse files Browse the repository at this point in the history
Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 committed Sep 25, 2023
1 parent a89c4e5 commit fb25506
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
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
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 useDeleteAssessmentMutation = (
};

export const useFetchAssessmentById = (id?: number | string) => {
const { data, isLoading, error } = useQuery({
const { data, isLoading, error, isFetching } = useQuery({
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

0 comments on commit fb25506

Please sign in to comment.