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

🐛 Sort Questionnaire items on fetch #1593

Merged
merged 1 commit into from
Dec 7, 2023
Merged
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
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 @@
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);

Check warning on line 29 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L24-L29

Added lines #L24 - L29 were not covered by tests
});
});
return q;

Check warning on line 32 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L32

Added line #L32 was not covered by tests
}

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;

Check warning on line 42 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L40-L42

Added lines #L40 - L42 were not covered by tests
},
});
return {
questionnaires: data || [],
Expand Down Expand Up @@ -72,7 +92,9 @@
queryKey: [QuestionnaireByIdQueryKey, id],
queryFn: () => getQuestionnaireById<Questionnaire>(id),
onError: (error: AxiosError) => console.log("error, ", error),
select: (q) => inPlaceSortByOrder(q),

Check warning on line 95 in client/src/app/queries/questionnaires.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/queries/questionnaires.ts#L95

Added line #L95 was not covered by tests
});

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