Skip to content

Commit

Permalink
feat: fetch the org unit only when the map is opened using useApiMeta…
Browse files Browse the repository at this point in the history
…dataQuery
  • Loading branch information
simonadomnisoru committed Nov 30, 2023
1 parent abb8d9d commit bb88f8d
Show file tree
Hide file tree
Showing 24 changed files with 148 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
getIncidentDateValidatorContainer,
} from './fieldValidators';
import { sectionKeysForEnrollmentDataEntry } from './constants/sectionKeys.const';
import { withCenterPoint } from '../../DataEntry/withCenterPoint';
import { type Enrollment, ProgramStage, RenderFoundation, getProgramThrowIfNotFound } from '../../../metaData';
import { EnrollmentWithFirstStageDataEntry } from './EnrollmentWithFirstStageDataEntry';
import {
Expand Down Expand Up @@ -240,7 +239,7 @@ const getGeometrySettings = () => ({
dialogLabel: i18n.t('Area'),
required: false,
orientation: getOrientation(props.formHorizontal),
center: props.center,
orgUnit: props.orgUnit,
});
}

Expand All @@ -251,7 +250,7 @@ const getGeometrySettings = () => ({
required: false,
orientation: getOrientation(props.formHorizontal),
shrinkDisabled: props.formHorizontal,
center: props.center,
orgUnit: props.orgUnit,
});
},
getPropName: () => 'geometry',
Expand Down Expand Up @@ -379,8 +378,7 @@ const AOCFieldBuilderHOC = withAOCFieldBuilder(getAOCSettingsFn())(
getCategoryOptionsSettingsFn(),
)(FinalEnrollmentDataEntry));
const LocationHOC = withDataEntryFieldIfApplicable(getGeometrySettings())(AOCFieldBuilderHOC);
const CenterPointHOC = withCenterPoint()(LocationHOC);
const IncidentDateFieldHOC = withDataEntryFieldIfApplicable(getIncidentDateSettings())(CenterPointHOC);
const IncidentDateFieldHOC = withDataEntryFieldIfApplicable(getIncidentDateSettings())(LocationHOC);
const EnrollmentDateFieldHOC = withDataEntryField(getEnrollmentDateSettings())(IncidentDateFieldHOC);
const BrowserBackWarningHOC = withBrowserBackWarning()(EnrollmentDateFieldHOC);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ const getStageGeometrySettings = () => ({
dialogLabel: i18n.t('Area'),
required: false,
orientation: getOrientation(props.formHorizontal),
orgUnit: props.orgUnit,
});
}

Expand All @@ -136,6 +137,7 @@ const getStageGeometrySettings = () => ({
required: false,
orientation: getOrientation(props.formHorizontal),
shrinkDisabled: props.formHorizontal,
orgUnit: props.orgUnit,
});
},
getPropName: () => stageMainDataIds.GEOMETRY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
withSaveHandler,
placements,
withCleanUp,
withCenterPoint,
} from '../../../../DataEntry';
import {
withInternalChangeHandler,
Expand Down Expand Up @@ -232,7 +231,7 @@ const buildGeometrySettingsFn = () => ({
dialogLabel: i18n.t('Area'),
required: false,
orientation: getOrientation(props.formHorizontal),
center: props.center,
orgUnit: props.orgUnit,
});
}

