Skip to content

Commit

Permalink
✨ Enhance identified-risks table to include archetype apps in calcula…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
ibolton336 committed Nov 27, 2023
1 parent 82a704c commit 9bbc35e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
4 changes: 4 additions & 0 deletions client/src/app/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,7 @@ export interface SectionWithQuestionOrder extends Section {
export interface AssessmentWithSectionOrder extends Assessment {
sections: SectionWithQuestionOrder[];
}

export interface AssessmentWithArchetypeApplications extends Assessment {
archetypeApplications: Ref[];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { Table, Tbody, Td, Th, Thead, Tr } from "@patternfly/react-table";
import { useFetchAssessments } from "@app/queries/assessments";
import { useFetchAssessmentsWithArchetypeApplications } from "@app/queries/assessments";
import { useTranslation } from "react-i18next";
import { Ref } from "@app/api/models";
import { NoDataEmptyState } from "@app/components/NoDataEmptyState";
Expand All @@ -20,7 +20,8 @@ export const IdentifiedRisksTable: React.FC<
> = () => {
const { t } = useTranslation();

const { assessments } = useFetchAssessments();
const { assessmentsWithArchetypeApplications } =
useFetchAssessmentsWithArchetypeApplications();

interface ITableRowData {
assessmentName: string;
Expand All @@ -34,7 +35,25 @@ export const IdentifiedRisksTable: React.FC<
const tableData: ITableRowData[] = [];

// ...
assessments.forEach((assessment) => {
assessmentsWithArchetypeApplications.forEach((assessment) => {
let combinedApplications = assessment.application
? [assessment.application]
: [];

combinedApplications = combinedApplications.concat(
assessment.archetypeApplications || []
);

const uniqueApplications = combinedApplications.reduce(
(acc: Ref[], current) => {
if (!acc.find((item) => item?.id === current.id)) {
// Assuming 'id' is the unique identifier
acc.push(current);
}
return acc;
},
[]
);
assessment.sections.forEach((section) => {
section.questions.forEach((question) => {
question.answers.forEach((answer) => {
Expand All @@ -52,14 +71,15 @@ export const IdentifiedRisksTable: React.FC<

if (existingItemIndex !== -1) {
const existingItem = tableData[existingItemIndex];
if (
assessment.application &&
!existingItem.applications
.map((app) => app.name)
.includes(assessment.application.name)
) {
existingItem.applications.push(assessment.application);
}
uniqueApplications.forEach((application) => {
if (
!existingItem.applications.some(
(app) => app.id === application.id
)
) {
existingItem.applications.push(application);
}
});
} else {
tableData.push({
section: section.name,
Expand Down Expand Up @@ -150,7 +170,7 @@ export const IdentifiedRisksTable: React.FC<
</Tr>
</Thead>
<ConditionalTableBody
isNoData={assessments?.length === 0}
isNoData={assessmentsWithArchetypeApplications?.length === 0}
numRenderedColumns={numRenderedColumns}
noDataEmptyState={
<div>
Expand Down
46 changes: 45 additions & 1 deletion client/src/app/queries/assessments.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useMemo } from "react";
import {
useMutation,
useQueries,
useQuery,
useQueryClient,
} from "@tanstack/react-query";

import {
createAssessment,
deleteAssessment,
getArchetypeById,
getAssessmentById,
getAssessments,
getAssessmentsByItemId,
Expand All @@ -13,6 +19,7 @@ import { AxiosError } from "axios";
import {
Assessment,
AssessmentWithSectionOrder,
AssessmentWithArchetypeApplications,
InitialAssessment,
} from "@app/api/models";
import { QuestionnairesQueryKey } from "./questionnaires";
Expand Down Expand Up @@ -210,3 +217,40 @@ const removeSectionOrderFromQuestions = (
})),
};
};

export const useFetchAssessmentsWithArchetypeApplications = () => {
const { assessments, isFetching: assessmentsLoading } = useFetchAssessments();

const archetypeQueries = useQueries({
queries:
assessments?.map((assessment) => ({
queryKey: ["archetype", assessment.archetype?.id],
queryFn: () =>
assessment.archetype?.id
? getArchetypeById(assessment.archetype.id)
: undefined,
enabled: !!assessment.archetype?.id,
})) || [],
});

const isArchetypesLoading = archetypeQueries.some((query) => query.isLoading);
const archetypesData = archetypeQueries
.map((query) => query.data)
.filter(Boolean);

const assessmentsWithArchetypeApplications: AssessmentWithArchetypeApplications[] =
assessments.map((assessment, index) => {
const archetypeInfo = archetypesData[index];
return {
...assessment,
archetypeApplications: archetypeInfo.applications
? archetypeInfo.applications
: [],
};
});

return {
assessmentsWithArchetypeApplications,
isLoading: assessmentsLoading || isArchetypesLoading,
};
};

0 comments on commit 9bbc35e

Please sign in to comment.