Skip to content

Commit

Permalink
03-2167:adding concept to the meta object
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcliff committed Apr 22, 2024
1 parent e5691b9 commit aed3e64
Show file tree
Hide file tree
Showing 46 changed files with 112 additions and 68 deletions.
32 changes: 32 additions & 0 deletions src/api/OHRIFormField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { OpenmrsResource } from '@openmrs/esm-framework';
import { OHRIFormQuestionOptions, HideProps } from './types';

export interface OHRIFormField {
label: string;
type: string;
questionOptions: OHRIFormQuestionOptions;
id: string;
uuid?: string;
groupId?: string;
questions?: Array<OHRIFormField>;
value?: any;
hide?: HideProps;
isHidden?: boolean;
isParentHidden?: boolean;
fieldDependants?: Set<string>;
pageDependants?: Set<string>;
sectionDependants?: Set<string>;
required?: boolean;
unspecified?: boolean;
disabled?: boolean;
readonly?: string | boolean;
inlineRendering?: 'single-line' | 'multiline' | 'automatic';
validators?: Array<Record<string, any>>;
behaviours?: Array<Record<string, any>>;
questionInfo?: string;
constrainMaxWidth?: boolean;
meta?: {
concept?: OpenmrsResource;
[anythingElse: string]: any;
};
}
5 changes: 4 additions & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export interface OHRIFormField {
behaviours?: Array<Record<string, any>>;
questionInfo?: string;
constrainMaxWidth?: boolean;
meta?:{ concept? : OpenmrsResource};
meta?: {
concept? : OpenmrsResource,
[anythingElse: string]: any;
};
}

