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 c768c34460..c47806ab2e 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"; @@ -35,6 +36,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"; @@ -67,6 +70,8 @@ export const AssessmentWizard: React.FC = ({ }) => { const isArchetype = useIsArchetype(); const queryClient = useQueryClient(); + const { stakeholderGroups } = useFetchStakeholderGroups(); + const { stakeholders } = useFetchStakeholders(); const onHandleUpdateAssessmentSuccess = () => { queryClient.invalidateQueries([ @@ -127,13 +132,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, @@ -149,11 +152,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, @@ -276,6 +277,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, }; @@ -320,6 +340,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, }; @@ -367,6 +406,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, };