Skip to content
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

(fix) Fix more react hooks exhaustive deps warnings #1387

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function DeathDateField() {
selectedDate ? dayjs(selectedDate).hour(0).minute(0).second(0).millisecond(0).toDate() : undefined,
);
},
[deathDate],
[setFieldValue],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const DobField: React.FC = () => {
setFieldValue('monthsEstimated', '');
setFieldTouched('birthdateEstimated', true, false);
},
[setFieldValue],
[setFieldTouched, setFieldValue],
);

const onDateChange = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { useTranslation } from 'react-i18next';
import { ContentSwitcher, Switch } from '@carbon/react';
import { useField } from 'formik';
import { ExtensionSlot, useConfig } from '@openmrs/esm-framework';
import { type RegistrationConfig } from '../../../config-schema';
import { Input } from '../../input/basic-input/input/input.component';
import { PatientRegistrationContext } from '../../patient-registration-context';
import { type RegistrationConfig } from '../../../config-schema';
import styles from '../field.scss';

export const unidentifiedPatientAttributeTypeUuid = '8b56eac7-5c76-4b9c-8c6f-1deab8d3fc47';
Expand Down Expand Up @@ -51,7 +51,7 @@ export const NameField = () => {
setFieldTouched('photo', true, false);
}
},
[setCapturePhotoProps],
[setCapturePhotoProps, setFieldTouched],
);

const toggleNameKnown = (e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import { Field } from 'formik';
import { Layer, Select, SelectItem } from '@carbon/react';
import { reportError } from '@openmrs/esm-framework';
import { type PersonAttributeTypeResponse } from '../../patient-registration.types';
import { useConceptAnswers } from '../field.resource';
import styles from './../field.scss';
import { reportError } from '@openmrs/esm-framework';

export interface CodedPersonAttributeFieldProps {
id: string;
Expand Down Expand Up @@ -44,7 +44,7 @@ export function CodedPersonAttributeField({
);
setError(true);
}
}, [answerConceptSetUuid, customConceptAnswers]);
}, [answerConceptSetUuid, customConceptAnswers, id, t]);

useEffect(() => {
if (!isLoadingConceptAnswers && !customConceptAnswers.length) {
Expand Down Expand Up @@ -72,7 +72,7 @@ export function CodedPersonAttributeField({
setError(true);
}
}
}, [isLoadingConceptAnswers, conceptAnswers, customConceptAnswers]);
}, [isLoadingConceptAnswers, conceptAnswers, customConceptAnswers, t, id, answerConceptSetUuid]);

const answers = useMemo(() => {
if (customConceptAnswers.length) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { type Dispatch, useEffect, useMemo, useState } from 'react';
import {
type FetchResponse,
type OpenmrsResource,
getSynchronizationItems,
openmrsFetch,
type OpenmrsResource,
restBaseUrl,
useConfig,
usePatient,
} from '@openmrs/esm-framework';
import camelCase from 'lodash-es/camelCase';
import { type Dispatch, useEffect, useMemo, useState } from 'react';
import dayjs from 'dayjs';
import useSWR from 'swr';
import { v4 } from 'uuid';
import { type RegistrationConfig } from '../config-schema';
Expand All @@ -29,7 +30,15 @@ import {
latestFirstEncounter,
} from './patient-registration-utils';
import { useInitialPatientRelationships } from './section/patient-relationships/relationships.resource';
import dayjs from 'dayjs';

interface DeathInfoResults {
uuid: string;
display: string;
causeOfDeath: OpenmrsResource | null;
dead: boolean;
deathDate: string;
causeOfDeathNonCoded: string | null;
}

export function useInitialFormValues(patientUuid: string): [FormValues, Dispatch<FormValues>] {
const { freeTextFieldConceptUuid } = useConfig<RegistrationConfig>();
Expand Down Expand Up @@ -99,7 +108,7 @@ export function useInitialFormValues(patientUuid: string): [FormValues, Dispatch
setInitialFormValues(registration._patientRegistrationData.formValues);
}
})();
}, [isLoadingPatientToEdit, patientToEdit, patientUuid]);
}, [initialFormValues, isLoadingPatientToEdit, patientToEdit, patientUuid]);

// Set initial patient death info
useEffect(() => {
Expand All @@ -118,7 +127,7 @@ export function useInitialFormValues(patientUuid: string): [FormValues, Dispatch
nonCodedCauseOfDeath: deathInfo.causeOfDeathNonCoded,
}));
}
}, [isLoadingDeathInfo, deathInfo, setInitialFormValues]);
}, [isLoadingDeathInfo, deathInfo, setInitialFormValues, freeTextFieldConceptUuid]);

