Skip to content

Commit

Permalink
Stricter types
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher committed Jul 26, 2023
1 parent 516594b commit c7ea88a
Show file tree
Hide file tree
Showing 28 changed files with 73 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from 'react';
import find from 'lodash-es/find';
import camelCase from 'lodash-es/camelCase';
import escapeRegExp from 'lodash-es/escapeRegExp';
import { FetchResponse, messageOmrsServiceWorker, openmrsFetch, Session } from '@openmrs/esm-framework';
import {
import { messageOmrsServiceWorker, openmrsFetch, Session } from '@openmrs/esm-framework';
import type {
PatientIdentifierType,
FetchedPatientIdentifierType,
AddressTemplate,
} from './patient-registration/patient-registration-types';
} from './patient-registration/patient-registration.types';
import { cacheForOfflineHeaders } from './constants';

export interface Resources {
Expand All @@ -25,7 +25,7 @@ export async function fetchCurrentSession(): Promise<Session> {
}

export async function fetchAddressTemplate() {
const { data } = await cacheAndFetch('/ws/rest/v1/addresstemplate');
const { data } = await cacheAndFetch<AddressTemplate>('/ws/rest/v1/addresstemplate');
return data;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/esm-patient-registration-app/src/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
fetchPatientIdentifierTypesWithSources,
} from './offline.resources';
import { FormManager } from './patient-registration/form-manager';
import { PatientRegistration } from './patient-registration/patient-registration-types';
import { PatientRegistration } from './patient-registration/patient-registration.types';

export function setupOffline() {
setupOfflineSync(patientRegistration, [], syncPatientRegistration, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConceptResponse } from '../../patient-registration-types';
import { ConceptResponse } from '../../patient-registration.types';

export const useConcept = jest.fn(function mockUseConceptImplementation(uuid: string): {
data: ConceptResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FetchResponse, openmrsFetch } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch } from '@openmrs/esm-framework';
import { useField } from 'formik';
import { useCallback, useContext, useEffect, useMemo } from 'react';
import useSWRImmutable from 'swr/immutable';
Expand All @@ -10,7 +10,7 @@ interface AddressFields {

export function useOrderedAddressHierarchyLevels() {
const url = '/module/addresshierarchy/ajax/getOrderedAddressHierarchyLevels.form';
const { data, isLoading, error } = useSWRImmutable<FetchResponse<Array<AddressFields>>>(url, openmrsFetch);
const { data, isLoading, error } = useSWRImmutable<FetchResponse<Array<AddressFields>>, Error>(url, openmrsFetch);

const results = useMemo(
() => ({
Expand Down Expand Up @@ -56,7 +56,7 @@ export function useAddressEntries(fetchResults, searchString) {
* This also returns the function to reset the lower ordered fields if the value of a field is changed.
*/
export function useAddressEntryFetchConfig(addressField: string) {
const { orderedFields, isLoadingFieldOrder, errorFetchingFieldOrder } = useOrderedAddressHierarchyLevels();
const { orderedFields, isLoadingFieldOrder } = useOrderedAddressHierarchyLevels();
const { setFieldValue } = useContext(PatientRegistrationContext);
const [, { value: addressValues }] = useField('address');

Expand Down Expand Up @@ -102,14 +102,7 @@ export function useAddressEntryFetchConfig(addressField: string) {
return results;
}

export function useAddressHierarchy(
searchString,
separator,
): {
addresses: Array<string>;
isLoading: boolean;
error: Error;
} {
export function useAddressHierarchy(searchString: string, separator: string) {
const { data, error, isLoading } = useSWRImmutable<
FetchResponse<
Array<{
Expand All @@ -135,25 +128,15 @@ export function useAddressHierarchy(
return results;
}

export function useAdressHierarchyWithParentSearch(
addressField,
parentid,
query,
): {
error: Error;
isLoading: boolean;
addresses: Array<{
uuid: string;
name: string;
}>;
} {
export function useAddressHierarchyWithParentSearch(addressField: string, parentid: string, query: string) {
const { data, error, isLoading } = useSWRImmutable<
FetchResponse<
Array<{
uuid: string;
name: string;
}>
>
>,
Error
>(
query
? `/module/addresshierarchy/ajax/getPossibleAddressHierarchyEntriesWithParents.form?addressField=${addressField}&limit=20&searchString=${query}&parentUuid=${parentid}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressTemplate } from '../../../patient-registration-types';
import { AddressTemplate } from '../../../patient-registration.types';

export const mockedAddressTemplate: AddressTemplate = {
displayName: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FetchResponse, openmrsFetch, showToast } from '@openmrs/esm-framework';
import useSWRImmutable from 'swr/immutable';
import { ConceptAnswers, ConceptResponse } from '../patient-registration-types';
import { ConceptAnswers, ConceptResponse } from '../patient-registration.types';

export function useConcept(conceptUuid: string): { data: ConceptResponse; isLoading: boolean } {
const shouldFetch = typeof conceptUuid === 'string' && conceptUuid !== '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
IdentifierSource,
PatientIdentifierType,
PatientIdentifierValue,
} from '../../patient-registration-types';
} from '../../patient-registration.types';
import { ResourcesContext } from '../../../offline.resources';
import styles from '../field.scss';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useMemo, useCallback, useEffect, useState, useContext } from 're
import { useTranslation } from 'react-i18next';
import { Button, ButtonSet, Checkbox, Search, RadioButtonGroup, RadioButton } from '@carbon/react';
import { isDesktop, useConfig, useLayoutType } from '@openmrs/esm-framework';
import { FormValues, PatientIdentifierType, PatientIdentifierValue } from '../../patient-registration-types';
import { FormValues, PatientIdentifierType, PatientIdentifierValue } from '../../patient-registration.types';
import Overlay from '../../ui-components/overlay';
import { ResourcesContext } from '../../../offline.resources';
import { PatientRegistrationContext } from '../../patient-registration-context';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Field } from 'formik';
import { useTranslation } from 'react-i18next';
import { InlineNotification, Layer, Select, SelectItem } from '@carbon/react';
import { useConfig } from '@openmrs/esm-framework';
import { ConceptResponse } from '../../patient-registration-types';
import { ConceptResponse } from '../../patient-registration.types';
import { FieldDefinition, RegistrationConfig } from '../../../config-schema';
import { Input } from '../../input/basic-input/input/input.component';
import { useConcept, useConceptAnswers } from '../field.resource';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Layer, Select, SelectItem } from '@carbon/react';
import { useConfig } from '@openmrs/esm-framework';
import { Input } from '../../input/basic-input/input/input.component';
import { CodedPersonAttributeConfig } from '../../patient-registration-types';
import { CodedPersonAttributeConfig } from '../../patient-registration.types';
import { useConceptAnswers } from '../field.resource';
import { usePersonAttributeType } from './person-attributes.resource';
import styles from './../field.scss';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Layer, Select, SelectItem } from '@carbon/react';
import { Input } from '../../input/basic-input/input/input.component';
import { PersonAttributeTypeResponse } from '../../patient-registration-types';
import { PersonAttributeTypeResponse } from '../../patient-registration.types';
import { useConceptAnswers } from '../field.resource';
import styles from './../field.scss';
import { useTranslation } from 'react-i18next';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FetchResponse, openmrsFetch, showToast } from '@openmrs/esm-framework';
import useSWRImmutable from 'swr/immutable';
import { PersonAttributeTypeResponse } from '../../patient-registration-types';
import { PersonAttributeTypeResponse } from '../../patient-registration.types';

export function usePersonAttributeType(personAttributeTypeUuid: string): {
data: PersonAttributeTypeResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styles from './../field.scss';
import { Input } from '../../input/basic-input/input/input.component';
import { Field } from 'formik';
import { useTranslation } from 'react-i18next';
import { PersonAttributeTypeResponse } from '../../patient-registration-types';
import { PersonAttributeTypeResponse } from '../../patient-registration.types';

export interface TextPersonAttributeFieldProps {
id: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormManager } from './form-manager';
import { FormValues } from './patient-registration-types';
import { FormValues } from './patient-registration.types';

jest.mock('./patient-registration.resource');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
PatientRegistration,
RelationshipValue,
Encounter,
} from './patient-registration-types';
} from './patient-registration.types';
import {
addPatientIdentifier,
deletePatientIdentifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ResourcesContext } from '../../../../offline.resources';
import { showModal, useConfig, UserHasAccess } from '@openmrs/esm-framework';
import { shouldBlockPatientIdentifierInOfflineMode } from './utils';
import { deleteIdentifierType, setIdentifierSource } from '../../../field/id/id-field.component';
import { PatientIdentifierValue } from '../../../patient-registration-types';
import { PatientIdentifierValue } from '../../../patient-registration.types';
import { PatientRegistrationContext } from '../../../patient-registration-context';
import { Input } from '../../basic-input/input/input.component';
import styles from '../../input.scss';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FetchedPatientIdentifierType, PatientIdentifierType } from '../../../patient-registration-types';
import { FetchedPatientIdentifierType, PatientIdentifierType } from '../../../patient-registration.types';

export function shouldBlockPatientIdentifierInOfflineMode(identifierType: PatientIdentifierType) {
// Patient Identifiers which are unique and can be manually entered are prohibited while offline because
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { v4 } from 'uuid';
import { FormValues } from '../../patient-registration-types';
import { FormValues } from '../../patient-registration.types';
import styles from './../input.scss';

interface DummyDataInputProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useConfig } from '@openmrs/esm-framework';
import { createContext, SetStateAction } from 'react';
import { RegistrationConfig } from '../config-schema';
import { FormValues, CapturePhotoProps } from './patient-registration-types';
import { FormValues, CapturePhotoProps } from './patient-registration.types';

export interface PatientRegistrationContextProps {
identifierTypes: Array<any>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PersonAttributeResponse,
PatientIdentifierResponse,
Encounter,
} from './patient-registration-types';
} from './patient-registration.types';
import {
getAddressFieldValuesFromFhirPatient,
getFormValuesFromFhirPatient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
PatientUuidMapType,
PatientIdentifierValue,
Encounter,
} from './patient-registration-types';
} from './patient-registration.types';
import { parseDate } from '@openmrs/esm-framework';
import camelCase from 'lodash-es/camelCase';
import capitalize from 'lodash-es/capitalize';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useTranslation } from 'react-i18next';
import { Formik, Form, FormikHelpers } from 'formik';
import { createErrorHandler, showToast, useConfig, interpolateUrl, usePatient } from '@openmrs/esm-framework';
import { validationSchema as initialSchema } from './validation/patient-registration-validation';
import { FormValues, CapturePhotoProps, PatientIdentifierValue } from './patient-registration-types';
import { FormValues, CapturePhotoProps, PatientIdentifierValue } from './patient-registration.types';
import { PatientRegistrationContext } from './patient-registration-context';
import { SavePatientForm, SavePatientTransactionManager } from './form-manager';
import { usePatientPhoto } from './patient-registration.resource';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo } from 'react';
import useSWR from 'swr';
import useSWRImmutable from 'swr/immutable';
import { FetchResponse, openmrsFetch, useConfig } from '@openmrs/esm-framework';
import { Patient, Relationship, PatientIdentifier, Encounter } from './patient-registration-types';
import { Patient, Relationship, PatientIdentifier, Encounter } from './patient-registration.types';

export const uuidIdentifier = '05a29f94-c0ed-11e2-94be-8c13b969e334';
export const uuidTelephoneNumber = '14d4f066-15f5-102d-96e4-000c29c2a5d7';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,35 +263,47 @@ export interface ConceptAnswers {
uuid: string;
}

export type AddressProperties =
| 'cityVillage'
| 'stateProvince'
| 'countyDistrict'
| 'postalCode'
| 'country'
| 'address1'
| 'address2'
| 'address3'
| 'address4'
| 'address5'
| 'address6'
| 'address7'
| 'address8'
| 'address9'
| 'address10'
| 'address11'
| 'address12'
| 'address13'
| 'address14'
| 'address15';

export type ExtensibleAddressProperties = { [p in AddressProperties]?: string } | null;

export interface AddressTemplate {
displayName: null;
codeName: string;
country: string;
displayName: string | null;
codeName: string | null;
country: string | null;
lines: Array<
Array<{
isToken: 'IS_NOT_ADDR_TOKEN' | 'IS_ADDR_TOKEN';
displayText: string;
codeName?: string;
codeName?: AddressProperties;
displaySize?: string;
}>
>;
lineByLineFormat: Array<string>;
nameMappings: {
[x: string]: string;
};
sizeMappings: {
[x: string]: string;
};
elementDefaults: {
[x: string]: string;
};
elementRegex: {
[x: string]: string;
};
elementRegexFormats: {
[x: string]: string;
};
requiredElements: {
[x: string]: string;
};
> | null;
lineByLineFormat: Array<string> | null;
nameMappings: ExtensibleAddressProperties;
sizeMappings: ExtensibleAddressProperties;
elementDefaults: ExtensibleAddressProperties;
elementRegex: ExtensibleAddressProperties;
elementRegexFormats: ExtensibleAddressProperties;
requiredElements: Array<AddressProperties> | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Autosuggest } from '../../input/custom-input/autosuggest/autosuggest.co
import { PatientRegistrationContext } from '../../patient-registration-context';
import { ResourcesContext } from '../../../offline.resources';
import { fetchPerson } from '../../patient-registration.resource';
import { RelationshipValue } from '../../patient-registration-types';
import { RelationshipValue } from '../../patient-registration.types';
import sectionStyles from '../section.scss';
import styles from './relationships.scss';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FetchResponse, openmrsFetch, showToast } from '@openmrs/esm-framework';
import { RelationshipValue } from '../../patient-registration-types';
import { RelationshipValue } from '../../patient-registration.types';
import useSWR from 'swr';
import { useMemo } from 'react';
import { personRelationshipRepresentation } from '../../../constants';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Yup from 'yup';
import mapValues from 'lodash/mapValues';
import { FormValues } from '../patient-registration-types';
import { FormValues } from '../patient-registration.types';

export const validationSchema = Yup.object({
givenName: Yup.string().required('givenNameRequired'),
Expand Down
8 changes: 2 additions & 6 deletions packages/esm-patient-registration-app/src/root.component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo } from 'react';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import { Grid, Row } from '@carbon/react';
import { ExtensionSlot, FetchResponse, useConnectivity, useSession } from '@openmrs/esm-framework';
import { ExtensionSlot, useConnectivity, useSession } from '@openmrs/esm-framework';
import {
ResourcesContext,
fetchAddressTemplate,
Expand All @@ -12,15 +12,11 @@ import { FormManager } from './patient-registration/form-manager';
import { PatientRegistration } from './patient-registration/patient-registration.component';
import useSWRImmutable from 'swr/immutable';
import styles from './root.scss';
import { AddressTemplate } from './patient-registration/patient-registration-types';

export default function Root() {
const isOnline = useConnectivity();
const currentSession = useSession();
const { data: addressTemplate } = useSWRImmutable<AddressTemplate>(
'patientRegistrationAddressTemplate',
fetchAddressTemplate,
);
const { data: addressTemplate } = useSWRImmutable('patientRegistrationAddressTemplate', fetchAddressTemplate);
const { data: relationshipTypes } = useSWRImmutable(
'patientRegistrationRelationshipTypes',
fetchAllRelationshipTypes,
Expand Down

0 comments on commit c7ea88a

Please sign in to comment.