diff --git a/frontend/package.json b/frontend/package.json index 5be506f3..4ba180be 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -45,6 +45,7 @@ "clsx": "^2.0.0", "cmdk": "^0.2.0", "d3-format": "^3.1.0", + "d3-time-format": "^4.1.0", "date-fns": "^2.30.0", "deck.gl": "8.9.31", "jotai": "2.4.3", diff --git a/frontend/src/components/widget/index.tsx b/frontend/src/components/widget/index.tsx index 858c3c66..e86a5c2f 100644 --- a/frontend/src/components/widget/index.tsx +++ b/frontend/src/components/widget/index.tsx @@ -1,4 +1,6 @@ -import { PropsWithChildren } from 'react'; +import { PropsWithChildren, useMemo } from 'react'; + +import { timeFormat } from 'd3-time-format'; type WidgetProps = { title: string; @@ -6,11 +8,16 @@ type WidgetProps = { }; const Widget: React.FC> = ({ title, lastUpdated, children }) => { + const formattedLastUpdated = useMemo( + () => timeFormat('%B %Y')(new Date(lastUpdated)), + [lastUpdated] + ); + return (

{title}

- Data last updated: {lastUpdated} + Data last updated: {formattedLastUpdated}
{children}
diff --git a/frontend/src/containers/data-tool/sidebar/widgets/habitat/index.tsx b/frontend/src/containers/data-tool/sidebar/widgets/habitat/index.tsx index c7fb8ed6..c37c333e 100644 --- a/frontend/src/containers/data-tool/sidebar/widgets/habitat/index.tsx +++ b/frontend/src/containers/data-tool/sidebar/widgets/habitat/index.tsx @@ -11,19 +11,43 @@ type HabitatWidgetProps = { }; const HabitatWidget: React.FC = ({ location }) => { - const lastUpdated = 'October 2023'; - - const { data: habitatStatsResponse } = useGetHabitatStats({ - populate: '*', + const defaultQueryParams = { filters: { location: { code: location.code, }, }, - 'pagination[limit]': -1, - }); + }; + + const { data: dataLastUpdate } = useGetHabitatStats( + { + ...defaultQueryParams, + sort: 'updatedAt:desc', + 'pagination[limit]': 1, + }, + { + query: { + select: ({ data }) => data?.[0]?.attributes?.updatedAt, + placeholderData: { data: null }, + }, + } + ); - const habitatStatsData = habitatStatsResponse?.data; + const { + data: { data: habitatStatsData }, + } = useGetHabitatStats( + { + ...defaultQueryParams, + populate: '*', + 'pagination[limit]': -1, + }, + { + query: { + select: ({ data }) => ({ data }), + placeholderData: { data: [] }, + }, + } + ); const widgetChartData = useMemo(() => { if (!habitatStatsData) return []; @@ -50,7 +74,7 @@ const HabitatWidget: React.FC = ({ location }) => { return ( {widgetChartData.map((chartData) => ( diff --git a/frontend/src/containers/data-tool/sidebar/widgets/marine-conservation/index.tsx b/frontend/src/containers/data-tool/sidebar/widgets/marine-conservation/index.tsx index 4e68bfac..38c89a6d 100644 --- a/frontend/src/containers/data-tool/sidebar/widgets/marine-conservation/index.tsx +++ b/frontend/src/containers/data-tool/sidebar/widgets/marine-conservation/index.tsx @@ -13,32 +13,51 @@ type MarineConservationWidgetProps = { }; const MarineConservationWidget: React.FC = ({ location }) => { - const lastUpdated = 'October 2023'; - - const { data: protectionStatsResponse } = useGetProtectionCoverageStats({ - populate: '*', - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - 'sort[year]': 'asc', + const defaultQueryParams = { filters: { location: { code: location.code, }, - // protection_status: { - // slug: 'oecm', // oecm | mpa - // }, }, - 'pagination[limit]': -1, - }); + }; + + const { data: dataLastUpdate } = useGetProtectionCoverageStats( + { + ...defaultQueryParams, + sort: 'updatedAt:desc', + 'pagination[limit]': 1, + }, + { + query: { + select: ({ data }) => data?.[0]?.attributes?.updatedAt, + placeholderData: { data: null }, + }, + } + ); - const protectionStats = useMemo(() => { - return protectionStatsResponse?.data || []; - }, [protectionStatsResponse?.data]); + const { + data: { data: protectionStatsData }, + } = useGetProtectionCoverageStats( + { + ...defaultQueryParams, + populate: '*', + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + 'sort[year]': 'asc', + 'pagination[limit]': -1, + }, + { + query: { + select: ({ data }) => ({ data }), + placeholderData: { data: [] }, + }, + } + ); const mergedProtectionStats = useMemo(() => { - if (!protectionStats.length) return null; + if (!protectionStatsData.length) return null; - const groupedByYear = groupBy(protectionStats, 'attributes.year'); + const groupedByYear = groupBy(protectionStatsData, 'attributes.year'); return Object.keys(groupedByYear).map((year) => { const entries = groupedByYear[year]; @@ -54,7 +73,7 @@ const MarineConservationWidget: React.FC = ({ loc protectedArea, }; }); - }, [protectionStats]); + }, [protectionStatsData]); const stats = useMemo(() => { if (!mergedProtectionStats) return null; @@ -115,7 +134,7 @@ const MarineConservationWidget: React.FC = ({ loc if (!stats || !chartData) return null; return ( - +
{stats?.protectedPercentage} diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 9fb2fd81..95478431 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -5850,7 +5850,7 @@ __metadata: languageName: node linkType: hard -"d3-time-format@npm:2 - 4": +"d3-time-format@npm:2 - 4, d3-time-format@npm:^4.1.0": version: 4.1.0 resolution: "d3-time-format@npm:4.1.0" dependencies: @@ -11617,6 +11617,7 @@ __metadata: clsx: ^2.0.0 cmdk: ^0.2.0 d3-format: ^3.1.0 + d3-time-format: ^4.1.0 date-fns: ^2.30.0 deck.gl: 8.9.31 eslint: 8.32.0