From 866bbc5445462d69bec0b175ddf313969c4d6d33 Mon Sep 17 00:00:00 2001 From: ibolton336 Date: Fri, 19 Jan 2024 16:56:03 -0500 Subject: [PATCH] Move app archetype filter logic to within the query hook for archetypes Signed-off-by: ibolton336 --- .../application-detail-drawer.tsx | 4 +-- .../components/assessed-archetypes.tsx | 14 +++++------ client/src/app/queries/archetypes.ts | 25 +++++++++++++++---- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/client/src/app/pages/applications/components/application-detail-drawer/application-detail-drawer.tsx b/client/src/app/pages/applications/components/application-detail-drawer/application-detail-drawer.tsx index 7b4c096758..6219a3e0f4 100644 --- a/client/src/app/pages/applications/components/application-detail-drawer/application-detail-drawer.tsx +++ b/client/src/app/pages/applications/components/application-detail-drawer/application-detail-drawer.tsx @@ -204,9 +204,7 @@ export const ApplicationDetailDrawer: React.FC< {t("terms.archetypesAssessed")} - + diff --git a/client/src/app/pages/applications/components/application-detail-drawer/components/assessed-archetypes.tsx b/client/src/app/pages/applications/components/application-detail-drawer/components/assessed-archetypes.tsx index 723ff91ffa..89503af756 100644 --- a/client/src/app/pages/applications/components/application-detail-drawer/components/assessed-archetypes.tsx +++ b/client/src/app/pages/applications/components/application-detail-drawer/components/assessed-archetypes.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { Ref } from "@app/api/models"; +import { Application } from "@app/api/models"; import { Label, LabelGroup, Spinner } from "@patternfly/react-core"; import { EmptyTextMessage } from "@app/components/EmptyTextMessage"; import { useTranslation } from "react-i18next"; @@ -7,17 +7,17 @@ import { useFetchArchetypes } from "@app/queries/archetypes"; import { useFetchAllAssessmentsWithArchetypes } from "@app/queries/assessments"; interface IAssessedArchetypesProps { - archetypeRefs: Ref[] | undefined; + application: Application | null; } export const AssessedArchetypes: React.FC = ({ - archetypeRefs, + application, }) => { const { t } = useTranslation(); - const { archetypes, isFetching: isFetchingArchetypes } = useFetchArchetypes(); - const applicationArchetypes = archetypes.filter( - (archetype) => archetypeRefs?.some((ref) => ref.id === archetype.id) - ); + const { + archetypes: applicationArchetypes, + isFetching: isFetchingArchetypes, + } = useFetchArchetypes(application); const { assessmentsWithArchetypes, diff --git a/client/src/app/queries/archetypes.ts b/client/src/app/queries/archetypes.ts index 7b0de34b64..e2d6efc470 100644 --- a/client/src/app/queries/archetypes.ts +++ b/client/src/app/queries/archetypes.ts @@ -1,7 +1,7 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { AxiosError } from "axios"; -import { Archetype } from "@app/api/models"; +import { Application, Archetype } from "@app/api/models"; import { createArchetype, deleteArchetype, @@ -14,18 +14,33 @@ import { assessmentsByItemIdQueryKey, } from "./assessments"; import { reviewsQueryKey } from "./reviews"; +import { useState } from "react"; export const ARCHETYPES_QUERY_KEY = "archetypes"; export const ARCHETYPE_QUERY_KEY = "archetype"; -export const useFetchArchetypes = () => { +export const useFetchArchetypes = (forApplication?: Application | null) => { + const [filteredArchetypes, setFilteredArchetypes] = useState([]); + const queryClient = useQueryClient(); const { isLoading, isSuccess, error, refetch, data } = useQuery({ initialData: [], - queryKey: [ARCHETYPES_QUERY_KEY], + queryKey: [ARCHETYPES_QUERY_KEY, forApplication?.id], + queryFn: getArchetypes, refetchInterval: 5000, - onSuccess: () => { + onSuccess: (fetchedArchetypes) => { + if (forApplication && forApplication.archetypes) { + const archetypeIds = forApplication.archetypes.map( + (archetype) => archetype.id + ); + const filtered = fetchedArchetypes.filter((archetype) => + archetypeIds.includes(archetype.id) + ); + setFilteredArchetypes(filtered); + } else { + setFilteredArchetypes(fetchedArchetypes); + } queryClient.invalidateQueries([reviewsQueryKey]); queryClient.invalidateQueries([assessmentsQueryKey]); queryClient.invalidateQueries([assessmentsByItemIdQueryKey]); @@ -35,7 +50,7 @@ export const useFetchArchetypes = () => { }); return { - archetypes: data || [], + archetypes: filteredArchetypes || [], isFetching: isLoading, isSuccess, error,