-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Vizzuality/SKY30-137-fe-implement-the-mar…
…ine-conservation-protection-types-widget [SKY30-137] FE - Implement the "Marine Conservation Protection Types" widget
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
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,4 @@ | ||
export const PROTECTION_TYPES_CHART_COLORS = { | ||
'fully-highly-protected': '#FD8E28', | ||
'less-protected-unknown': '#FFDD88', | ||
}; |
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
88 changes: 88 additions & 0 deletions
88
frontend/src/containers/data-tool/sidebar/widgets/protection-types/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,88 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import HorizontalBarChart from '@/components/charts/horizontal-bar-chart'; | ||
import Widget from '@/components/widget'; | ||
import { PROTECTION_TYPES_CHART_COLORS } from '@/constants/protection-types-chart-colors'; | ||
import { useGetMpaaProtectionLevelStats } from '@/types/generated/mpaa-protection-level-stat'; | ||
import type { Location } from '@/types/generated/strapi.schemas'; | ||
|
||
type ProtectionTypesWidgetProps = { | ||
location: Location; | ||
}; | ||
|
||
const ProtectionTypesWidget: React.FC<ProtectionTypesWidgetProps> = ({ location }) => { | ||
// Default params: filter by location | ||
const defaultQueryParams = { | ||
filters: { | ||
location: { | ||
code: location.code, | ||
}, | ||
}, | ||
}; | ||
|
||
// Find last updated in order to display the last data update | ||
const { data: dataLastUpdate } = useGetMpaaProtectionLevelStats( | ||
{ | ||
...defaultQueryParams, | ||
sort: 'updatedAt:desc', | ||
'pagination[limit]': 1, | ||
}, | ||
{ | ||
query: { | ||
select: ({ data }) => data?.[0]?.attributes?.updatedAt, | ||
placeholderData: { data: null }, | ||
}, | ||
} | ||
); | ||
|
||
// Get protection levels by location | ||
const { | ||
data: { data: protectionLevelStatsData }, | ||
} = useGetMpaaProtectionLevelStats( | ||
{ | ||
...defaultQueryParams, | ||
populate: '*', | ||
'pagination[limit]': -1, | ||
}, | ||
{ | ||
query: { | ||
select: ({ data }) => ({ data }), | ||
placeholderData: { data: [] }, | ||
}, | ||
} | ||
); | ||
|
||
// Parse data to display in the chart | ||
const widgetChartData = useMemo(() => { | ||
if (!protectionLevelStatsData.length) return []; | ||
|
||
const parsedData = protectionLevelStatsData.map((entry) => { | ||
const stats = entry?.attributes; | ||
const protectionLevel = stats?.mpaa_protection_level?.data.attributes; | ||
|
||
return { | ||
title: protectionLevel.name, | ||
slug: protectionLevel.slug, | ||
barColor: PROTECTION_TYPES_CHART_COLORS[protectionLevel.slug], | ||
totalArea: location.totalMarineArea, | ||
protectedArea: stats.area, | ||
info: protectionLevel.info, | ||
}; | ||
}); | ||
|
||
return parsedData; | ||
}, [location, protectionLevelStatsData]); | ||
|
||
// If there's no widget data, don't display the widget | ||
if (!widgetChartData.length) return null; | ||
|
||
return ( | ||
<Widget title="Marine Conservation Protection Types" lastUpdated={dataLastUpdate}> | ||
{widgetChartData.map((chartData) => ( | ||
<HorizontalBarChart key={chartData.slug} className="py-2" data={chartData} /> | ||
))} | ||
</Widget> | ||
); | ||
}; | ||
|
||
export default ProtectionTypesWidget; |