Skip to content

Commit

Permalink
Adding expected data lag days option
Browse files Browse the repository at this point in the history
  • Loading branch information
gislawill committed Jul 1, 2024
1 parent df5a171 commit 0c08e8c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ cd frontend
yarn clean
yarn install
yarn setup:common
REACT_APP_COUNTRY=jordan REACT_APP_USE_LOCAL_HIP_SERVICE=http://localhost:3001/q_multi_geojson docker compose up
REACT_APP_COUNTRY=cambodia yarn start
```
### Available Scripts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ const paintProps: (
const CompositeLayer = ({ layer, before }: Props) => {
// look to refacto with impactLayer and maybe other layers
const [adminBoundaryLimitPolygon, setAdminBoundaryPolygon] = useState(null);
const selectedDate = useDefaultDate(layer.dateLayer);
const selectedDate = useDefaultDate(
layer.dateLayer,
layer.expectedDataLagDays,
);
console.log('selectedDate', selectedDate);

Check warning on line 50 in frontend/src/components/MapView/Layers/CompositeLayer/index.tsx

View workflow job for this annotation

GitHub Actions / frontend_tests (ubuntu-latest)

Unexpected console statement

Check warning on line 50 in frontend/src/components/MapView/Layers/CompositeLayer/index.tsx

View workflow job for this annotation

GitHub Actions / frontend_tests (windows-latest)

Unexpected console statement
const serverAvailableDates = useSelector(availableDatesSelector);
const opacityState = useSelector(opacitySelector(layer.id));
const dispatch = useDispatch();

const layerAvailableDates = serverAvailableDates[layer.dateLayer];
const queryDate = getRequestDate(layerAvailableDates, selectedDate);

Expand Down
1 change: 1 addition & 0 deletions frontend/src/config/jordan/layers.json
Original file line number Diff line number Diff line change
Expand Up @@ -4197,6 +4197,7 @@
"aggregation": "pixel",
"date_layer": "spi_1m",
"start_date": "2020-12-01",
"expected_data_lag_days": 55,
"opacity": 0.7
},
"daily_rainfall_forecast": {
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ export class CompositeLayerProps extends CommonLayerProps {

@optional
endDate?: string;

@optional
expectedDataLagDays?: number;
}

export class StaticRasterLayerProps extends CommonLayerProps {
Expand Down
22 changes: 18 additions & 4 deletions frontend/src/utils/useDefaultDate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useDispatch, useSelector } from 'react-redux';
import { useEffect } from 'react';
import { useEffect, useMemo } from 'react';
import { isMainLayer, LayerKey } from 'config/types';
import { availableDatesSelector } from 'context/serverStateSlice';
import {
Expand All @@ -15,7 +15,10 @@ import { getFormattedDate } from './date-utils';
* Returns either the user selected date or the default date, dispatching it to the date picker beforehand. Can also return undefined if no default date is available.
* @param availableDatesLookupKey key to lookup in AvailableDates
*/
export function useDefaultDate(layerId: LayerKey): number | undefined {
export function useDefaultDate(
layerId: LayerKey,
expectedDataLagDays?: number,
): number | undefined {
const dispatch = useDispatch();
const selectedLayers = useSelector(layersSelector);
// check layer without group or main layer in group
Expand All @@ -25,10 +28,21 @@ export function useDefaultDate(layerId: LayerKey): number | undefined {
const { updateHistory } = useUrlHistory();

// TODO - use getPossibleDatesForLayer
// Update on 2024-07-01: getPossibleDatesForLayer would give us the first possible layer date, we want the _last_ possible date.
const possibleDates = useSelector(availableDatesSelector)[layerId];

const defaultDate: number | undefined =
possibleDates?.[possibleDates?.length - 1]?.displayDate;
const soonestAvailableDate =
new Date().getTime() - (expectedDataLagDays ?? 0) * 24 * 60 * 60 * 1000;

const defaultDate = useMemo(() => {
let index = possibleDates?.length - 1;
let defaultDate = possibleDates?.[index]?.displayDate;

Check warning on line 39 in frontend/src/utils/useDefaultDate.ts

View workflow job for this annotation

GitHub Actions / frontend_tests (ubuntu-latest)

'defaultDate' is already declared in the upper scope

Check warning on line 39 in frontend/src/utils/useDefaultDate.ts

View workflow job for this annotation

GitHub Actions / frontend_tests (windows-latest)

'defaultDate' is already declared in the upper scope
while (defaultDate && defaultDate > soonestAvailableDate) {
index -= 1;

Check warning on line 41 in frontend/src/utils/useDefaultDate.ts

View workflow job for this annotation

GitHub Actions / frontend_tests (ubuntu-latest)

Unallowed reassignment

Check warning on line 41 in frontend/src/utils/useDefaultDate.ts

View workflow job for this annotation

GitHub Actions / frontend_tests (windows-latest)

Unallowed reassignment
defaultDate = possibleDates?.[index]?.displayDate;

Check warning on line 42 in frontend/src/utils/useDefaultDate.ts

View workflow job for this annotation

GitHub Actions / frontend_tests (ubuntu-latest)

Unallowed reassignment

Check warning on line 42 in frontend/src/utils/useDefaultDate.ts

View workflow job for this annotation

GitHub Actions / frontend_tests (windows-latest)

Unallowed reassignment
}
return defaultDate;
}, [possibleDates, soonestAvailableDate]);

// React doesn't allow updating other components within another component
// useEffect removes this error and updates DateSelector correctly in the lifecycle.
Expand Down

0 comments on commit 0c08e8c

Please sign in to comment.