Skip to content

Commit

Permalink
(feat) add dynamic showing of publication number
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldkibet committed Nov 5, 2024
1 parent 5c69ea9 commit 526dd09
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { useTranslation } from 'react-i18next';
import classNames from 'classnames';
import { PatientRegistrationContext } from '../../patient-registration-context';
import styles from './../field.scss';
import { ResourcesContext } from '../../../offline.resources';
import useUpdateIdentifierRequirement from './useUpdateIdentifierRequirement';

interface PersonAttributeTypeResponse {
uuid: string;
Expand Down Expand Up @@ -46,9 +48,8 @@ const CustomPersonAttributeField: React.FC<CustomPersonAttributeFieldProps> = ({
}) => {
const { t } = useTranslation();
const fieldName = `attributes.${personAttributeType.uuid}`;
const context = useContext(PatientRegistrationContext) as PatientRegistrationContextType;
const { setFieldValue, values } = context;

const { setFieldValue, values } = useContext(PatientRegistrationContext);
useUpdateIdentifierRequirement(setFieldValue, values);
// TODO: Improve this logic
const filteredCustomConceptAnswers = customConceptAnswers.filter((answer) => {
const showExpression = answer.showServiceExpression;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { useEffect, useCallback, useRef, useMemo, useContext } from 'react';
import { deleteIdentifierType, initializeIdentifier } from '../id/id-field.component';
import { ResourcesContext } from '../../../offline.resources';

const useUpdateIdentifierRequirement = (setFieldValue, values) => {
const { identifierTypes = [] } = useContext(ResourcesContext);
const previousAttributes = useRef(values.attributes);
const previousIdentifiers = useRef(values.identifiers);

const publicationNumberIdentifier = useMemo(
() => identifierTypes?.find((identifier) => identifier.name === 'Publication Number'),
[identifierTypes],
);

// Memoize the civilian check
const isCivilian = useMemo(() => Object.values(values.attributes ?? {}).includes('Civilian'), [values.attributes]);

// Memoize the identifier initialization logic
const initializePublicationIdentifier = useCallback(
(currentIdentifiers) => {
if (!publicationNumberIdentifier) {
console.warn('Publication Number identifier type not found');
return null;
}

const initializedIdentifier = initializeIdentifier(
publicationNumberIdentifier,
currentIdentifiers[publicationNumberIdentifier.uuid],
);

return initializedIdentifier;
},
[publicationNumberIdentifier],
);
// Only run the effect if isCivilian is true
useEffect(() => {
// Skip if we don't have the required data
if (!values.attributes || !publicationNumberIdentifier) {
return;
}

// Check if relevant values have actually changed
const attributesChanged = previousAttributes.current !== values.attributes;
const identifiersChanged = previousIdentifiers.current !== values.identifiers;

if (!attributesChanged && !identifiersChanged) {
return;
}

// Update refs
previousAttributes.current = values.attributes;
previousIdentifiers.current = values.identifiers;
const isDependant = Object.values(values.attributes ?? {}).includes('Dependant');
// Only proceed if the user is a civilian
if (isCivilian && isDependant) {
const initializedIdentifier = initializePublicationIdentifier(values.identifiers);

// check if values.identifiers already has the publication number identifier
const hasPublicationNumberIdentifier = values.identifiers[publicationNumberIdentifier.fieldName];

if (initializedIdentifier && !hasPublicationNumberIdentifier) {
setFieldValue('identifiers', {
...values.identifiers,
[publicationNumberIdentifier.fieldName]: { ...initializedIdentifier, required: true },
});
}
} else {
// Before deleting the publication number identifier, check if it exists
if (values.identifiers[publicationNumberIdentifier.fieldName]) {
setFieldValue('identifiers', deleteIdentifierType(values.identifiers, publicationNumberIdentifier.fieldName));
}
}
}, [
values.attributes,
values.identifiers,
isCivilian,
publicationNumberIdentifier,
initializePublicationIdentifier,
setFieldValue,
]);
};

export default useUpdateIdentifierRequirement;
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3068,8 +3068,8 @@ __metadata:
linkType: hard

"@openmrs/esm-patient-common-lib@npm:next":
version: 8.2.1-pre.5751
resolution: "@openmrs/esm-patient-common-lib@npm:8.2.1-pre.5751"
version: 8.2.1-pre.5938
resolution: "@openmrs/esm-patient-common-lib@npm:8.2.1-pre.5938"
dependencies:
"@carbon/react": "npm:^1.12.0"
lodash-es: "npm:^4.17.21"
Expand All @@ -3078,7 +3078,7 @@ __metadata:
"@openmrs/esm-framework": 5.x
react: 18.x
single-spa: 6.x
checksum: 10/d23e6016e9c67fa12805fed897934a29b114bcbbf438b8938bea513fe55e9d93f8339351e38a0cc34cdd6adb97a956ffe2bca571853ac0f83cfaea72a2c43242
checksum: 10/e9eb4db1927d1f3c2e64cc93392ef68e3a3d54fdb741a801b2267ae3d8a938943296b599d73c5c82dfad70df1c797cbef6f2a6fba2e0d55a25d722d22a95ad8f
languageName: node
linkType: hard

Expand Down

0 comments on commit 526dd09

Please sign in to comment.