Skip to content

Commit

Permalink
Fix compare label on block map
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbyul-here committed Sep 19, 2024
1 parent e93f1ab commit b174470
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
13 changes: 8 additions & 5 deletions app/scripts/components/common/blocks/block-map.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useMemo, useState, useEffect } from 'react';
import styled from 'styled-components';
import { MapboxOptions } from 'mapbox-gl';
import * as dateFns from 'date-fns';
import {
convertProjectionToMapbox,
projectionDefault,
Expand Down Expand Up @@ -162,7 +163,6 @@ function MapBlock(props: MapBlockProps) {
}

const datasetLayers = getDatasetLayers(datasets);

const layersToFetch = useMemo(() => {
const [baseMapStaticData] = reconcileDatasets([layerId], datasetLayers, []);
let totalLayers = [baseMapStaticData];
Expand Down Expand Up @@ -259,9 +259,12 @@ function MapBlock(props: MapBlockProps) {

const computedCompareLabel = useMemo(() => {
// Use a provided label if it exist.
if (compareLabel && compareDataLayer) {
const providedLabel = compareDataLayer.data.mapLabel as string;
return providedLabel;
if (compareLabel) return compareLabel as string;
// Use label function from originalData.Compare
else if (baseDataLayer?.data.compare?.mapLabel) {
if (typeof baseDataLayer.data.compare.mapLabel === 'string') return baseDataLayer.data.compare.mapLabel;
const labelFn = baseDataLayer.data.compare.mapLabel as (unknown) => string;
return labelFn({dateFns, datetime: selectedDatetime, compareDatetime: compareToDate });
}

// Default to date comparison.
Expand All @@ -275,7 +278,7 @@ function MapBlock(props: MapBlockProps) {
: null;
}, [
compareLabel,
compareDataLayer,
baseDataLayer,
selectedDatetime,
compareToDate,
baseTimeDensity,
Expand Down
16 changes: 16 additions & 0 deletions app/scripts/components/sandbox/mdx-page/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@
</Figure>
</Block>

<Block type='wide'>
<Figure>
<Map
datasetId='no2'
layerId='no2-monthly'
dateTime='2020-03-01'
compareDateTime='2018-03-01'
compareLabel='overriding compare label'
projectionId='equirectangular'
allowProjectionChange
isComparing={true}
/>
<Caption>Comparing dates set manually</Caption>
</Figure>
</Block>

<Block type='full'>
<Figure>
<Map
Expand Down
4 changes: 2 additions & 2 deletions mock/datasets/no2.data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ layers:
layerId: nightlights-hd-monthly
mapLabel: |
::js ({ dateFns, datetime, compareDatetime }) => {
return `${dateFns.format(datetime, 'LLL yyyy')} VS ${dateFns.format(compareDatetime, 'LLL yyyy')}`;
return `${dateFns.format(datetime, 'yyyy')} VS ${dateFns.format(compareDatetime, 'LLL yyyy')}`;
}
legend:
unit:
Expand Down Expand Up @@ -163,7 +163,7 @@ layers:
layerId: no2-monthly-diff
mapLabel: |
::js ({ dateFns, datetime, compareDatetime }) => {
return `${dateFns.format(datetime, 'LLL yyyy')} VS ${dateFns.format(compareDatetime, 'LLL yyyy')}`;
return `${dateFns.format(datetime, 'yyyy')} VS ${dateFns.format(compareDatetime, 'LLL yyyy')}`;
}
legend:
unit:
Expand Down

0 comments on commit b174470

Please sign in to comment.