-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathuseFormFieldsMeta.ts
61 lines (55 loc) · 2.23 KB
/
useFormFieldsMeta.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { type FormField } from '../types';
import { useMemo } from 'react';
import { codedTypes } from '../constants';
import { findConceptByReference } from '../utils/form-helper';
import { type OpenmrsResource } from '@openmrs/esm-framework';
export function useFormFieldsMeta(rawFormFields: FormField[], concepts: OpenmrsResource[]) {
const formFields = useMemo(() => {
if (rawFormFields.length && concepts?.length) {
return rawFormFields.map((field) => {
const matchingConcept = findConceptByReference(field.questionOptions.concept, concepts);
field.questionOptions.concept = matchingConcept ? matchingConcept.uuid : field.questionOptions.concept;
field.label = field.label ? field.label : matchingConcept?.display;
if (matchingConcept) {
const { lowAbsolute, hiAbsolute } = matchingConcept;
if (lowAbsolute !== undefined) {
field.questionOptions.min = lowAbsolute;
}
if (hiAbsolute !== undefined) {
field.questionOptions.max = hiAbsolute;
}
}
if (
codedTypes.includes(field.questionOptions.rendering) &&
!field.questionOptions.answers?.length &&
matchingConcept?.conceptClass?.display === 'Question' &&
matchingConcept?.answers?.length
) {
field.questionOptions.answers = matchingConcept.answers.map((answer) => {
return {
concept: answer?.uuid,
label: answer?.display,
};
});
}
field.meta = {
...(field.meta || {}),
concept: matchingConcept,
};
if (field.questionOptions.answers) {
field.questionOptions.answers = field.questionOptions.answers.map((answer) => {
const matchingAnswerConcept = findConceptByReference(answer.concept, concepts);
return {
...answer,
concept: matchingAnswerConcept ? matchingAnswerConcept.uuid : answer.concept,
label: answer.label ? answer.label : matchingAnswerConcept?.display,
};
});
}
return field;
});
}
return rawFormFields ?? [];
}, [concepts, rawFormFields]);
return formFields;
}