From da679f6eb6d0bb054cea204600375a70a6139328 Mon Sep 17 00:00:00 2001 From: ibolton336 Date: Thu, 4 Jan 2024 21:21:26 -0500 Subject: [PATCH] wip --- client/src/app/api/models.ts | 1 + .../application-assessment-status.tsx | 1 + client/src/app/queries/assessments.ts | 100 +++++++++++++++--- 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/client/src/app/api/models.ts b/client/src/app/api/models.ts index e3107eff72..bc8a01024a 100644 --- a/client/src/app/api/models.ts +++ b/client/src/app/api/models.ts @@ -788,6 +788,7 @@ export interface SectionWithQuestionOrder extends Section { export interface AssessmentWithSectionOrder extends Assessment { sections: SectionWithQuestionOrder[]; + required?: boolean; } export interface AssessmentWithArchetypeApplications diff --git a/client/src/app/pages/applications/components/application-assessment-status/application-assessment-status.tsx b/client/src/app/pages/applications/components/application-assessment-status/application-assessment-status.tsx index 56b6cf738f..48ab7ce8d6 100644 --- a/client/src/app/pages/applications/components/application-assessment-status/application-assessment-status.tsx +++ b/client/src/app/pages/applications/components/application-assessment-status/application-assessment-status.tsx @@ -35,6 +35,7 @@ export const ApplicationAssessmentStatus: React.FC< isFetching: isFetchingAssessmentsById, fetchError, } = useFetchAssessmentsByItemId(false, application.id); + console.log("assessments", assessments); if (fetchError) { return ; diff --git a/client/src/app/queries/assessments.ts b/client/src/app/queries/assessments.ts index 8128ba75b4..955d4966bb 100644 --- a/client/src/app/queries/assessments.ts +++ b/client/src/app/queries/assessments.ts @@ -13,6 +13,7 @@ import { getAssessmentById, getAssessments, getAssessmentsByItemId, + getQuestionnaires, updateAssessment, } from "@app/api/rest"; import { AxiosError } from "axios"; @@ -21,6 +22,7 @@ import { AssessmentWithArchetypeApplications, AssessmentWithSectionOrder, InitialAssessment, + Questionnaire, } from "@app/api/models"; import { QuestionnairesQueryKey } from "./questionnaires"; import { ARCHETYPE_QUERY_KEY } from "./archetypes"; @@ -161,37 +163,101 @@ export const useFetchAssessmentById = (id?: number | string) => { }; }; +// export const useFetchAssessmentsByItemId = ( +// isArchetype: boolean, +// itemId?: number | string +// ) => { +// const { data, isLoading, error } = useQuery({ +// queryKey: [assessmentsByItemIdQueryKey, itemId, isArchetype], +// queryFn: () => getAssessmentsByItemId(isArchetype, itemId), +// onError: (error: AxiosError) => console.log("error, ", error), +// onSuccess: (_data) => {}, +// enabled: !!itemId, +// }); + +// const queryClient = useQueryClient(); + +// const invalidateAssessmentsQuery = () => { +// queryClient.invalidateQueries([ +// assessmentsByItemIdQueryKey, +// itemId, +// isArchetype, +// ]); +// }; +// const assessmentsWithOrder: AssessmentWithSectionOrder[] = +// data?.map(addSectionOrderToQuestions) || []; +// return { +// assessments: assessmentsWithOrder, +// isFetching: isLoading, +// fetchError: error, +// invalidateAssessmentsQuery, +// }; +// }; + export const useFetchAssessmentsByItemId = ( isArchetype: boolean, itemId?: number | string ) => { - const { data, isLoading, error } = useQuery({ - queryKey: [assessmentsByItemIdQueryKey, itemId, isArchetype], - queryFn: () => getAssessmentsByItemId(isArchetype, itemId), - onError: (error: AxiosError) => console.log("error, ", error), - onSuccess: (_data) => {}, - enabled: !!itemId, + const queryClient = useQueryClient(); + + const results = useQueries({ + queries: [ + { + queryKey: ["assessmentsByItemId", itemId, isArchetype], + queryFn: () => getAssessmentsByItemId(isArchetype, itemId), + onError: (error: unknown) => console.log("error, ", error), + enabled: !!itemId, + }, + { + queryKey: ["allQuestionnaires"], + queryFn: getQuestionnaires, + }, + ], }); - const queryClient = useQueryClient(); + const [assessmentsResult, questionnairesResult] = results; + const assessmentsData = assessmentsResult.data || []; + const questionnairesData = questionnairesResult.data || []; + + const enrichedAssessments = assessmentsData.map((assessment) => { + return enrichAssessment(assessment, questionnairesData); + }); const invalidateAssessmentsQuery = () => { - queryClient.invalidateQueries([ - assessmentsByItemIdQueryKey, - itemId, - isArchetype, - ]); + queryClient.invalidateQueries(["assessmentsByItemId", itemId, isArchetype]); + queryClient.invalidateQueries(["allQuestionnaires"]); }; - const assessmentsWithOrder: AssessmentWithSectionOrder[] = - data?.map(addSectionOrderToQuestions) || []; + return { - assessments: assessmentsWithOrder, - isFetching: isLoading, - fetchError: error, + assessments: enrichedAssessments, + isFetching: assessmentsResult.isLoading || questionnairesResult.isLoading, + fetchError: assessmentsResult.error || questionnairesResult.error, invalidateAssessmentsQuery, }; }; +export const enrichAssessment = ( + assessment: Assessment, + questionnaires: Questionnaire[] +): AssessmentWithSectionOrder => { + const questionnaireRef = assessment.questionnaire; // Adjust to your data structure + const matchedQuestionnaire = questionnaires.find( + (q) => q.id === questionnaireRef.id || q.name === questionnaireRef.name + ); + + return { + ...assessment, + required: matchedQuestionnaire ? matchedQuestionnaire.required : false, + sections: assessment.sections.map((section) => ({ + ...section, + questions: section.questions.map((question) => ({ + ...question, + sectionOrder: section.order, + })), + })), + }; +}; + export const addSectionOrderToQuestions = ( assessment: Assessment ): AssessmentWithSectionOrder => {