export interface previousValue {
Expand Down
8 changes: 4 additions & 4 deletions src/components/encounter/ohri-encounter-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import React, { Dispatch, SetStateAction, useCallback, useEffect, useMemo, useSt
import { SessionLocation, showToast, useLayoutType, Visit } from '@openmrs/esm-framework';
import { ConceptFalse, ConceptTrue } from '../../constants';
import {
OHRIFormField,
OHRIFormPage as OHRIFormPageProps,
OHRIFormSchema,
OpenmrsEncounter,
SessionMode,
ValidationResult,
RepeatObsGroupCounter,
} from '../../api/types';
import { OHRIFormField } from '../../api/OHRIFormField';
import OHRIFormPage from '../page/ohri-form-page';
import { OHRIFormContext } from '../../ohri-form-context';
import {
Expand Down Expand Up @@ -263,9 +263,9 @@ export const OHRIEncounterForm: React.FC<OHRIEncounterFormProps> = ({
const matchingConcept = findConceptByReference(field.questionOptions.concept, concepts);
field.questionOptions.concept = matchingConcept ? matchingConcept.uuid : field.questionOptions.concept;
field.label = field.label ? field.label : matchingConcept?.display;
field.meta ={
concept: matchingConcept
}
field.meta = {
concept: matchingConcept,
};
if (field.questionOptions.answers) {
field.questionOptions.answers = field.questionOptions.answers.map((answer) => {
const matchingAnswerConcept = findConceptByReference(answer.concept, concepts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { render, fireEvent, screen, cleanup, act, waitFor } from '@testing-library/react';
import { Form, Formik } from 'formik';
import { EncounterContext, OHRIFormContext } from '../../../ohri-form-context';
import { OHRIFormField } from '../../../api/types';
import { OHRIFormField } from '../../../api/OHRIFormField';
import { ObsSubmissionHandler } from '../../../submission-handlers/base-handlers';
import { OHRIContentSwitcher } from './ohri-content-switcher.component';

Expand Down
1 change: 0 additions & 1 deletion src/components/inputs/date/ohri-date.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const OHRIDate: React.FC<OHRIFormFieldProps> = ({ question, onChange, handler, p
const isFieldRequiredError = useMemo(() => errors[0]?.errCode == fieldRequiredErrCode, [errors]);
const [previousValueForReview, setPreviousValueForReview] = useState(null);
const [time, setTime] = useState('');

useEffect(() => {
if (question['submission']) {
question['submission'].errors && setErrors(question['submission'].errors);
Expand Down
1 change: 0 additions & 1 deletion src/components/inputs/file/file.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const File: React.FC<FileProps> = ({ question, onChange, handler }) => {
const [selectedFiles, setSelectedFiles] = useState(null); // Add state for selected files
const [imagePreview, setImagePreview] = useState(null);
const [uploadMode, setUploadMode] = useState<AllowedModes>('');

useEffect(() => {
if (encounterContext.sessionMode === 'edit') {
setUploadMode('edit');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createErrorHandler } from '@openmrs/esm-framework';
import { isInlineView } from '../../../utils/ohri-form-helper';
import { getAllLocations, getLocationsByTag } from '../../../api/api';
import { isTrue } from '../../../utils/boolean-utils';
import { OHRIFormField } from '../../../api/types';
import { OHRIFormField } from '../../../api/OHRIFormField';
import { OHRIFormContext } from '../../../ohri-form-context';
import { OHRIFieldValueView } from '../../value/view/ohri-field-value-view.component';
import styles from './ohri-encounter-location.scss';
Expand Down
2 changes: 0 additions & 2 deletions src/components/inputs/radio/ohri-radio.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const OHRIRadio: React.FC<OHRIFormFieldProps> = ({ question, onChange, handler,
const [errors, setErrors] = useState([]);
const isFieldRequiredError = useMemo(() => errors[0]?.errCode == fieldRequiredErrCode, [errors]);
const [warnings, setWarnings] = useState([]);


useEffect(() => {
if (question['submission']) {
question['submission'].errors && setErrors(question['submission'].errors);
Expand Down
1 change: 0 additions & 1 deletion src/components/inputs/select/ohri-dropdown.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const OHRIDropdown: React.FC<OHRIFormFieldProps> = ({ question, onChange, handle
const [errors, setErrors] = useState([]);
const isFieldRequiredError = useMemo(() => errors[0]?.errCode == fieldRequiredErrCode, [errors]);
const [warnings, setWarnings] = useState([]);

useEffect(() => {
if (question['submission']) {
question['submission'].errors && setErrors(question['submission'].errors);
Expand Down
5 changes: 2 additions & 3 deletions src/components/inputs/select/ohri-dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render, fireEvent, screen, cleanup, act } from '@testing-library/react'
import { Form, Formik } from 'formik';
import { EncounterContext, OHRIFormContext } from '../../../ohri-form-context';
import OHRIDropdown from './ohri-dropdown.component';
import { OHRIFormField } from '../../../api/types';
import { OHRIFormField } from '../../../api/OHRIFormField';
import { ObsSubmissionHandler } from '../../../submission-handlers/base-handlers';

const question: OHRIFormField = {
Expand Down Expand Up @@ -67,8 +67,7 @@ const renderForm = (intialValues) => {
isFieldInitializationComplete: true,
isSubmitting: false,
formFieldHandlers: { obs: ObsSubmissionHandler },
}}
>
}}>
<OHRIDropdown question={question} onChange={jest.fn()} handler={ObsSubmissionHandler} />
</OHRIFormContext.Provider>
</Form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const OHRITextArea: React.FC<OHRIFormFieldProps> = ({
const [errors, setErrors] = useState([]);
const isFieldRequiredError = useMemo(() => errors[0]?.errCode == fieldRequiredErrCode, [errors]);
const [warnings, setWarnings] = useState([]);

useEffect(() => {
if (question['submission']) {
question['submission'].errors && setErrors(question['submission'].errors);
Expand Down
2 changes: 0 additions & 2 deletions src/components/inputs/text/ohri-text.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const OHRIText: React.FC<OHRIFormFieldProps> = ({ question, onChange, handler, p
const [errors, setErrors] = useState([]);
const [warnings, setWarnings] = useState([]);
const isFieldRequiredError = useMemo(() => errors[0]?.errCode == fieldRequiredErrCode, [errors]);


useEffect(() => {
if (question['submission']) {
question['submission'].errors && setErrors(question['submission'].errors);
Expand Down
1 change: 0 additions & 1 deletion src/components/inputs/toggle/ohri-toggle.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { booleanConceptToBoolean } from '../../../utils/common-expression-helper
const OHRIToggle: React.FC<OHRIFormFieldProps> = ({ question, onChange, handler, previousValue }) => {
const [field, meta] = useField(question.id);
const { setFieldValue, encounterContext, layoutType, workspaceLayout } = React.useContext(OHRIFormContext);

const handleChange = (value) => {
setFieldValue(question.id, value);
onChange(question.id, value, null, null);
Expand Down
2 changes: 1 addition & 1 deletion src/components/inputs/tooltip/ohri-tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Tooltip } from '@carbon/react';
import { Information } from '@carbon/react/icons';
import styles from './ohri-tooltip.scss';
import { OHRIFormField } from '../../../api/types';
import { OHRIFormField } from '../../../api/OHRIFormField';

interface OHRITooltipProps {
field: OHRIFormField;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { render, fireEvent, waitFor, act, screen } from '@testing-library/react';
import UISelectExtended from './ui-select-extended';
import { OHRIFormField } from '../../../api/types';
import { OHRIFormField } from '../../../api/OHRIFormField';
import { EncounterContext, OHRIFormContext } from '../../../ohri-form-context';
import { Form, Formik } from 'formik';
import { ObsSubmissionHandler } from '../../../submission-handlers/base-handlers';
Expand Down
2 changes: 1 addition & 1 deletion src/components/repeat/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OHRIFormField } from '../../api/types';
import { OHRIFormField } from '../../api/OHRIFormField';
import { cloneDeep } from 'lodash-es';

export function cloneObsGroup(srcField: OHRIFormField, obsGroup: any, idSuffix: number) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/repeat/ohri-repeat.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Add, TrashCan } from '@carbon/react/icons';
import { useFormikContext } from 'formik';
import { useTranslation } from 'react-i18next';
import { OHRIFormContext } from '../../ohri-form-context';
import { OHRIFormField, OHRIFormFieldProps } from '../../api/types';
import { OHRIFormFieldProps } from '../../api/types';
import { OHRIFormField } from '../../api/OHRIFormField';
import { OHRIObsGroup } from '../group/ohri-obs-group.component';
import { evaluateAsyncExpression, evaluateExpression } from '../../utils/expression-runner';
import { isEmpty } from '../../validators/ohri-form-validator';
Expand Down
2 changes: 1 addition & 1 deletion src/components/section/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { formatDate } from '@openmrs/esm-framework';
import { OHRIFormField } from '../../api/types';
import { OHRIFormField } from '../../api/OHRIFormField';
import { getRegisteredControl } from '../../registry/registry';
import { isTrue } from '../../utils/boolean-utils';

Expand Down
3 changes: 2 additions & 1 deletion src/components/section/ohri-form-section.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { ErrorBoundary } from 'react-error-boundary';
import { ToastNotification } from '@carbon/react';
import { getRegisteredFieldSubmissionHandler } from '../../registry/registry';
import { OHRIUnspecified } from '../inputs/unspecified/ohri-unspecified.component';
import { OHRIFormField, OHRIFormFieldProps, previousValue, SubmissionHandler } from '../../api/types';
import { OHRIFormFieldProps, previousValue, SubmissionHandler } from '../../api/types';
import { OHRIFormField } from '../../api/OHRIFormField';
import styles from './ohri-form-section.scss';
import { formatPreviousValueDisplayText, getFieldControlWithFallback, isUnspecifiedSupported } from './helpers';
import { OHRITooltip } from '../inputs/tooltip/ohri-tooltip';
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/useFormFieldHandlers.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { useEffect, useState } from 'react';
import { OHRIFormField, SubmissionHandler } from '../api/types';
import { SubmissionHandler } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { getRegisteredFieldSubmissionHandler } from '..';

export function useFormFieldHandlers(fields: OHRIFormField[]) {
const [formFieldHandlers, setFormFieldHandlers] = useState<Record<string, SubmissionHandler>>({});

useEffect(() => {
const supportedTypes = new Set<string>();
fields.forEach(field => {
fields.forEach((field) => {
supportedTypes.add(field.type);
});
const supportedTypesArray = Array.from(supportedTypes);
Promise.all(supportedTypesArray.map(type => getRegisteredFieldSubmissionHandler(type))).then(handlers => {
Promise.all(supportedTypesArray.map((type) => getRegisteredFieldSubmissionHandler(type))).then((handlers) => {
const typeToHandlersArray = supportedTypesArray.map((type, index) => ({
[type]: handlers[index],
}));
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/useFormFieldValidators.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { useEffect, useState } from 'react';
import { FieldValidator, OHRIFormField } from '../api/types';
import { FieldValidator } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { getRegisteredValidator } from '../registry/registry';

export function useFormFieldValidators(fields: OHRIFormField[]) {
const [validators, setValidators] = useState<Record<string, FieldValidator>>();

useEffect(() => {
const supportedTypes = new Set<string>(['default']);
fields.forEach(field => {
field.validators?.forEach(validator => supportedTypes.add(validator.type));
fields.forEach((field) => {
field.validators?.forEach((validator) => supportedTypes.add(validator.type));
});
const supportedTypesArray = Array.from(supportedTypes);
Promise.all(supportedTypesArray.map(type => getRegisteredValidator(type))).then(validators => {
Promise.all(supportedTypesArray.map((type) => getRegisteredValidator(type))).then((validators) => {
setValidators(
Object.assign({}, ...validators.map((validator, index) => ({ [supportedTypesArray[index]]: validator }))),
);
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useInitialValues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { act, renderHook } from '@testing-library/react';
import { useInitialValues } from './useInitialValues';
import testEncounter from '../../__mocks__/use-initial-values/encounter.mock.json';
import testPatient from '../../__mocks__/use-initial-values/patient.mock.json';
import { OHRIFormField, OpenmrsEncounter } from '../api/types';
import { OpenmrsEncounter } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { ObsSubmissionHandler } from '../submission-handlers/base-handlers';

const obsGroupMembers: Array<OHRIFormField> = [
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useInitialValues.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';
import { EncounterContext, inferInitialValueFromDefaultFieldValue, isEmpty } from '..';
import { OHRIFormField, OpenmrsEncounter, SubmissionHandler } from '../api/types';
import { OpenmrsEncounter, SubmissionHandler } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { evaluateAsyncExpression } from '../utils/expression-runner';
import { cloneObsGroup } from '../components/repeat/helpers';
import { assignedObsIds } from '../submission-handlers/base-handlers';
Expand Down
3 changes: 2 additions & 1 deletion src/ohri-form-context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LayoutType } from '@openmrs/esm-framework';
import React from 'react';
import { RepeatObsGroupCounter, OHRIFormField, OpenmrsEncounter, SessionMode, SubmissionHandler } from './api/types';
import { RepeatObsGroupCounter, OpenmrsEncounter, SessionMode, SubmissionHandler } from './api/types';
import { OHRIFormField } from './api/OHRIFormField';

type OHRIFormContextProps = {
values: Record<string, any>;
Expand Down
2 changes: 1 addition & 1 deletion src/submission-handlers/base-handlers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable } from 'rxjs';
import { EncounterContext } from '../ohri-form-context';
import { OHRIFormField } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { findObsByFormField, ObsSubmissionHandler } from './base-handlers';

const encounterContext: EncounterContext = {
Expand Down
3 changes: 2 additions & 1 deletion src/submission-handlers/base-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import dayjs from 'dayjs';
import { getConcept, getAttachmentByUuid } from '../api/api';
import { ConceptTrue } from '../constants';
import { EncounterContext } from '../ohri-form-context';
import { OHRIFormField, OpenmrsEncounter, OpenmrsObs, SubmissionHandler } from '../api/types';
import { OpenmrsEncounter, OpenmrsObs, SubmissionHandler } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { parseToLocalDateTime } from '../utils/ohri-form-helper';
import { flattenObsList, hasRendering } from '../utils/common-utils';

Expand Down
3 changes: 2 additions & 1 deletion src/submission-handlers/encounterDatetimeHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SubmissionHandler } from '..';
import { OpenmrsEncounter, OHRIFormField } from '../api/types';
import { OpenmrsEncounter } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { EncounterContext } from '../ohri-form-context';

export const EncounterDatetimeHandler: SubmissionHandler = {
Expand Down
3 changes: 2 additions & 1 deletion src/submission-handlers/encounterLocationHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { map } from 'rxjs/operators';
import { SubmissionHandler } from '..';
import { getAllLocations } from '../api/api';
import { OpenmrsEncounter, OHRIFormField } from '../api/types';
import { OpenmrsEncounter } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { EncounterContext } from '../ohri-form-context';

export const EncounterLocationSubmissionHandler: SubmissionHandler = {
Expand Down
3 changes: 2 additions & 1 deletion src/submission-handlers/encounterProviderHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SubmissionHandler } from '..';
import { OpenmrsEncounter, OHRIFormField } from '../api/types';
import { OpenmrsEncounter } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { EncounterContext } from '../ohri-form-context';

export const EncounterProviderHandler: SubmissionHandler = {
Expand Down
3 changes: 2 additions & 1 deletion src/transformers/angular-fe-schema-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FormSchemaTransformer, OHRIFormField, OHRIFormSchema } from '../api/types';
import { FormSchemaTransformer, OHRIFormSchema } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';

export const AngularFormEngineSchemaTransformer: FormSchemaTransformer = {
transform: (form: OHRIFormSchema) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/common-expression-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import filter from 'lodash/filter';
import first from 'lodash/first';
import forEach from 'lodash/forEach';
import last from 'lodash/last';
import { OHRIFormField } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { FormNode } from './expression-runner';
import { isEmpty as isValueEmpty } from '../validators/ohri-form-validator';
import * as apiFunctions from '../api/api';
Expand Down
3 changes: 2 additions & 1 deletion src/utils/common-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OHRIFormField, OpenmrsObs, RenderType, AttachmentResponse, Attachment } from '../api/types';
import { OpenmrsObs, RenderType, AttachmentResponse, Attachment } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { formatDate } from '@openmrs/esm-framework';

export function flattenObsList(obsList: OpenmrsObs[]): OpenmrsObs[] {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/expression-parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OHRIFormField } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { ConceptFalse } from '../constants';
import {
findAndRegisterReferencedFields,
Expand Down Expand Up @@ -201,7 +201,7 @@ describe('findAndRegisterReferencedFields', () => {
it('should register field dependants', () => {
// setup
const expression = "linkedToCare == 'cf82933b-3f3f-45e7-a5ab-5d31aaee3da3' && !isEmpty(htsProviderRemarks)";
const patientIdentificationNumberField = testFields.find(f => f.id === 'patientIdentificationNumber');
const patientIdentificationNumberField = testFields.find((f) => f.id === 'patientIdentificationNumber');

// replay
findAndRegisterReferencedFields(
Expand All @@ -211,8 +211,8 @@ describe('findAndRegisterReferencedFields', () => {
);

// verify
const linkedToCare = testFields.find(f => f.id === 'linkedToCare');
const htsProviderRemarks = testFields.find(f => f.id === 'htsProviderRemarks');
const linkedToCare = testFields.find((f) => f.id === 'linkedToCare');
const htsProviderRemarks = testFields.find((f) => f.id === 'htsProviderRemarks');
expect(linkedToCare.fieldDependants).toStrictEqual(new Set(['patientIdentificationNumber']));
expect(htsProviderRemarks.fieldDependants).toStrictEqual(new Set(['patientIdentificationNumber']));
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils/expression-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OHRIFormField } from '../api/types';
import { OHRIFormField } from '../api/OHRIFormField';
import { ConceptFalse, ConceptTrue } from '../constants';
import { registerDependency } from './common-expression-helpers';
import { FormNode } from './expression-runner';
Expand Down
Loading

0 comments on commit aed3e64

Please sign in to comment.