-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) (feat) O3-3367 Add support for person attributes
- Loading branch information
1 parent
a26a78c
commit f95290b
Showing
18 changed files
with
238 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) { | ||
return null; | ||
} | ||
field.meta.submission.newValue = { | ||
value: value, | ||
attributeType: field.questionOptions?.attribute?.type, | ||
}; | ||
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.attribute?.type, | ||
)?.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; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { openmrsFetch, type PersonAttribute, restBaseUrl } from '@openmrs/esm-framework'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export const usePersonAttributes = (patientUuid: string) => { | ||
const [personAttributes, setPersonAttributes] = useState<Array<PersonAttribute>>([]); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
if (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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.