-
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.
feat: fetch the org unit only when the map is opened using useApiMeta…
…dataQuery
- Loading branch information
1 parent
abb8d9d
commit bb88f8d
Showing
24 changed files
with
148 additions
and
203 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
51 changes: 0 additions & 51 deletions
51
src/core_modules/capture-core/components/DataEntry/withCenterPoint.js
This file was deleted.
Oops, something went wrong.
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', 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
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', 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, | ||
}; | ||
}; |
54 changes: 0 additions & 54 deletions
54
src/core_modules/capture-core/components/WidgetEnrollment/hooks/useCenterPoint.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.