@openmrs/esm-fast-data-entry-app 1.0.1-pre.180 → 1.0.1-pre.184

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
+ import useSWR from 'swr';
3
+ import { type SpecificQuestion, type SpecificQuestionConfig } from '../types';
4
+ import { useMemo } from 'react';
5
+
6
+ const formUrl = `${restBaseUrl}/o3/forms`;
7
+
8
+ export const useSpecificQuestions = (formUuid: string, specificQuestionConfig: Array<SpecificQuestionConfig>) => {
9
+ const specificQuestionsToLoad = useMemo(
10
+ () => getQuestionByFormId(formUuid, specificQuestionConfig),
11
+ [formUuid, specificQuestionConfig],
12
+ );
13
+
14
+ const { data, error } = useSWR<FetchResponse, Error>(
15
+ specificQuestionsToLoad ? `${formUrl}/${formUuid}` : null,
16
+ openmrsFetch,
17
+ );
18
+
19
+ const specificQuestions = getQuestions(specificQuestionsToLoad, data?.data);
20
+
21
+ return {
22
+ questions: specificQuestions || null,
23
+ isError: error,
24
+ isLoading: !data && !error,
25
+ };
26
+ };
27
+
28
+ function getQuestionByFormId(formUuid: string, specificQuestionConfig: Array<SpecificQuestionConfig>) {
29
+ return specificQuestionConfig.filter((question) => question.forms.includes(formUuid));
30
+ }
31
+
32
+ function getQuestions(specificQuestions: Array<SpecificQuestionConfig>, formSchema): Array<SpecificQuestion> {
33
+ if (!formSchema || specificQuestions.length <= 0) {
34
+ return [];
35
+ }
36
+
37
+ const specificQuestionsMap = new Map<string, SpecificQuestionConfig>(
38
+ specificQuestions.map((sq) => [sq.questionId, sq]),
39
+ );
40
+
41
+ const questionIds = new Set(specificQuestionsMap.keys());
42
+ const conceptLabels = formSchema.conceptReferences;
43
+
44
+ return formSchema.pages.flatMap((page) =>
45
+ page.sections.flatMap((section) =>
46
+ section.questions
47
+ .filter((question) => questionIds.has(question.id))
48
+ .map((question) => {
49
+ const specificQuestion = specificQuestionsMap.get(question.id) || {};
50
+
51
+ return {
52
+ question: {
53
+ display: question.label ?? conceptLabels[question.questionOptions.concept]?.display,
54
+ id: question.id,
55
+ disabled: (specificQuestion as SpecificQuestionConfig).disabled,
56
+ defaultAnswer: (specificQuestion as SpecificQuestionConfig).defaultAnswer,
57
+ },
58
+ answers: (question.questionOptions.answers ?? []).map((answer) => ({
59
+ value: answer.concept,
60
+ display: answer.label ?? conceptLabels[answer.concept]?.display,
61
+ })),
62
+ };
63
+ }),
64
+ ),
65
+ );
66
+ }
67
+
68
+ export default useSpecificQuestions;
package/src/types.ts CHANGED
@@ -7,6 +7,8 @@ export interface SpecificQuestion {
7
7
  question: {
8
8
  id: string;
9
9
  display: string;
10
+ defaultAnswer?: string;
11
+ disabled?: boolean;
10
12
  };
11
13
  answers: Array<{
12
14
  value: string;
@@ -17,4 +19,6 @@ export interface SpecificQuestion {
17
19
  export interface SpecificQuestionConfig {
18
20
  forms: Array<string>;
19
21
  questionId: string;
22
+ defaultAnswer?: string;
23
+ disabled?: boolean;
20
24
  }
package/turbo.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://turbo.build/schema.json",
3
- "pipeline": {
3
+ "tasks": {
4
4
  "build": {
5
5
  "outputs": ["dist/**"]
6
6
  },
@@ -1,56 +0,0 @@
1
- import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
- import useSWR from 'swr';
3
- import { type SpecificQuestion, type SpecificQuestionConfig } from '../types';
4
- import { useMemo } from 'react';
5
-
6
- const formUrl = `${restBaseUrl}/o3/forms`;
7
-
8
- export const useSpecificQuestions = (formUuid: string, specificQuestionConfig: Array<SpecificQuestionConfig>) => {
9
- const specificQuestionsToLoad = useMemo(
10
- () => getQuestionIdsByFormId(formUuid, specificQuestionConfig),
11
- [formUuid, specificQuestionConfig],
12
- );
13
-
14
- const { data, error } = useSWR<FetchResponse, Error>(
15
- specificQuestionsToLoad ? `${formUrl}/${formUuid}` : null,
16
- openmrsFetch,
17
- );
18
-
19
- const specificQuestions = getQuestionsByIds(specificQuestionsToLoad, data?.data);
20
-
21
- return {
22
- questions: specificQuestions || null,
23
- isError: error,
24
- isLoading: !data && !error,
25
- };
26
- };
27
-
28
- function getQuestionIdsByFormId(formUuid: string, specificQuestionConfig: Array<SpecificQuestionConfig>) {
29
- const matchingQuestions = specificQuestionConfig.filter((question) => question.forms.includes(formUuid));
30
- return matchingQuestions.map((question) => question.questionId);
31
- }
32
-
33
- function getQuestionsByIds(questionIds, formSchema): Array<SpecificQuestion> {
34
- if (!formSchema || questionIds.lenght <= 0) {
35
- return [];
36
- }
37
- const conceptLabels = formSchema.conceptReferences;
38
- return formSchema.pages.flatMap((page) =>
39
- page.sections.flatMap((section) =>
40
- section.questions
41
- .filter((question) => questionIds.includes(question.id))
42
- .map((question) => ({
43
- question: {
44
- display: question.label ?? conceptLabels[question.questionOptions.concept]?.display,
45
- id: question.id,
46
- },
47
- answers: (question.questionOptions.answers ?? []).map((answer) => ({
48
- value: answer.concept,
49
- display: answer.label ?? conceptLabels[answer.concept]?.display,
50
- })),
51
- })),
52
- ),
53
- );
54
- }
55
-
56
- export default useSpecificQuestions;