Skip to content

Commit

Permalink
🐛 Sort Questionnaire items on fetch (#1593)
Browse files Browse the repository at this point in the history
A questionnaire's sections, questions and answers all have a order key.
The key needs to be present in the defining yaml file, but does not need
to be in sorted order. To ensure that the UI always has questionnaire
items in proper order order, sort everything in the fetch hooks.

Note: Hub returns the questionnaire in the same order as the original
yaml file. We need to make sure things are sorted properly.

Signed-off-by: Scott J Dickerson <[email protected]>
  • Loading branch information
sjd78 authored Dec 7, 2023
1 parent cc14acb commit f6dc53e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions client/src/app/queries/questionnaires.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,31 @@ import saveAs from "file-saver";
export const QuestionnairesQueryKey = "questionnaires";
export const QuestionnaireByIdQueryKey = "questionnaireById";

/**
* For a Questionnaire, walk the structure and sort lists by order if the items
* in that list have an order. Hub stores things in the document order not logical
* order. UI needs to have things in logical order.
*/
function inPlaceSortByOrder(q: Questionnaire) {
q.sections.sort((a, b) => a.order - b.order);
q.sections.forEach((s) => {
s.questions.sort((a, b) => a.order - b.order);
s.questions.forEach((q) => {
q.answers.sort((a, b) => a.order - b.order);
});
});
return q;
}

export const useFetchQuestionnaires = () => {
const { isLoading, data, error } = useQuery({
queryKey: [QuestionnairesQueryKey],
queryFn: getQuestionnaires,
onError: (error: AxiosError) => console.log("error, ", error),
select: (questionnaires) => {
questionnaires.forEach((q) => inPlaceSortByOrder(q));
return questionnaires;
},
});
return {
questionnaires: data || [],
Expand Down Expand Up @@ -72,7 +92,9 @@ export const useFetchQuestionnaireById = (id: number | string) => {
queryKey: [QuestionnaireByIdQueryKey, id],
queryFn: () => getQuestionnaireById<Questionnaire>(id),
onError: (error: AxiosError) => console.log("error, ", error),
select: (q) => inPlaceSortByOrder(q),
});

return {
questionnaire: data,
isFetching: isLoading,
Expand Down

0 comments on commit f6dc53e

Please sign in to comment.