Skip to content

Commit

Permalink
fix: rules, coordinates etc
Browse files Browse the repository at this point in the history
  • Loading branch information
JoakimSM committed Jan 20, 2025
1 parent c2fbae8 commit a4bf697
Show file tree
Hide file tree
Showing 31 changed files with 291 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
export type D2FunctionsInput = $ReadOnly<{|
dateUtils: IDateUtils,
variablesHash: RuleVariables,
selectedOrgUnit: OrgUnit,
selectedOrgUnit: ?OrgUnit,
selectedUserRoles: Array<string>,
|}>;

Expand Down
2 changes: 1 addition & 1 deletion packages/rules-engine/src/rulesEngine.types.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export type RulesEngineInput = {|
selectedEntity?: ?TEIValues,
trackedEntityAttributes?: ?TrackedEntityAttributes,
selectedEnrollment?: ?Enrollment,
selectedOrgUnit: OrgUnit,
selectedOrgUnit: ?OrgUnit,
selectedUserRoles?: ?Array<string>,
optionSets: OptionSets,
|}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type SourceData = {
selectedEntity: ?TEIValues,
selectedEnrollment: ?Enrollment,
optionSets: OptionSets,
selectedOrgUnit: OrgUnit,
selectedOrgUnit: ?OrgUnit,
};

const variableSourceTypesDataElementSpecific = {
Expand Down Expand Up @@ -516,7 +516,7 @@ export class VariableService {
return variables;
}

getOrganisationContextVariables(orgUnit: OrgUnit) {
getOrganisationContextVariables(orgUnit: ?OrgUnit) {
const variables = {};
variables.orgunit_code = this.buildContextVariable(orgUnit?.code, typeKeys.TEXT);
return variables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type VariableServiceInput = {|
selectedEntity: ?TEIValues,
trackedEntityAttributes: ?TrackedEntityAttributes,
selectedEnrollment: ?Enrollment,
selectedOrgUnit: OrgUnit,
selectedOrgUnit: ?OrgUnit,
optionSets: OptionSets,
constants: ?Constants,
|};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ const getGeometrySettings = () => ({
dialogLabel: i18n.t('Area'),
required: false,
orientation: getOrientation(props.formHorizontal),
orgUnit: props.orgUnit,
orgUnitId: props.orgUnit?.id,
});
}

Expand All @@ -248,7 +248,7 @@ const getGeometrySettings = () => ({
required: false,
orientation: getOrientation(props.formHorizontal),
shrinkDisabled: props.formHorizontal,
orgUnit: props.orgUnit,
orgUnitId: props.orgUnit?.id,
});
},
getPropName: () => 'geometry',
Expand Down Expand Up @@ -329,6 +329,7 @@ type FinalTeiDataEntryProps = {
programId: string,
id: string,
orgUnitId: string,
orgUnit: OrgUnit,
onUpdateDataEntryField: Function,
onUpdateFormFieldAsync: Function,
onUpdateFormField: Function,
Expand Down Expand Up @@ -457,6 +458,7 @@ export class EnrollmentDataEntryComponent extends React.Component<PreEnrollmentD
onUpdateDataEntryField,
onStartAsyncUpdateField,
onGetUnsavedAttributeValues,
orgUnit,
...passOnProps
} = this.props;

Expand All @@ -467,6 +469,8 @@ export class EnrollmentDataEntryComponent extends React.Component<PreEnrollmentD
onUpdateFormField={this.handleUpdateField}
onUpdateDataEntryField={this.handleUpdateDataEntryField}
onUpdateFormFieldAsync={this.handleStartAsyncUpdateField}
orgUnit={orgUnit}
orgUnitId={orgUnit?.id}
{...passOnProps}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const getStageGeometrySettings = () => ({
dialogLabel: i18n.t('Area'),
required: false,
orientation: getOrientation(props.formHorizontal),
orgUnit: props.orgUnit,
orgUnitId: props.orgUnit?.id,
});
}

Expand All @@ -139,7 +139,7 @@ const getStageGeometrySettings = () => ({
required: false,
orientation: getOrientation(props.formHorizontal),
shrinkDisabled: props.formHorizontal,
orgUnit: props.orgUnit,
orgUnitId: props.orgUnit?.id,
});
},
getPropName: () => stageMainDataIds.GEOMETRY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const getSectionId = sectionId =>
(sectionId === Section.MAIN_SECTION_ID ? `${Section.MAIN_SECTION_ID}-stage` : sectionId);

