-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add learning by sector,regions bar chart and Map for learning #1577
Merged
samshara
merged 11 commits into
project/operational-learning-2.0
from
feature/add-charts-learnings
Jan 29, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
29848de
Add keyfigues for operational learning
roshni73 a22f937
fix: operational learning bug fixes
samshara aa5550d
fix: operational learning bug fixes
samshara 850fc64
Add keyfigues for operational learning
roshni73 55b99c1
Use GlobalMap instead of BaseMap
roshni73 e06da79
feat: extract operation learning stats into a standalone component
samshara e9bbd65
feat: refactor operational learning map
samshara 4983278
fix: update `getDatesSeparatedByYear` function
samshara e00d8bd
feat: exclude sub-components from the PER components filter in Operat…
samshara e7932c3
feat(operational-learning): show source details on click for sources …
samshara 3123692
fix: remove unused usePerComponent hook
samshara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
7 changes: 7 additions & 0 deletions
7
app/src/views/OperationalLearning/Stats/OperationalLearningMap/i18n.json
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,7 @@ | ||
{ | ||
"namespace": "operationalLearning", | ||
"strings": { | ||
"downloadMapTitle": "Operational learning map", | ||
"learningCount": "Learning count" | ||
} | ||
} |
267 changes: 267 additions & 0 deletions
267
app/src/views/OperationalLearning/Stats/OperationalLearningMap/index.tsx
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,267 @@ | ||
import { | ||
useCallback, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import { | ||
Container, | ||
NumberOutput, | ||
TextOutput, | ||
} from '@ifrc-go/ui'; | ||
import { useTranslation } from '@ifrc-go/ui/hooks'; | ||
import { maxSafe } from '@ifrc-go/ui/utils'; | ||
import { | ||
_cs, | ||
isDefined, | ||
isNotDefined, | ||
listToMap, | ||
} from '@togglecorp/fujs'; | ||
import { | ||
MapBounds, | ||
MapLayer, | ||
MapSource, | ||
} from '@togglecorp/re-map'; | ||
import { type CirclePaint } from 'mapbox-gl'; | ||
|
||
import GlobalMap from '#components/domain/GlobalMap'; | ||
import Link from '#components/Link'; | ||
import MapContainerWithDisclaimer from '#components/MapContainerWithDisclaimer'; | ||
import MapPopup from '#components/MapPopup'; | ||
import useCountry from '#hooks/domain/useCountry'; | ||
import { | ||
DEFAULT_MAP_PADDING, | ||
DURATION_MAP_ZOOM, | ||
} from '#utils/constants'; | ||
import { getCountryListBoundingBox } from '#utils/map'; | ||
import { type GoApiResponse } from '#utils/restRequest'; | ||
|
||
import i18n from './i18n.json'; | ||
import styles from './styles.module.css'; | ||
|
||
type OperationLearningStatsResponse = GoApiResponse<'/api/v2/ops-learning/stats/'>; | ||
const sourceOptions: mapboxgl.GeoJSONSourceRaw = { | ||
type: 'geojson', | ||
}; | ||
|
||
interface CountryProperties { | ||
countryId: number; | ||
name: string; | ||
learningCount: number; | ||
} | ||
interface ClickedPoint { | ||
feature: GeoJSON.Feature<GeoJSON.Point, CountryProperties>; | ||
lngLat: mapboxgl.LngLatLike; | ||
} | ||
|
||
const LEARNING_COUNT_LOW_COLOR = '#AEB7C2'; | ||
const LEARNING_COUNT_HIGH_COLOR = '#011E41'; | ||
|
||
interface Props { | ||
className?: string; | ||
learningByCountry: OperationLearningStatsResponse['learning_by_country'] | undefined; | ||
} | ||
|
||
function OperationalLearningMap(props: Props) { | ||
const strings = useTranslation(i18n); | ||
const { | ||
className, | ||
learningByCountry, | ||
} = props; | ||
|
||
const [ | ||
clickedPointProperties, | ||
setClickedPointProperties, | ||
] = useState<ClickedPoint | undefined>(); | ||
|
||
const countries = useCountry(); | ||
|
||
const countriesMap = useMemo(() => ( | ||
listToMap(countries, (country) => country.id) | ||
), [countries]); | ||
|
||
const learningCountGeoJSON = useMemo( | ||
(): GeoJSON.FeatureCollection<GeoJSON.Geometry> | undefined => { | ||
if ((countries?.length ?? 0) < 1 || (learningByCountry?.length ?? 0) < 1) { | ||
return undefined; | ||
} | ||
|
||
const features = learningByCountry | ||
?.map((value) => { | ||
const country = countriesMap?.[value.country_id]; | ||
if (isNotDefined(country)) { | ||
return undefined; | ||
} | ||
return { | ||
type: 'Feature' as const, | ||
geometry: country.centroid as { | ||
type: 'Point', | ||
coordinates: [number, number], | ||
}, | ||
properties: { | ||
countryId: country.id, | ||
name: country.name, | ||
learningCount: value.count, | ||
}, | ||
}; | ||
}) | ||
.filter(isDefined) ?? []; | ||
|
||
return { | ||
type: 'FeatureCollection', | ||
features, | ||
}; | ||
}, | ||
[learningByCountry, countriesMap, countries], | ||
); | ||
|
||
const bluePointHaloCirclePaint: CirclePaint = useMemo(() => { | ||
const countriesWithLearning = learningByCountry?.filter((value) => value.count > 0); | ||
|
||
const maxScaleValue = countriesWithLearning && countriesWithLearning.length > 0 | ||
? Math.max( | ||
...(countriesWithLearning | ||
.map((country) => country.count)), | ||
) | ||
: 0; | ||
|
||
return { | ||
'circle-opacity': 0.9, | ||
'circle-color': [ | ||
'interpolate', | ||
['linear'], | ||
['number', ['get', 'learningCount']], | ||
0, | ||
LEARNING_COUNT_LOW_COLOR, | ||
maxScaleValue, | ||
LEARNING_COUNT_HIGH_COLOR, | ||
], | ||
'circle-radius': [ | ||
'interpolate', | ||
['linear'], | ||
['zoom'], | ||
3, 10, | ||
8, 15, | ||
], | ||
}; | ||
}, [learningByCountry]); | ||
|
||
const handlePointClose = useCallback(() => { | ||
setClickedPointProperties(undefined); | ||
}, []); | ||
|
||
const handlePointClick = useCallback( | ||
(feature: mapboxgl.MapboxGeoJSONFeature, lngLat: mapboxgl.LngLatLike) => { | ||
setClickedPointProperties({ | ||
feature: feature as unknown as ClickedPoint['feature'], | ||
lngLat, | ||
}); | ||
return true; | ||
}, | ||
[setClickedPointProperties], | ||
); | ||
|
||
const maxLearning = useMemo(() => ( | ||
maxSafe( | ||
learningByCountry?.map((value) => value.count), | ||
) | ||
), [learningByCountry]); | ||
|
||
const bounds = useMemo( | ||
() => { | ||
if (isNotDefined(learningByCountry)) { | ||
return undefined; | ||
} | ||
const countryList = learningByCountry | ||
.map((d) => countriesMap?.[d.country_id]) | ||
.filter(isDefined); | ||
return getCountryListBoundingBox(countryList); | ||
}, | ||
[countriesMap, learningByCountry], | ||
); | ||
|
||
return ( | ||
<Container | ||
className={_cs(styles.operationLearningMap, className)} | ||
footerClassName={styles.footer} | ||
footerContent={( | ||
<div className={styles.legend}> | ||
<div className={styles.legendLabel}>{strings.learningCount}</div> | ||
<div className={styles.legendContent}> | ||
<div | ||
className={styles.gradient} | ||
style={{ background: `linear-gradient(90deg, ${LEARNING_COUNT_LOW_COLOR}, ${LEARNING_COUNT_HIGH_COLOR})` }} | ||
/> | ||
<div className={styles.labelList}> | ||
<NumberOutput | ||
value={0} | ||
/> | ||
<NumberOutput | ||
value={maxLearning} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
)} | ||
childrenContainerClassName={styles.mainContent} | ||
> | ||
<GlobalMap> | ||
<MapContainerWithDisclaimer | ||
className={styles.mapContainer} | ||
title={strings.downloadMapTitle} | ||
/> | ||
{isDefined(learningCountGeoJSON) && ( | ||
<MapSource | ||
sourceKey="points" | ||
sourceOptions={sourceOptions} | ||
geoJson={learningCountGeoJSON} | ||
> | ||
<MapLayer | ||
layerKey="points-halo-circle" | ||
onClick={handlePointClick} | ||
layerOptions={{ | ||
type: 'circle', | ||
paint: bluePointHaloCirclePaint, | ||
}} | ||
/> | ||
</MapSource> | ||
)} | ||
{clickedPointProperties?.lngLat && ( | ||
<MapPopup | ||
onCloseButtonClick={handlePointClose} | ||
coordinates={clickedPointProperties.lngLat} | ||
heading={( | ||
<Link | ||
to="countriesLayout" | ||
urlParams={{ | ||
countryId: clickedPointProperties.feature.properties.countryId, | ||
}} | ||
> | ||
{clickedPointProperties.feature.properties.name} | ||
</Link> | ||
)} | ||
childrenContainerClassName={styles.popupContent} | ||
> | ||
<Container | ||
headingLevel={5} | ||
> | ||
<TextOutput | ||
value={clickedPointProperties.feature.properties.learningCount} | ||
label={strings.learningCount} | ||
valueType="number" | ||
/> | ||
</Container> | ||
</MapPopup> | ||
)} | ||
{isDefined(bounds) && ( | ||
<MapBounds | ||
duration={DURATION_MAP_ZOOM} | ||
bounds={bounds} | ||
padding={DEFAULT_MAP_PADDING} | ||
/> | ||
)} | ||
</GlobalMap> | ||
</Container> | ||
); | ||
} | ||
|
||
export default OperationalLearningMap; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.