Skip to content

Commit f6dc53e

Browse files
authored
🐛 Sort Questionnaire items on fetch (#1593)
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]>
1 parent cc14acb commit f6dc53e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

client/src/app/queries/questionnaires.ts

+22
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,31 @@ import saveAs from "file-saver";
1616
export const QuestionnairesQueryKey = "questionnaires";
1717
export const QuestionnaireByIdQueryKey = "questionnaireById";
1818

19+
/**
20+
* For a Questionnaire, walk the structure and sort lists by order if the items
21+
* in that list have an order. Hub stores things in the document order not logical
22+
* order. UI needs to have things in logical order.
23+
*/
24+
function inPlaceSortByOrder(q: Questionnaire) {
25+
q.sections.sort((a, b) => a.order - b.order);
26+
q.sections.forEach((s) => {
27+
s.questions.sort((a, b) => a.order - b.order);
28+
s.questions.forEach((q) => {
29+
q.answers.sort((a, b) => a.order - b.order);
30+
});
31+
});
32+
return q;
33+
}
34+
1935
export const useFetchQuestionnaires = () => {
2036
const { isLoading, data, error } = useQuery({
2137
queryKey: [QuestionnairesQueryKey],
2238
queryFn: getQuestionnaires,
2339
onError: (error: AxiosError) => console.log("error, ", error),
40+
select: (questionnaires) => {
41+
questionnaires.forEach((q) => inPlaceSortByOrder(q));
42+
return questionnaires;
43+
},
2444
});
2545
return {
2646
questionnaires: data || [],
@@ -72,7 +92,9 @@ export const useFetchQuestionnaireById = (id: number | string) => {
7292
queryKey: [QuestionnaireByIdQueryKey, id],
7393
queryFn: () => getQuestionnaireById<Questionnaire>(id),
7494
onError: (error: AxiosError) => console.log("error, ", error),
95+
select: (q) => inPlaceSortByOrder(q),
7596
});
97+
7698
return {
7799
questionnaire: data,
78100
isFetching: isLoading,

0 commit comments

Comments
 (0)