Skip to content

Commit

Permalink
Merge pull request #284 from Vizzuality/SKY30-423-marine-conservation…
Browse files Browse the repository at this point in the history
…-protection-levels-not-working

[SKY30-423] Marine Conservation Protection Levels not working
  • Loading branch information
SARodrigues authored Jul 19, 2024
2 parents cee2db7 + b48a3e9 commit 56d9a1d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 48 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import Widget from '@/components/widget';
import { PROTECTION_TYPES_CHART_COLORS } from '@/constants/protection-types-chart-colors';
import { FCWithMessages } from '@/types';
import { useGetDataInfos } from '@/types/generated/data-info';
import { useGetLocations } from '@/types/generated/location';
import { useGetMpaaProtectionLevelStats } from '@/types/generated/mpaa-protection-level-stat';
import type { LocationGroupsDataItemAttributes } from '@/types/generated/strapi.schemas';

import { PROTECTION_LEVEL_NAME_SUBSTITUTIONS } from './constants';

type ProtectionTypesWidgetProps = {
location: LocationGroupsDataItemAttributes;
};
Expand All @@ -22,28 +20,20 @@ const ProtectionTypesWidget: FCWithMessages<ProtectionTypesWidgetProps> = ({ loc

// Get protection levels data for the location
const {
data: { data: protectionLevelsData },
isFetching: isFetchingProtectionLevelsData,
} = useGetLocations(
data: { data: protectionLevelsStatsData },
isFetching: isFetchingProtectionLevelsStatsData,
} = useGetMpaaProtectionLevelStats(
{
locale,
filters: {
code: location?.code,
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
populate: {
mpaa_protection_level_stats: {
filters: {
mpaa_protection_level: {
slug: 'fully-highly-protected',
},
},
populate: {
mpaa_protection_level: '*',
},
location: {
code: location?.code || 'GLOB',
},
mpaa_protection_level: {
slug: 'fully-highly-protected',
},
},
populate: '*',
'pagination[limit]': -1,
},
{
Expand Down Expand Up @@ -84,47 +74,42 @@ const ProtectionTypesWidget: FCWithMessages<ProtectionTypesWidgetProps> = ({ loc

// Go through all the relevant stats, find the last updated one's value
const lastUpdated = useMemo(() => {
const protectionLevelStats =
protectionLevelsData[0]?.attributes?.mpaa_protection_level_stats?.data;
const updatedAtValues = protectionLevelStats?.reduce(
const updatedAtValues = protectionLevelsStatsData?.reduce(
(acc, curr) => [...acc, curr?.attributes?.updatedAt],
[]
);

return updatedAtValues?.sort()?.reverse()?.[0];
}, [protectionLevelsData]);
}, [protectionLevelsStatsData]);

// Parse data to display in the chart
const widgetChartData = useMemo(() => {
if (!protectionLevelsData.length) return [];
if (!protectionLevelsStatsData.length) return [];

const parseProtectionLevelStats = (protectionLevelStats) => {
const mpaaProtectionLevel = protectionLevelStats?.mpaa_protection_level?.data?.attributes;
const location = protectionLevelStats?.location?.data?.attributes;

const barColor = PROTECTION_TYPES_CHART_COLORS[mpaaProtectionLevel?.slug];

const parsedProtectionLevel = (label, protectionLevel, stats) => {
return {
title: label,
slug: protectionLevel.slug,
background: PROTECTION_TYPES_CHART_COLORS[protectionLevel.slug],
totalArea: location.totalMarineArea,
protectedArea: stats?.area,
title: mpaaProtectionLevel?.name,
slug: mpaaProtectionLevel?.slug,
background: barColor,
totalArea: location?.totalMarineArea,
protectedArea: protectionLevelStats?.area,
info: metadata?.info,
sources: metadata?.sources,
};
};

const parsedMpaaProtectionLevelData =
protectionLevelsData[0]?.attributes?.mpaa_protection_level_stats?.data?.map((entry) => {
const stats = entry?.attributes;
const protectionLevel = stats?.mpaa_protection_level?.data.attributes;
const displayName =
PROTECTION_LEVEL_NAME_SUBSTITUTIONS[protectionLevel.slug] || protectionLevel?.name;
return parsedProtectionLevel(displayName, protectionLevel, stats);
});

return parsedMpaaProtectionLevelData;
}, [location, protectionLevelsData, metadata]);
return protectionLevelsStatsData?.map(({ attributes }) =>
parseProtectionLevelStats(attributes)
);
}, [metadata, protectionLevelsStatsData]);

const noData = !widgetChartData.length;

const loading = isFetchingProtectionLevelsData;
const loading = isFetchingProtectionLevelsStatsData;

return (
<Widget
Expand Down

0 comments on commit 56d9a1d

Please sign in to comment.