// Set initial patient relationships
useEffect(() => {
Expand Down Expand Up @@ -187,7 +196,7 @@ export function useInitialAddressFieldValues(patientUuid: string, fallback = {})
setInitialAddressFieldValues(registration?._patientRegistrationData.initialAddressFieldValues ?? fallback);
}
})();
}, [isLoading, patient, patientUuid]);
}, [fallback, initialAddressFieldValues, isLoading, patient, patientUuid]);

return [initialAddressFieldValues, setInitialAddressFieldValues];
}
Expand All @@ -208,7 +217,7 @@ export function usePatientUuidMap(
setPatientUuidMap(registration?._patientRegistrationData.initialAddressFieldValues ?? fallback),
);
}
}, [isLoadingPatientToEdit, patientToEdit, patientUuid]);
}, [fallback, isLoadingPatientToEdit, patientToEdit, patientUuid, patientUuidMap]);

useEffect(() => {
if (attributes) {
Expand Down Expand Up @@ -263,7 +272,7 @@ export function useInitialPatientIdentifiers(patientUuid: string): {
data: identifiers,
isLoading,
};
}, [data, error]);
}, [data?.data?.results, isLoading]);

return result;
}
Expand Down Expand Up @@ -299,19 +308,10 @@ function useInitialPersonAttributes(personUuid: string) {
data: data?.data?.results,
isLoading,
};
}, [data, error]);
}, [data?.data?.results, isLoading]);
return result;
}

interface DeathInfoResults {
uuid: string;
display: string;
causeOfDeath: OpenmrsResource | null;
dead: boolean;
deathDate: string;
causeOfDeathNonCoded: string | null;
}

function useInitialPersonDeathInfo(personUuid: string) {
const { data, error, isLoading } = useSWR<FetchResponse<DeathInfoResults>, Error>(
!!personUuid
Expand All @@ -325,7 +325,7 @@ function useInitialPersonDeathInfo(personUuid: string) {
data: data?.data,
isLoading,
};
}, [data, error]);
}, [data?.data, isLoading]);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useContext, useEffect } from 'react';
import styles from './../section.scss';
import { useField } from 'formik';
import { PatientRegistrationContext } from '../../patient-registration-context';
import { Field } from '../../field/field.component';
import { PatientRegistrationContext } from '../../patient-registration-context';
import styles from './../section.scss';

export interface DemographicsSectionProps {
fields: Array<string>;
Expand All @@ -18,7 +18,7 @@ export const DemographicsSection: React.FC<DemographicsSectionProps> = ({ fields
setFieldValue('additionalMiddleName', '');
setFieldValue('additionalFamilyName', '');
}
}, [field.value, meta.touched]);
}, [field.value, meta.touched, setFieldValue]);

return (
<section className={styles.formSection} aria-label="Demographics Section">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-fram
import { type RelationshipValue } from '../../patient-registration.types';
import { personRelationshipRepresentation } from '../../../constants';

export interface Relationship {
display: string;
uuid: string;
personA: {
age: number;
display: string;
birthdate: string;
uuid: string;
};
personB: {
age: number;
display: string;
birthdate: string;
uuid: string;
};
relationshipType: {
uuid: string;
display: string;
aIsToB: string;
bIsToA: string;
};
}

interface RelationshipsResponse {
results: Array<Relationship>;
}

export function useInitialPatientRelationships(patientUuid: string): {
data: Array<RelationshipValue>;
isLoading: boolean;
Expand Down Expand Up @@ -45,34 +72,7 @@ export function useInitialPatientRelationships(patientUuid: string): {
error,
isLoading,
};
}, [patientUuid, data, error]);
}, [data?.data?.results, error, isLoading, patientUuid]);

return result;
}

export interface Relationship {
display: string;
uuid: string;
personA: {
age: number;
display: string;
birthdate: string;
uuid: string;
};
personB: {
age: number;
display: string;
birthdate: string;
uuid: string;
};
relationshipType: {
uuid: string;
display: string;
aIsToB: string;
bIsToA: string;
};
}

interface RelationshipsResponse {
results: Array<Relationship>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const CompactPatientSearchComponent: React.FC<CompactPatientSearchProps> = ({
});
}
},
[config.search.patientChartUrl, user, currentLocation],
[addViewedPatientAndCloseSearchResults, config.search.patientChartUrl],
);
const focusedResult = useArrowNavigation(
!recentPatients ? searchedPatients?.length ?? 0 : recentPatients?.length ?? 0,
Expand Down Expand Up @@ -134,7 +134,7 @@ const CompactPatientSearchComponent: React.FC<CompactPatientSearchProps> = ({
subtitle: errorFetchingUserProperties?.message,
});
}
}, [fetchError, errorFetchingUserProperties]);
}, [fetchError, errorFetchingUserProperties, t]);

