-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
291 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...s/Enrollment/EnrollmentWithFirstStageDataEntry/EnrollmentWithFirstStageDataEntry.types.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 79 additions & 23 deletions
102
src/core_modules/capture-core/components/FormFields/New/HOC/withCenterPoint.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.