Skip to content

Commit

Permalink
🐛 Intitial values for stakeholders
Browse files Browse the repository at this point in the history
Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 committed Sep 21, 2023
1 parent 1573d37 commit 637e278
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 11 deletions.
4 changes: 2 additions & 2 deletions client/src/app/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Assessment,
AssessmentStatus,
Question,
Ref,
Section,
} from "@app/api/models";
import { CustomWizardFooter } from "../custom-wizard-footer";
Expand All @@ -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";

Expand Down Expand Up @@ -66,6 +69,8 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
}) => {
const isArchetype = useIsArchetype();
const queryClient = useQueryClient();
const { stakeholderGroups } = useFetchStakeholderGroups();
const { stakeholders } = useFetchStakeholders();

const onHandleUpdateAssessmentSuccess = () => {
queryClient.invalidateQueries([
Expand Down Expand Up @@ -124,13 +129,11 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
}, [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,
Expand All @@ -146,11 +149,9 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
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,
Expand Down Expand Up @@ -273,6 +274,25 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
const assessmentStatus: AssessmentStatus = "started";
const payload: Assessment = {
...assessment,
stakeholders:
values.stakeholders === undefined
? undefined
: (values.stakeholders
.map((name) => stakeholders.find((s) => s.name === name))
.map<Ref | undefined>((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<Ref | undefined>((sg) =>
!sg ? undefined : { id: sg.id, name: sg.name }
)
.filter(Boolean) as Ref[]),
sections,
status: assessmentStatus,
};
Expand Down Expand Up @@ -317,6 +337,25 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({

const payload: Assessment = {
...assessment,
stakeholders:
values.stakeholders === undefined
? undefined
: (values.stakeholders
.map((name) => stakeholders.find((s) => s.name === name))
.map<Ref | undefined>((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<Ref | undefined>((sg) =>
!sg ? undefined : { id: sg.id, name: sg.name }
)
.filter(Boolean) as Ref[]),
sections,
status: assessmentStatus,
};
Expand Down Expand Up @@ -364,6 +403,25 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({

const payload: Assessment = {
...assessment,
stakeholders:
values.stakeholders === undefined
? undefined
: (values.stakeholders
.map((name) => stakeholders.find((s) => s.name === name))
.map<Ref | undefined>((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<Ref | undefined>((sg) =>
!sg ? undefined : { id: sg.id, name: sg.name }
)
.filter(Boolean) as Ref[]),
sections,
status: assessmentStatus,
};
Expand Down

0 comments on commit 637e278

Please sign in to comment.