Skip to content

Commit

Permalink
Move app archetype filter logic to within the query hook for archetypes
Browse files Browse the repository at this point in the history
Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 committed Jan 19, 2024
1 parent 9bd01af commit 866bbc5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ export const ApplicationDetailDrawer: React.FC<
{t("terms.archetypesAssessed")}
</DescriptionListTerm>
<DescriptionListDescription>
<AssessedArchetypes
archetypeRefs={application?.archetypes}
/>
<AssessedArchetypes application={application} />
</DescriptionListDescription>
</DescriptionListGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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";
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<IAssessedArchetypesProps> = ({
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,
Expand Down
25 changes: 20 additions & 5 deletions client/src/app/queries/archetypes.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Archetype[]>([]);

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]);
Expand All @@ -35,7 +50,7 @@ export const useFetchArchetypes = () => {
});

return {
archetypes: data || [],
archetypes: filteredArchetypes || [],
isFetching: isLoading,
isSuccess,
error,
Expand Down

0 comments on commit 866bbc5

Please sign in to comment.