-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Waitp 1224 overlay selector desktop (#4685)
* WAITP-1224 initial working selector * WAITP-1244 Tidy up files * Update border radius * Update border radius logic * Tidy ups
- Loading branch information
Showing
12 changed files
with
510 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { useQuery } from 'react-query'; | ||
import { get } from '../api'; | ||
import { EntityCode, ProjectCode, SingleMapOverlayItem } from '../../types'; | ||
|
||
export const useMapOverlayData = ( | ||
projectCode?: ProjectCode, | ||
entityCode?: EntityCode, | ||
mapOverlayCode?: SingleMapOverlayItem['code'], | ||
) => { | ||
return useQuery( | ||
['mapOverlayData', projectCode, entityCode, mapOverlayCode], | ||
async () => { | ||
return get('measureData', { | ||
params: { | ||
mapOverlayCode, | ||
organisationUnitCode: entityCode, | ||
projectCode, | ||
shouldShowAllParentCountryResults: projectCode !== entityCode, // TODO: figure out the logic here for shouldShowAllParentCountryResults | ||
}, | ||
}); | ||
}, | ||
{ | ||
enabled: !!projectCode && !!entityCode && !!mapOverlayCode, | ||
}, | ||
); | ||
}; |
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,77 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import { ChangeEvent } from 'react'; | ||
import { useParams, useSearchParams } from 'react-router-dom'; | ||
import { useQuery, UseQueryResult } from 'react-query'; | ||
import { get } from '../api'; | ||
import { MapOverlayGroup, SingleMapOverlayItem } from '../../types'; | ||
import { URL_SEARCH_PARAMS } from '../../constants'; | ||
|
||
const mapOverlayByCode = ( | ||
mapOverlayGroups: MapOverlayGroup[] = [], | ||
): Record<SingleMapOverlayItem['code'], SingleMapOverlayItem> => { | ||
return mapOverlayGroups.reduce( | ||
( | ||
result: Record<string, SingleMapOverlayItem>, | ||
mapOverlay: MapOverlayGroup | SingleMapOverlayItem, | ||
) => { | ||
if (mapOverlay.children) { | ||
return { ...result, ...mapOverlayByCode(mapOverlay.children) }; | ||
} | ||
return { | ||
...result, | ||
[mapOverlay.code]: mapOverlay, | ||
}; | ||
}, | ||
{}, | ||
); | ||
}; | ||
|
||
interface UseMapOverlaysResult { | ||
hasMapOverlays: boolean; | ||
mapOverlayGroups: MapOverlayGroup[]; | ||
isLoadingMapOverlays: boolean; | ||
errorLoadingMapOverlays: UseQueryResult['error']; | ||
selectedOverlayCode: string | null; | ||
selectedOverlay?: SingleMapOverlayItem; | ||
updateSelectedMapOverlay: (e: ChangeEvent<HTMLInputElement>) => void; | ||
} | ||
|
||
/** | ||
* Gets the map overlays and returns useful utils and values associated with these | ||
*/ | ||
export const useMapOverlays = (): UseMapOverlaysResult => { | ||
const [urlSearchParams, setUrlParams] = useSearchParams(); | ||
const { projectCode, entityCode } = useParams(); | ||
const { data, isLoading, error } = useQuery( | ||
['mapOverlays', projectCode, entityCode], | ||
async () => { | ||
return get(`mapOverlays/${projectCode}/${entityCode}`); | ||
}, | ||
{ | ||
enabled: !!projectCode && !!entityCode, | ||
}, | ||
); | ||
|
||
const selectedOverlayCode = urlSearchParams.get(URL_SEARCH_PARAMS.MAP_OVERLAY); | ||
const codedOverlays = mapOverlayByCode(data?.mapOverlays); | ||
|
||
const selectedOverlay = codedOverlays[selectedOverlayCode!]; | ||
|
||
const updateSelectedMapOverlay = (e: ChangeEvent<HTMLInputElement>) => { | ||
urlSearchParams.set(URL_SEARCH_PARAMS.MAP_OVERLAY, e.target.value); | ||
setUrlParams(urlSearchParams); | ||
}; | ||
|
||
return { | ||
hasMapOverlays: !!data?.mapOverlays?.length, | ||
mapOverlayGroups: data?.mapOverlays, | ||
isLoadingMapOverlays: isLoading, | ||
errorLoadingMapOverlays: error, | ||
selectedOverlayCode, | ||
selectedOverlay, | ||
updateSelectedMapOverlay, | ||
}; | ||
}; |
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.