Skip to content

Commit

Permalink
Merge branch 'main' into assessment-settings-table-cols
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 authored Sep 22, 2023
2 parents d567a24 + 48dd6fd commit 598438b
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 107 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 @@ -711,8 +711,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 @@ -220,8 +220,8 @@ export const ApplicationsTable: React.FC = () => {
await Promise.all(
application.assessments.map(async (assessment) => {
await deleteAssessment({
id: assessment.id,
name: application.name,
assessmentId: assessment.id,
applicationName: application.name,
});
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const AssessmentActionsTable: React.FC<AssessmentActionsTableProps> = ({
<QuestionnairesTable
application={application}
archetype={archetype}
isReadonly
questionnaires={archivedQuestionnaires}
assessments={assessments}
isFetching={isFetchingQuestionnaires || isFetchingAssessmentsById}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ const DynamicAssessmentActionsRow: FunctionComponent<
if (assessment) {
try {
await deleteAssessmentAsync({
name: assessment.name,
id: assessment.id,
assessmentId: assessment.id,
applicationName: application?.name,
applicationId: application?.id,
archetypeId: archetype?.id,
}).then(() => {
createAssessment();
});
Expand Down Expand Up @@ -257,8 +259,10 @@ const DynamicAssessmentActionsRow: FunctionComponent<
variant="plain"
onClick={() => {
deleteAssessment({
id: assessment.id,
name: assessment.name,
assessmentId: assessment.id,
applicationName: application?.name,
applicationId: application?.id,
archetypeId: archetype?.id,
});
}}
>
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 @@ -26,6 +27,7 @@ import {
import { AxiosError } from "axios";
import {
assessmentsByItemIdQueryKey,
useDeleteAssessmentMutation,
useUpdateAssessmentMutation,
} from "@app/queries/assessments";
import { useQueryClient } from "@tanstack/react-query";
Expand All @@ -34,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";

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

const onHandleUpdateAssessmentSuccess = () => {
queryClient.invalidateQueries([
Expand All @@ -77,12 +83,14 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
onHandleUpdateAssessmentSuccess
);

const { mutate: deleteAssessmentMutation } = useDeleteAssessmentMutation();

const { t } = useTranslation();

const [currentStep, setCurrentStep] = useState(0);

const [isConfirmDialogOpen, setIsConfirmDialogOpen] =
React.useState<boolean>(false);
const [assessmentToCancel, setAssessmentToCancel] =
React.useState<Assessment | null>(null);

const history = useHistory();

Expand Down Expand Up @@ -124,13 +132,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 +152,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 +277,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 +340,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 +406,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 @@ -509,35 +570,50 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
onNext={() => setCurrentStep((current) => current + 1)}
onBack={() => setCurrentStep((current) => current - 1)}
onClose={() => {
setIsConfirmDialogOpen(true);
assessment && setAssessmentToCancel(assessment);
}}
onGoToStep={(step) => setCurrentStep(step.id as number)}
/>
{isConfirmDialogOpen && (
{assessmentToCancel && (
<ConfirmDialog
title={t("dialog.title.leavePage")}
isOpen
message={t("dialog.message.leavePage")}
confirmBtnVariant={ButtonVariant.primary}
confirmBtnLabel={t("actions.continue")}
cancelBtnLabel={t("actions.cancel")}
onCancel={() => setIsConfirmDialogOpen(false)}
onClose={() => setIsConfirmDialogOpen(false)}
onCancel={() => setAssessmentToCancel(null)}
onClose={() => setAssessmentToCancel(null)}
onConfirm={() => {
setIsConfirmDialogOpen(false);
if (isArchetype) {
assessmentToCancel.status === "empty" &&
deleteAssessmentMutation({
assessmentId: assessmentToCancel.id,
applicationName: assessmentToCancel.application?.name,
applicationId: assessmentToCancel.application?.id,
archetypeId: assessmentToCancel.archetype?.id,
});

history.push(
formatPath(Paths.archetypeAssessmentActions, {
archetypeId: assessment?.archetype?.id,
})
);
} else {
assessmentToCancel.status === "empty" &&
deleteAssessmentMutation({
assessmentId: assessmentToCancel.id,
applicationName: assessmentToCancel.application?.name,
applicationId: assessmentToCancel.application?.id,
archetypeId: assessmentToCancel.archetype?.id,
});
history.push(
formatPath(Paths.applicationAssessmentActions, {
applicationId: assessment?.application?.id,
})
);
}
setAssessmentToCancel(null);
}}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ export const MultiInputSelection: React.FC<MultiInputSelectionProps> = ({

const isArchetype = useIsArchetype();
const { t } = useTranslation();

return (
<Stack>
{sortedOptions.map((option, i) => (
<StackItem key={option.text} className="pf-v5-u-pb-xs">
<StackItem key={`${questionFieldName}-${i}`} className="pf-v5-u-pb-xs">
<HookFormPFGroupController
control={control}
name={questionFieldName as `questions.${string}`}
fieldId="stakeholders"
fieldId={`${questionFieldName}-${i}`}
renderInput={({ field: { value, onChange } }) => (
<Radio
id={`${option.text}`}
id={`${questionFieldName}-${option.order}`}
name={questionFieldName}
isChecked={value === option.text}
onChange={(checked, e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import HelpIcon from "@patternfly/react-icons/dist/esm/icons/help-icon";
import { MultiInputSelection } from "./multi-input-selection";
import { Question, QuestionHeader, QuestionBody } from "./question";
import { getCommentFieldName } from "../../form-utils";
import { getCommentFieldName, getQuestionFieldName } from "../../form-utils";
import { HookFormPFTextInput } from "@app/components/HookFormPFFields";
import { useFormContext } from "react-hook-form";
import { Section } from "@app/api/models";
Expand Down Expand Up @@ -54,7 +54,7 @@ export const QuestionnaireForm: React.FC<QuestionnaireFormProps> = ({
</TextContent>
</StackItem>
{sortedQuestions.map((question) => (
<StackItem key={question.text}>
<StackItem key={question.order}>
<Question cy-data="question">
<QuestionHeader>
<Split hasGutter>
Expand All @@ -74,7 +74,10 @@ export const QuestionnaireForm: React.FC<QuestionnaireFormProps> = ({
</Split>
</QuestionHeader>
<QuestionBody>
<MultiInputSelection question={question} />
<MultiInputSelection
key={getQuestionFieldName(question, true)}
question={question}
/>
</QuestionBody>
</Question>
</StackItem>
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/pages/assessment/form-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ describe("Application assessment - form utils", () => {

it("getQuestionFieldName: fullName", () => {
const fieldName = getQuestionFieldName(question, true);
expect(fieldName).toBe("questions.question-Question 321");
expect(fieldName).toBe("questions.question-1");
});

it("getQuestionFieldName: singleName", () => {
const fieldName = getQuestionFieldName(question, false);
expect(fieldName).toBe("question-Question 321");
expect(fieldName).toBe("question-1");
});
});
2 changes: 1 addition & 1 deletion client/src/app/pages/assessment/form-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export const getCommentFieldName = (section: Section, fullName: boolean) => {
};

export const getQuestionFieldName = (question: Question, fullName: boolean) => {
const fieldName = `question-${question.text}`;
const fieldName = `question-${question.order}`;
return fullName ? `${QUESTIONS_KEY}.${fieldName}` : fieldName;
};
Loading

0 comments on commit 598438b

Please sign in to comment.