export const EnrollmentWithFirstStageDataEntry = (props: Props) => {
const { firstStageMetaData, ...passOnProps } = props;
const { firstStageMetaData, orgUnit, ...passOnProps } = props;
const {
stage: { stageForm: firstStageFormFoundation, name: stageName },
} = firstStageMetaData;
Expand All @@ -21,6 +21,8 @@ export const EnrollmentWithFirstStageDataEntry = (props: Props) => {
return (
<FirstStageDataEntry
{...passOnProps}
orgUnit={orgUnit}
orgUnitId={orgUnit?.id}
firstStageMetaData={firstStageMetaData}
dataEntrySections={dataEntrySections}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// @flow
import type { OrgUnit } from '@dhis2/rules-engine-javascript';
import type { ProgramStage, RenderFoundation } from '../../../../metaData';

export type Props = {
firstStageMetaData: {
stage: ProgramStage,
},
formFoundation: RenderFoundation,
orgUnit: ?OrgUnit,
};
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const buildGeometrySettingsFn = () => ({
dialogLabel: i18n.t('Area'),
required: false,
orientation: getOrientation(props.formHorizontal),
orgUnit: props.orgUnit,
orgUnitId: props.orgUnit?.id,
});
}

Expand All @@ -247,7 +247,7 @@ const buildGeometrySettingsFn = () => ({
required: false,
orientation: getOrientation(props.formHorizontal),
shrinkDisabled: props.formHorizontal,
orgUnit: props.orgUnit,
orgUnitId: props.orgUnit?.id,
});
},
getPropName: () => 'geometry',
Expand Down Expand Up @@ -637,6 +637,7 @@ class NewEventDataEntry extends Component<Props> {
dataEntrySections={this.dataEntrySections}
relationshipsRef={this.setRelationshipsInstance}
orgUnit={orgUnit}
orgUnitId={orgUnit?.id}
{...passOnProps}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export {
} from './actions/dataEntry.actions';
export { actionTypes as loadNewActionTypes } from './actions/dataEntryLoadNew.actions';
export { actionTypes as loadEditActionTypes, cleanUpDataEntry } from './actions/dataEntry.actions';
export { getDataEntryKey } from './common/getDataEntryKey';

Original file line number Diff line number Diff line change
@@ -1,59 +1,115 @@
// @flow
import React, { type ComponentType, useMemo, useState } from 'react';
import React, { type ComponentType, useMemo, useState, useEffect } from 'react';
import { ceil } from 'lodash';
import { useApiMetadataQuery } from 'capture-core/utils/reactQueryHelpers';

type Props = {
orgUnitId: ?string,
};

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];
case 'Polygon': {
// Calculate a center point by finding the min and max values for longitude and latitude
// and getting the mean of those values
const { minLatitude, maxLatitude, minLongitude, maxLongitude } = coordinates[0]
.reduce((accExtremes, [iLongitude, iLatitude]) => {
if (iLatitude > accExtremes.maxLatitude) {
accExtremes.maxLatitude = iLatitude;
} else if (iLatitude < accExtremes.minLatitude) {
accExtremes.minLatitude = iLatitude;
}

if (iLongitude > accExtremes.maxLongitude) {
accExtremes.maxLongitude = iLongitude;
} else if (iLongitude < accExtremes.minLongitude) {
accExtremes.minLongitude = iLongitude;
}

return accExtremes;
}, {
minLatitude: coordinates[0][0][1],
maxLatitude: coordinates[0][0][1],
minLongitude: coordinates[0][0][0],
maxLongitude: coordinates[0][0][0],
});

const latitude = ceil((maxLatitude + minLatitude) / 2, 6);
const longitude = ceil((maxLongitude + minLongitude) / 2, 6);

return [latitude, longitude];
}
default:
return DEFAULT_CENTER;
}
};

const getCenterPoint = (InnerComponent: ComponentType<any>) => (props: Object) => {
const { orgUnit, ...passOnProps } = props;
if (!orgUnit || !orgUnit.id) {
return <InnerComponent {...passOnProps} center={DEFAULT_CENTER} onOpenMap={() => {}} />;
}
const [orgUnitKey, setOrgUnitKey] = useState(orgUnit.id);
const [shouldFetch, setShouldFetch] = useState(false);
const queryKey = ['organisationUnit', 'geometry', orgUnitKey];
const getCenterPoint = (InnerComponent: ComponentType<any>) => ({ orgUnitId, ...passOnProps }: Props) => {
const [orgUnitFetchId, setOrgUnitFetchId] = useState(orgUnitId);
const [fetchEnabled, setFetchEnabled] = useState(false);

useEffect(() => {
setOrgUnitFetchId(orgUnitId);
}, [orgUnitId]);

const queryKey = ['organisationUnit', 'geometry', orgUnitFetchId];
const queryFn = {
resource: 'organisationUnits',
id: () => orgUnitKey,
id: () => orgUnitFetchId,
params: {
fields: 'geometry,parent',
},
};
const queryOptions = useMemo(
() => ({ enabled: Boolean(orgUnit.id) && shouldFetch }),
[shouldFetch, orgUnit.id],
() => ({ enabled: Boolean(orgUnitFetchId) && fetchEnabled }),
[fetchEnabled, orgUnitFetchId],
);

// $FlowFixMe When the query is disabled, the prerequisites for the queryKey and the queryFn are not met.
const { data } = useApiMetadataQuery<any>(queryKey, queryFn, queryOptions);

useEffect(() => {
if (data?.parent && !data?.geometry) {
setOrgUnitFetchId(data.parent.id);
}
}, [data]);

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

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

return <InnerComponent {...passOnProps} center={center} onOpenMap={onOpenMap} />;
const onCloseMap = () => {
setFetchEnabled(false);
};

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

export const withCenterPoint = () => (InnerComponent: ComponentType<any>) => getCenterPoint(InnerComponent);
Loading

0 comments on commit a4bf697

Please sign in to comment.