From 637e278997fe7f01e88dbffd44a4a8e464da766b Mon Sep 17 00:00:00 2001 From: ibolton336 Date: Thu, 21 Sep 2023 16:56:43 -0400 Subject: [PATCH] :bug: Intitial values for stakeholders Signed-off-by: ibolton336 --- client/src/app/api/models.ts | 4 +- .../assessment-wizard/assessment-wizard.tsx | 76 ++++++++++++++++--- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/client/src/app/api/models.ts b/client/src/app/api/models.ts index 4df69028ae..968e9b678a 100644 --- a/client/src/app/api/models.ts +++ b/client/src/app/api/models.ts @@ -709,8 +709,8 @@ export interface Assessment description: string; status: AssessmentStatus; risk: Risk; - stakeholders: Ref[]; - stakeholderGroups: Ref[]; + stakeholders?: Ref[]; + stakeholderGroups?: Ref[]; } export interface CategorizedTag { category: TagCategory; diff --git a/client/src/app/pages/assessment/components/assessment-wizard/assessment-wizard.tsx b/client/src/app/pages/assessment/components/assessment-wizard/assessment-wizard.tsx index 2c9c2a8589..2fcb702c96 100644 --- a/client/src/app/pages/assessment/components/assessment-wizard/assessment-wizard.tsx +++ b/client/src/app/pages/assessment/components/assessment-wizard/assessment-wizard.tsx @@ -10,6 +10,7 @@ import { Assessment, AssessmentStatus, Question, + Ref, Section, } from "@app/api/models"; import { CustomWizardFooter } from "../custom-wizard-footer"; @@ -34,6 +35,8 @@ import { Paths } from "@app/Paths"; import { yupResolver } from "@hookform/resolvers/yup"; import { AssessmentStakeholdersForm } from "../assessment-stakeholders-form/assessment-stakeholders-form"; import useIsArchetype from "@app/hooks/useIsArchetype"; +import { useFetchStakeholderGroups } from "@app/queries/stakeholdergoups"; +import { useFetchStakeholders } from "@app/queries/stakeholders"; export const SAVE_ACTION_KEY = "saveAction"; @@ -66,6 +69,8 @@ export const AssessmentWizard: React.FC = ({ }) => { const isArchetype = useIsArchetype(); const queryClient = useQueryClient(); + const { stakeholderGroups } = useFetchStakeholderGroups(); + const { stakeholders } = useFetchStakeholders(); const onHandleUpdateAssessmentSuccess = () => { queryClient.invalidateQueries([ @@ -124,13 +129,11 @@ export const AssessmentWizard: React.FC = ({ }, [assessment]); useEffect(() => { + console.log("asssessment.stakeholders", assessment?.stakeholders); methods.reset({ - stakeholders: - assessment?.stakeholders.map((stakeholder) => stakeholder.name) || [], + stakeholders: assessment?.stakeholders?.map((sh) => sh.name).sort() ?? [], stakeholderGroups: - assessment?.stakeholderGroups.map( - (stakeholderGroup) => stakeholderGroup.name - ) || [], + assessment?.stakeholderGroups?.map((sg) => sg.name).sort() ?? [], // comments: initialComments, questions: initialQuestions, [SAVE_ACTION_KEY]: SAVE_ACTION_VALUE.SAVE_AS_DRAFT, @@ -146,11 +149,9 @@ export const AssessmentWizard: React.FC = ({ defaultValues: useMemo(() => { return { stakeholders: - assessment?.stakeholders.map((stakeholder) => stakeholder.name) || [], + assessment?.stakeholders?.map((sh) => sh.name).sort() ?? [], stakeholderGroups: - assessment?.stakeholderGroups.map( - (stakeholderGroup) => stakeholderGroup.name - ) || [], + assessment?.stakeholderGroups?.map((sg) => sg.name).sort() ?? [], // comments: initialComments, questions: initialQuestions, [SAVE_ACTION_KEY]: SAVE_ACTION_VALUE.SAVE_AS_DRAFT, @@ -273,6 +274,25 @@ export const AssessmentWizard: React.FC = ({ const assessmentStatus: AssessmentStatus = "started"; const payload: Assessment = { ...assessment, + stakeholders: + values.stakeholders === undefined + ? undefined + : (values.stakeholders + .map((name) => stakeholders.find((s) => s.name === name)) + .map((sh) => + !sh ? undefined : { id: sh.id, name: sh.name } + ) + .filter(Boolean) as Ref[]), + + stakeholderGroups: + values.stakeholderGroups === undefined + ? undefined + : (values.stakeholderGroups + .map((name) => stakeholderGroups.find((s) => s.name === name)) + .map((sg) => + !sg ? undefined : { id: sg.id, name: sg.name } + ) + .filter(Boolean) as Ref[]), sections, status: assessmentStatus, }; @@ -317,6 +337,25 @@ export const AssessmentWizard: React.FC = ({ const payload: Assessment = { ...assessment, + stakeholders: + values.stakeholders === undefined + ? undefined + : (values.stakeholders + .map((name) => stakeholders.find((s) => s.name === name)) + .map((sh) => + !sh ? undefined : { id: sh.id, name: sh.name } + ) + .filter(Boolean) as Ref[]), + + stakeholderGroups: + values.stakeholderGroups === undefined + ? undefined + : (values.stakeholderGroups + .map((name) => stakeholderGroups.find((s) => s.name === name)) + .map((sg) => + !sg ? undefined : { id: sg.id, name: sg.name } + ) + .filter(Boolean) as Ref[]), sections, status: assessmentStatus, }; @@ -364,6 +403,25 @@ export const AssessmentWizard: React.FC = ({ const payload: Assessment = { ...assessment, + stakeholders: + values.stakeholders === undefined + ? undefined + : (values.stakeholders + .map((name) => stakeholders.find((s) => s.name === name)) + .map((sh) => + !sh ? undefined : { id: sh.id, name: sh.name } + ) + .filter(Boolean) as Ref[]), + + stakeholderGroups: + values.stakeholderGroups === undefined + ? undefined + : (values.stakeholderGroups + .map((name) => stakeholderGroups.find((s) => s.name === name)) + .map((sg) => + !sg ? undefined : { id: sg.id, name: sg.name } + ) + .filter(Boolean) as Ref[]), sections, status: assessmentStatus, };