Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Not started for archived assessments #1657

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/src/app/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
identity?: Ref;
createTime?: string;
createUser?: string;
id: any;

Check warning on line 215 in client/src/app/api/models.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

Unexpected any. Specify a different type
enabled: boolean;
}

Expand Down Expand Up @@ -369,7 +369,7 @@

export interface TaskgroupTask {
name: string;
data: any;

Check warning on line 372 in client/src/app/api/models.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.x)

Unexpected any. Specify a different type
application: Ref;
}

Expand Down Expand Up @@ -788,6 +788,7 @@

export interface AssessmentWithSectionOrder extends Assessment {
sections: SectionWithQuestionOrder[];
required?: boolean;
}

export interface AssessmentWithArchetypeApplications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const ApplicationAssessmentStatus: React.FC<
isFetching: isFetchingAssessmentsById,
fetchError,
} = useFetchAssessmentsByItemId(false, application.id);
console.log("assessments", assessments);

if (fetchError) {
return <EmptyTextMessage message={t("terms.notAvailable")} />;
Expand Down
100 changes: 83 additions & 17 deletions client/src/app/queries/assessments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getAssessmentById,
getAssessments,
getAssessmentsByItemId,
getQuestionnaires,
updateAssessment,
} from "@app/api/rest";
import { AxiosError } from "axios";
Expand All @@ -21,6 +22,7 @@ import {
AssessmentWithArchetypeApplications,
AssessmentWithSectionOrder,
InitialAssessment,
Questionnaire,
} from "@app/api/models";
import { QuestionnairesQueryKey } from "./questionnaires";
import { ARCHETYPE_QUERY_KEY } from "./archetypes";
Expand Down Expand Up @@ -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 => {
Expand Down
Loading