Skip to content

Commit

Permalink
(refactor) Clean up hooks
Browse files Browse the repository at this point in the history
This PR cleans up the hooks directory, removing unused hooks and renaming others to match the `.ts` extension for consistency. `.tsx` should be reserved for components that contain JSX.
  • Loading branch information
denniskigen committed Dec 11, 2024
1 parent 02ce745 commit 4a297c5
Show file tree
Hide file tree
Showing 14 changed files with 7 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const mockUsePatient = jest.mocked(usePatient);
const mockUseSession = jest.mocked(useSession);
global.ResizeObserver = require('resize-observer-polyfill');

jest.mock('../../../hooks/useRestMaxResultsCount', () => jest.fn().mockReturnValue({ systemSetting: { value: '50' } }));
jest.mock('lodash-es/debounce', () => jest.fn((fn) => fn));

jest.mock('lodash-es', () => ({
Expand Down
1 change: 0 additions & 1 deletion src/form-engine.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ jest.mock('../src/api', () => {
};
});

jest.mock('./hooks/useRestMaxResultsCount', () => jest.fn().mockReturnValue({ systemSetting: { value: '50' } }));
jest.mock('./hooks/useEncounterRole', () => ({
useEncounterRole: jest.fn().mockReturnValue({
isLoading: false,
Expand Down
21 changes: 0 additions & 21 deletions src/hooks/useClobData.tsx

This file was deleted.

3 changes: 1 addition & 2 deletions src/hooks/useConcepts.tsx → src/hooks/useConcepts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react';
import { type FetchResponse, type OpenmrsResource, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import useSWRInfinite from 'swr/infinite';
import { type FetchResponse, type OpenmrsResource, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';

type ConceptFetchResponse = FetchResponse<{ results: Array<OpenmrsResource> }>;

Expand All @@ -12,7 +12,6 @@ export function useConcepts(references: Set<string>): {
isLoading: boolean;
error: Error | undefined;
} {

const chunkSize = 100;
const totalCount = references.size;
const totalPages = Math.ceil(totalCount / chunkSize);
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useDataSourceDependentValue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useWatch } from 'react-hook-form';
import { type FormField } from '../types';
import { useFormProviderContext } from '../provider/form-provider';
import { useWatch } from 'react-hook-form';

const useDataSourceDependentValue = (field: FormField) => {
const dependentField = field.questionOptions['config']?.referencedField;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type OpenmrsResource, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import useSWRImmutable from 'swr/immutable';
import { type OpenmrsResource, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';

export function useEncounterRole() {
const { data, error, isLoading } = useSWRImmutable<{ data: { results: Array<OpenmrsResource> } }, Error>(
Expand Down
18 changes: 0 additions & 18 deletions src/hooks/useFieldValidationResults.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FormExpanded, SessionMode } from '../types';
import { useCallback, useEffect, useState } from 'react';
import type { FormExpanded, SessionMode } from '../types';

export function useFormCollapse(sessionMode: SessionMode) {
const [isFormExpanded, setIsFormExpanded] = useState<FormExpanded>(undefined);
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useFormJson.tsx → src/hooks/useFormJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { moduleName } from '../globals';
export function useFormJson(formUuid: string, rawFormJson: any, encounterUuid: string, formSessionIntent: string) {
const [formJson, setFormJson] = useState<FormSchema>(null);
const [error, setError] = useState(validateFormsArgs(formUuid, rawFormJson));

useEffect(() => {
const setFormJsonWithTranslations = (formJson: FormSchema) => {
if (formJson?.translations) {
Expand All @@ -17,6 +18,7 @@ export function useFormJson(formUuid: string, rawFormJson: any, encounterUuid: s
}
setFormJson(formJson);
};

loadFormJson(formUuid, rawFormJson, formSessionIntent)
.then((formJson) => {
setFormJsonWithTranslations({ ...formJson, encounter: encounterUuid });
Expand Down
27 changes: 0 additions & 27 deletions src/hooks/useFormsConfig.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const patientGenderMap = {
export const usePatientData = (patientUuid) => {
const { patient, isLoading: isLoadingPatient, error: patientError } = usePatient(patientUuid);
if (patient && !isLoadingPatient) {
// This is to support backward compatibility with the AMPATH JSON format
// This is for backward compatibility with the Angular form engine
patient['age'] = calculateAge(new Date(patient?.birthDate));
patient['sex'] = patientGenderMap[patient.gender] ?? 'U';
}
Expand Down
5 changes: 0 additions & 5 deletions src/hooks/useRestMaxResultsCount.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/hooks/useSystemSetting.ts

This file was deleted.

0 comments on commit 4a297c5

Please sign in to comment.