-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: [DHIS2-6335] use custom map center point (#3312)
- Loading branch information
1 parent
b1d88f4
commit 2dbaffe
Showing
21 changed files
with
155 additions
and
26 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
2 changes: 2 additions & 0 deletions
2
src/core_modules/capture-core/components/FormFields/New/HOC/index.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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @flow | ||
export { withCenterPoint } from './withCenterPoint'; |
56 changes: 56 additions & 0 deletions
56
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 |
---|---|---|
@@ -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', 'geometry', 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); |
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
src/core_modules/capture-core/components/WidgetEnrollment/MapModal/hooks/index.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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// @flow | ||
export { useCenterPoint } from './useCenterPoint'; |
54 changes: 54 additions & 0 deletions
54
src/core_modules/capture-core/components/WidgetEnrollment/MapModal/hooks/useCenterPoint.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 |
---|---|---|
@@ -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', 'geometry', 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 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
Oops, something went wrong.