-
Notifications
You must be signed in to change notification settings - Fork 103
(feat) O3-3367 Add support for person attributes #423
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { type PersonAttribute, type OpenmrsResource } from '@openmrs/esm-framework'; | ||
| import { type FormContextProps } from '../provider/form-provider'; | ||
| import { type FormField, type FormFieldValueAdapter, type FormProcessorContextProps } from '../types'; | ||
| import { clearSubmission } from '../utils/common-utils'; | ||
| import { isEmpty } from '../validators/form-validator'; | ||
|
|
||
| export const PersonAttributesAdapter: FormFieldValueAdapter = { | ||
| transformFieldValue: function (field: FormField, value: any, context: FormContextProps) { | ||
| clearSubmission(field); | ||
| if (field.meta?.previousValue?.value === value || isEmpty(value)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the user tries to delete an attribute in edit mode? if (field.meta?.previousValue && isEmpty(value)) {
// should we void the attribute?
} |
||
| return null; | ||
| } | ||
| field.meta.submission.newValue = { | ||
| value: value, | ||
| attributeType: field.questionOptions?.attributeType, | ||
| }; | ||
| return field.meta.submission.newValue; | ||
| }, | ||
| getInitialValue: function (field: FormField, sourceObject: OpenmrsResource, context: FormProcessorContextProps) { | ||
| const rendering = field.questionOptions.rendering; | ||
|
|
||
| const personAttributeValue = context?.customDependencies.personAttributes.find( | ||
| (attribute: PersonAttribute) => attribute.attributeType.uuid === field.questionOptions.attributeType, | ||
| )?.value; | ||
| if (rendering === 'text') { | ||
| if (typeof personAttributeValue === 'string') { | ||
| return personAttributeValue; | ||
| } else if ( | ||
| personAttributeValue && | ||
| typeof personAttributeValue === 'object' && | ||
| 'display' in personAttributeValue | ||
| ) { | ||
| return personAttributeValue?.display; | ||
| } | ||
| } else if (rendering === 'ui-select-extended') { | ||
| if (personAttributeValue && typeof personAttributeValue === 'object' && 'uuid' in personAttributeValue) { | ||
| return personAttributeValue?.uuid; | ||
| } | ||
| } | ||
| return null; | ||
| }, | ||
| getPreviousValue: function (field: FormField, sourceObject: OpenmrsResource, context: FormProcessorContextProps) { | ||
| return null; | ||
| }, | ||
| getDisplayValue: function (field: FormField, value: any) { | ||
| if (value?.display) { | ||
| return value.display; | ||
| } | ||
| return value; | ||
| }, | ||
| tearDown: function (): void { | ||
| return; | ||
| }, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; | ||
| import { BaseOpenMRSDataSource } from './data-source'; | ||
|
|
||
| export class PersonAttributeLocationDataSource extends BaseOpenMRSDataSource { | ||
CynthiaKamau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| constructor() { | ||
| super(null); | ||
| } | ||
|
|
||
| async fetchData(searchTerm: string, config?: Record<string, any>, uuid?: string): Promise<any[]> { | ||
| const rep = 'v=custom:(uuid,display)'; | ||
| const url = `${restBaseUrl}/location?${rep}`; | ||
| const { data } = await openmrsFetch(searchTerm ? `${url}&q=${searchTerm}` : url); | ||
|
|
||
| return data?.results; | ||
| } | ||
|
Comment on lines
+9
to
+15
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This data source seems to just load locations (not tied down to the "attribute" model). Anything stopping you from reusing the existing location DS`?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^^^^ This
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ export class SelectConceptAnswersDatasource extends BaseOpenMRSDataSource { | |
| } | ||
|
|
||
| fetchData(searchTerm: string, config?: Record<string, any>): Promise<any[]> { | ||
| const apiUrl = this.url.replace('conceptUuid', config.referencedValue || config.concept); | ||
| const apiUrl = this.url.replace('conceptUuid', config.concept || config.referencedValue); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the inversion here? |
||
| return openmrsFetch(apiUrl).then(({ data }) => { | ||
| return data['setMembers'].length ? data['setMembers'] : data['answers']; | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { openmrsFetch, type PersonAttribute, restBaseUrl } from '@openmrs/esm-framework'; | ||
| import { useEffect, useState } from 'react'; | ||
| import { type FormSchema } from '../types'; | ||
|
|
||
| export const usePersonAttributes = (patientUuid: string, formJson: FormSchema) => { | ||
| const [personAttributes, setPersonAttributes] = useState<Array<PersonAttribute>>([]); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
| const [error, setError] = useState(null); | ||
|
|
||
| useEffect(() => { | ||
| if (formJson.meta?.personAttributes?.hasPersonAttributeFields && patientUuid) { | ||
| openmrsFetch(`${restBaseUrl}/patient/${patientUuid}?v=custom:(attributes)`) | ||
| .then((response) => { | ||
| setPersonAttributes(response.data?.attributes); | ||
| setIsLoading(false); | ||
| }) | ||
| .catch((error) => { | ||
| setError(error); | ||
| setIsLoading(false); | ||
| }); | ||
| } else { | ||
| setIsLoading(false); | ||
| } | ||
| }, [patientUuid]); | ||
|
|
||
| return { | ||
| personAttributes, | ||
| error, | ||
| isLoading: isLoading, | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,6 +94,6 @@ export const inbuiltControls: Array<RegistryItem<React.ComponentType<FormFieldIn | |
| }, | ||
| ...controlTemplates.map((template) => ({ | ||
| name: template.name, | ||
| component: templateToComponentMap.find((component) => component.name === template.name).baseControlComponent, | ||
| component: templateToComponentMap.find((component) => component.name === template.name)?.baseControlComponent, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't really understand why we need the |
||
| })), | ||
| ]; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you planning on adding some test coverage for this adapter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes i will