Skip to content

Commit

Permalink
fix(client): Stop layer animation if dataset becomes inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
clementprdhomme committed Nov 22, 2024
1 parent b29ae6e commit c68d225
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions client/src/components/dataset-card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ const DatasetCard = ({ id, name, defaultLayerId, layers, metadata }: DatasetCard
[layers, selectedLayerId],
);

const onToggleAnimation = useCallback(() => {
const newIsAnimated = !isAnimated;

if (newIsAnimated) {
dateBeforeAnimationRef.current = selectedDate !== undefined ? selectedDate : null;
} else {
dateBeforeAnimationRef.current = null;
}

setIsAnimated(newIsAnimated);
}, [selectedDate, isAnimated, setIsAnimated]);

const onToggleDataset = useCallback(
(active: boolean) => {
if (selectedLayerId === undefined) {
Expand All @@ -109,11 +121,22 @@ const DatasetCard = ({ id, name, defaultLayerId, layers, metadata }: DatasetCard

if (!active) {
removeLayer(selectedLayerId);
if (isAnimated) {
onToggleAnimation();
}
} else {
addLayer(selectedLayerId, { ["return-period"]: selectedReturnPeriod, date: selectedDate });
}
},
[selectedLayerId, addLayer, removeLayer, selectedReturnPeriod, selectedDate],
[
selectedLayerId,
addLayer,
removeLayer,
selectedReturnPeriod,
selectedDate,
isAnimated,
onToggleAnimation,
],
);

const onChangeSelectedLayer = useCallback(
Expand Down Expand Up @@ -184,18 +207,6 @@ const DatasetCard = ({ id, name, defaultLayerId, layers, metadata }: DatasetCard
[selectedLayerId, isDatasetActive, addLayer, updateLayer, layers, layersConfiguration],
);

const onToggleAnimation = useCallback(() => {
const newIsAnimated = !isAnimated;

if (newIsAnimated) {
dateBeforeAnimationRef.current = selectedDate !== undefined ? selectedDate : null;
} else {
dateBeforeAnimationRef.current = null;
}

setIsAnimated(newIsAnimated);
}, [selectedDate, isAnimated, setIsAnimated]);

// When the layer is animated, show each month of the year in a loop
useEffect(() => {
if (isAnimated && selectedDate !== undefined && selectedLayerId !== undefined) {
Expand Down

0 comments on commit c68d225

Please sign in to comment.