const handleSubmit = useCallback(
(debouncedSearchTerm) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useEffect } from 'react';
import { Button } from '@carbon/react';
import { Search } from '@carbon/react/icons';
import { launchWorkspace } from '@openmrs/esm-framework';
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { type PatientSearchWorkspaceProps } from '../patient-search-workspace/patient-search.workspace';

Expand Down Expand Up @@ -37,7 +37,7 @@ const PatientSearchButton: React.FC<PatientSearchButtonProps> = ({
}) => {
const { t } = useTranslation();

const launchPatientSearchWorkspace = () => {
const launchPatientSearchWorkspace = useCallback(() => {
const workspaceProps: PatientSearchWorkspaceProps = {
handleSearchTermUpdated: searchQueryUpdatedAction,
initialQuery: searchQuery,
Expand All @@ -47,13 +47,13 @@ const PatientSearchButton: React.FC<PatientSearchButtonProps> = ({
...workspaceProps,
workspaceTitle: overlayHeader,
});
};
}, [overlayHeader, searchQuery, searchQueryUpdatedAction, selectPatientAction]);

useEffect(() => {
if (isOpen) {
launchPatientSearchWorkspace();
}
}, [isOpen]);
}, [isOpen, launchPatientSearchWorkspace]);

return (
<Button
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useConfig, useDebounce } from '@openmrs/esm-framework';
import React, { useCallback, useState } from 'react';
import { useConfig, useDebounce } from '@openmrs/esm-framework';
import { type PatientSearchConfig } from '../config-schema';
import PatientSearchBar from '../patient-search-bar/patient-search-bar.component';
import { PatientSearchContext, type PatientSearchContextProps } from '../patient-search-context';
import PatientSearchBar from '../patient-search-bar/patient-search-bar.component';
import AdvancedPatientSearchComponent from '../patient-search-page/advanced-patient-search.component';

export interface PatientSearchWorkspaceProps extends PatientSearchContextProps {
Expand All @@ -28,10 +28,13 @@ const PatientSearchWorkspace: React.FC<PatientSearchWorkspaceProps> = ({

const handleClearSearchTerm = useCallback(() => setSearchTerm(''), [setSearchTerm]);

const onSearchTermChange = useCallback((value: string) => {
setSearchTerm(value);
handleSearchTermUpdated && handleSearchTermUpdated(value);
}, []);
const onSearchTermChange = useCallback(
(value: string) => {
setSearchTerm(value);
handleSearchTermUpdated && handleSearchTermUpdated(value);
},
[handleSearchTermUpdated],
);

return (
<PatientSearchContext.Provider value={{ nonNavigationSelectPatientAction, patientClickSideEffect }}>
Expand Down
13 changes: 8 additions & 5 deletions packages/esm-patient-search-app/src/patient-search.resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function useInfinitePatientSearch(
currentPage: size,
totalResults: data?.[0]?.data?.totalCount ?? 0,
}),
[data, isLoading, isValidating, error, setSize, size],
[mappedData, isLoading, error, data, isValidating, setSize, size],
);
}

Expand Down Expand Up @@ -120,7 +120,10 @@ export function useRecentlyViewedPatients(showRecentlySearchedPatients: boolean
);

const userProperties = data?.data?.userProperties;
const patientsVisited = userProperties?.patientsVisited?.split(',').filter(Boolean) ?? [];
const patientsVisited = useMemo(
() => userProperties?.patientsVisited?.split(',').filter(Boolean) ?? [],
[userProperties],
);

const updateRecentlyViewedPatients = useCallback(
(patientUuid: string) => {
Expand All @@ -138,7 +141,7 @@ export function useRecentlyViewedPatients(showRecentlySearchedPatients: boolean
},
});
},
[patientsVisited, userProperties],
[patientsVisited, url, userProperties],
);

return useMemo(
Expand All @@ -149,7 +152,7 @@ export function useRecentlyViewedPatients(showRecentlySearchedPatients: boolean
updateRecentlyViewedPatients,
mutateUserProperties: mutate,
}),
[data, error, mutate],
[error, isLoading, mutate, patientsVisited, updateRecentlyViewedPatients],
);
}

Expand Down Expand Up @@ -213,6 +216,6 @@ export function useRestPatients(
currentPage: size,
totalResults: patientUuids?.length ?? 0,
}),
[data, isLoading, isValidating, error, setSize, size, patientUuids],
[mappedData, isLoading, error, patientUuids, size, isValidating, setSize],
);
}
Loading