Expand All @@ -243,7 +242,7 @@ const buildGeometrySettingsFn = () => ({
required: false,
orientation: getOrientation(props.formHorizontal),
shrinkDisabled: props.formHorizontal,
center: props.center,
orgUnit: props.orgUnit,
});
},
getPropName: () => 'geometry',
Expand Down Expand Up @@ -413,7 +412,7 @@ const CleanUpHOC = withCleanUp()(withFilterProps(dataEntryFilterProps)(DataEntry
const AssigneeField = withDataEntryFieldIfApplicable(buildAssigneeSettingsFn())(CleanUpHOC);
const RelationshipField = withDataEntryFieldIfApplicable(buildRelationshipsSettingsFn())(AssigneeField);
const CommentField = withDataEntryField(buildNotesSettingsFn())(RelationshipField);
const GeometryField = withCenterPoint()(withDataEntryFieldIfApplicable(buildGeometrySettingsFn())(CommentField));
const GeometryField = withDataEntryFieldIfApplicable(buildGeometrySettingsFn())(CommentField);
const ReportDateField = withDataEntryField(buildReportDateSettingsFn())(GeometryField);
const FeedbackOutput = withFeedbackOutput()(ReportDateField);
const IndicatorOutput = withIndicatorOutput()(FeedbackOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export { withErrorOutput } from './dataEntryOutput/withErrorOutput';
export { withIndicatorOutput } from './dataEntryOutput/withIndicatorOutput';
export { withCleanUp } from './withCleanUp';
export { withAskToCreateNew } from './withAskToCreateNew';
export { withCenterPoint } from './withCenterPoint';

// misc
export { inMemoryFileStore } from './file/inMemoryFileStore';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CoordinateField as UICoordinateField } from 'capture-ui';
import Dialog from '@material-ui/core/Dialog';
import DialogTitle from '@material-ui/core/DialogTitle';
import { typeof orientations } from '../../../New';
import { withCenterPoint } from '../../HOC';

const getStyles = (theme: Theme) => ({
inputWrapperFocused: {
Expand Down Expand Up @@ -93,4 +94,4 @@ class CoordinateFieldPlain extends React.Component<Props> {
}
}

export const CoordinateField = withStyles(getStyles)(CoordinateFieldPlain);
export const CoordinateField = withStyles(getStyles)(withCenterPoint()(CoordinateFieldPlain));
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import withStyles from '@material-ui/core/styles/withStyles';
import { PolygonField as UIPolygonField } from 'capture-ui';
import { Dialog, DialogTitle } from '@material-ui/core';
import { typeof orientations } from '../../../New';
import { withCenterPoint } from '../../HOC';

const getStyles = () => ({
dialogPaper: {
Expand Down Expand Up @@ -56,4 +57,4 @@ class PolygonFieldPlain extends React.Component<Props> {
}
}

export const PolygonField = withStyles(getStyles)(PolygonFieldPlain);
export const PolygonField = withStyles(getStyles)(withCenterPoint()(PolygonFieldPlain));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @flow
export { withCenterPoint } from './withCenterPoint';
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// @flow
import React, { type ComponentType, useMemo, useState } from 'react';
import { useApiMetadataQuery } from 'capture-core/utils/reactQueryHelpers';

const DEFAULT_CENTER = [51.505, -0.09];

const convertToClientCoordinates = ({ coordinates, type }: { coordinates: any[], type: string }) => {
switch (type) {
case 'Point':
return [coordinates[1], coordinates[0]];
case 'Polygon':
return coordinates[0][0];
default:
return DEFAULT_CENTER;
}
};

const getCenterPoint = (InnerComponent: ComponentType<any>) => (props: Object) => {
const { orgUnit, ...passOnProps } = props;
const [orgUnitKey, setOrgUnitKey] = useState(orgUnit.id);
const [shouldFetch, setShouldFetch] = useState(false);
const queryKey = ['organisationUnit', orgUnitKey];
const queryFn = {
resource: 'organisationUnits',
id: () => orgUnitKey,
params: {
fields: 'geometry,parent',
},
};
const queryOptions = useMemo(
() => ({ enabled: Boolean(orgUnit.id) && shouldFetch }),
[shouldFetch, orgUnit.id],
);
const { data } = useApiMetadataQuery<any>(queryKey, queryFn, queryOptions);

const center = useMemo(() => {
if (data) {
const { geometry, parent } = data;
if (geometry) {
return convertToClientCoordinates(geometry);
} else if (parent?.id) {
setOrgUnitKey(parent.id);
}
return DEFAULT_CENTER;
}
return undefined;
}, [data]);

const onOpenMap = (hasValue) => {
setShouldFetch(!hasValue);
};

return <InnerComponent {...passOnProps} center={center} onOpenMap={onOpenMap} />;
};

export const withCenterPoint = () => (InnerComponent: ComponentType<any>) => getCenterPoint(InnerComponent);
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export { withLabel } from './HOC/withLabel';
export { withStyledContainer } from './HOC/withStyledContainer';
export { withOptionsIconElement } from './HOC/withOptionsIconElement';
export { withFocusSaver, withInternalChangeHandler } from 'capture-ui';
export { withCenterPoint } from './HOC/withCenterPoint';

// OrgUnit HOCs
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { AddLocation } from './AddLocation';
import type { PlainProps } from './actions.types';
import { LoadingMaskForButton } from '../../LoadingMasks';
import { MapModal } from '../MapModal';
import { useCenterPoint } from '../hooks/useCenterPoint';

const styles = {
actions: {
Expand Down Expand Up @@ -40,7 +39,6 @@ export const ActionsPlain = ({
}: PlainProps) => {
const [isOpenActions, setOpenActions] = useState(false);
const [isOpenMap, setOpenMap] = useState(false);
const { center } = useCenterPoint(enrollment.orgUnit);
const handleOnUpdate = (arg) => {
setOpenActions(false);
onUpdate(arg);
Expand Down Expand Up @@ -111,7 +109,6 @@ export const ActionsPlain = ({
enrollment={enrollment}
onUpdate={handleOnUpdate}
setOpenMap={setOpenMap}
center={center}
/>}
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import React, { useCallback } from 'react';
import { useGeometry } from '../hooks/useGeometry';
import type { MapModalProps } from './MapModal.types';
import { MapModal as MapModalComponent } from './MapModal.component';
import { useCenterPoint } from './hooks';

export const MapModal = ({
enrollment,
onUpdate,
setOpenMap,
defaultValues,
center,
center: storedCenter,
}: MapModalProps) => {
const { geometryType, dataElementType } = useGeometry(enrollment);
const { center } = useCenterPoint(enrollment.orgUnit, storedCenter);

const onSetCoordinates = useCallback((coordinates) => {
const geometry = coordinates ? { type: geometryType, coordinates } : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @flow
export { useCenterPoint } from './useCenterPoint';
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// @flow
import { useMemo, useState } from 'react';
import { useApiMetadataQuery } from 'capture-core/utils/reactQueryHelpers';

const DEFAULT_CENTER = [51.505, -0.09];

const convertToClientCoordinates = ({ coordinates, type }: { coordinates: any[], type: string }) => {
switch (type) {
case 'Point':
return [coordinates[1], coordinates[0]];
case 'Polygon':
return coordinates[0][0];
default:
return DEFAULT_CENTER;
}
};

export const useCenterPoint = (orgUnitId: string, storedCenter: ?[number, number]) => {
const [orgUnitKey, setOrgUnitKey] = useState(orgUnitId);
const queryKey = ['organisationUnit', orgUnitKey];
const queryFn = {
resource: 'organisationUnits',
id: () => orgUnitKey,
params: {
fields: 'geometry,parent',
},
};
const queryOptions = { enabled: !storedCenter && Boolean(orgUnitId) };
const { data, isLoading } = useApiMetadataQuery<any>(queryKey, queryFn, queryOptions);

const center = useMemo(() => {
if (data) {
const { geometry, parent } = data;
if (geometry) {
return convertToClientCoordinates(geometry);
} else if (parent?.id) {
setOrgUnitKey(parent.id);
}
return DEFAULT_CENTER;
}
return undefined;
}, [data]);

if (storedCenter) {
return {
center: storedCenter,
};
}

return {
center,
loading: isLoading,
};
};

This file was deleted.

Loading

0 comments on commit bb88f8d

Please sign in to comment.