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 Oct 31, 2024
1 parent 5c69ea9 commit 78087a7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 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;

0 comments on commit 78087a7

Please sign in to comment.