diff --git a/client/src/components/dataset-card/index.tsx b/client/src/components/dataset-card/index.tsx
index 2f005e3..168811a 100644
--- a/client/src/components/dataset-card/index.tsx
+++ b/client/src/components/dataset-card/index.tsx
@@ -14,6 +14,8 @@ import { Switch } from "@/components/ui/switch";
import useMapLayers from "@/hooks/use-map-layers";
import { DatasetLayersDataItem } from "@/types/generated/strapi.schemas";
+import { getDefaultReturnPeriod, getDefaultSelectedLayerId, getReturnPeriods } from "./utils";
+
interface DatasetCardProps {
id: number;
name: string;
@@ -24,22 +26,18 @@ interface DatasetCardProps {
const DatasetCard = ({ id, name, defaultLayerId, layers }: DatasetCardProps) => {
const [layersConfiguration, { addLayer, updateLayer, removeLayer }] = useMapLayers();
- const defaultSelectedLayerId = useMemo(() => {
- // The ids of the layers that belong to the dataset
- const datasetLayerIds = layers.map(({ id }) => id!);
- // The ids of the layers active on the map, probably not from this dataset
- const activeLayerIds = layersConfiguration.map(({ id }) => id!);
- // The id of the layer that belongs to the dataset and is active, if any
- const activeDatasetLayerId = datasetLayerIds.find((id) => activeLayerIds.includes(id));
-
- if (activeDatasetLayerId) {
- return activeDatasetLayerId;
- }
+ const defaultSelectedLayerId = useMemo(
+ () => getDefaultSelectedLayerId(defaultLayerId, layers, layersConfiguration),
+ [layers, defaultLayerId, layersConfiguration],
+ );
- return defaultLayerId;
- }, [layers, defaultLayerId, layersConfiguration]);
+ const defaultSelectedReturnPeriod = useMemo(
+ () => getDefaultReturnPeriod(defaultSelectedLayerId, layers, layersConfiguration),
+ [layers, layersConfiguration, defaultSelectedLayerId],
+ );
const [selectedLayerId, setSelectedLayerId] = useState(defaultSelectedLayerId);
+ const [selectedReturnPeriod, setSelectedReturnPeriod] = useState(defaultSelectedReturnPeriod);
const isDatasetActive = useMemo(() => {
if (selectedLayerId === undefined) {
@@ -49,6 +47,11 @@ const DatasetCard = ({ id, name, defaultLayerId, layers }: DatasetCardProps) =>
return layersConfiguration.findIndex(({ id }) => id === selectedLayerId) !== -1;
}, [selectedLayerId, layersConfiguration]);
+ const layerReturnPeriods = useMemo(
+ () => getReturnPeriods(selectedLayerId, layers),
+ [layers, selectedLayerId],
+ );
+
const onToggleDataset = useCallback(
(active: boolean) => {
if (selectedLayerId === undefined) {
@@ -58,25 +61,48 @@ const DatasetCard = ({ id, name, defaultLayerId, layers }: DatasetCardProps) =>
if (!active) {
removeLayer(selectedLayerId);
} else {
- addLayer(selectedLayerId);
+ addLayer(selectedLayerId, { ["return-period"]: selectedReturnPeriod });
}
},
- [selectedLayerId, addLayer, removeLayer],
+ [selectedLayerId, addLayer, removeLayer, selectedReturnPeriod],
);
const onChangeSelectedLayer = useCallback(
(stringId: string) => {
const id = Number.parseInt(stringId);
const previousId = selectedLayerId;
+ const returnPeriod = getDefaultReturnPeriod(id, layers, layersConfiguration);
setSelectedLayerId(id);
+ setSelectedReturnPeriod(returnPeriod);
+
// If the dataset was active and the layer is changed, we replace the current layer by the new
// one keeping all the same settings (visibility, opacity, etc.)
if (isDatasetActive && previousId !== undefined) {
- updateLayer(previousId, { id });
+ updateLayer(previousId, { id, ["return-period"]: returnPeriod });
+ }
+ },
+ [
+ selectedLayerId,
+ setSelectedLayerId,
+ isDatasetActive,
+ updateLayer,
+ layers,
+ layersConfiguration,
+ ],
+ );
+
+ const onChangeSelectedReturnPeriod = useCallback(
+ (stringReturnPeriod: string) => {
+ const returnPeriod = Number.parseInt(stringReturnPeriod);
+
+ setSelectedReturnPeriod(returnPeriod);
+
+ if (isDatasetActive && selectedLayerId !== undefined) {
+ updateLayer(selectedLayerId, { ["return-period"]: returnPeriod });
}
},
- [selectedLayerId, setSelectedLayerId, isDatasetActive, updateLayer],
+ [selectedLayerId, setSelectedReturnPeriod, isDatasetActive, updateLayer],
);
return (
@@ -89,7 +115,7 @@ const DatasetCard = ({ id, name, defaultLayerId, layers }: DatasetCardProps) =>
-
+
+ {!!layerReturnPeriods && (
+
+ )}
);
diff --git a/client/src/components/dataset-card/utils.ts b/client/src/components/dataset-card/utils.ts
new file mode 100644
index 0000000..2d4e0b3
--- /dev/null
+++ b/client/src/components/dataset-card/utils.ts
@@ -0,0 +1,76 @@
+import useMapLayers from "@/hooks/use-map-layers";
+import { DatasetLayersDataItem } from "@/types/generated/strapi.schemas";
+import { LayerParamsConfig } from "@/types/layer";
+
+export const getDefaultSelectedLayerId = (
+ defaultLayerId: number | undefined,
+ layers: DatasetLayersDataItem[],
+ layersConfiguration: ReturnType[0],
+) => {
+ // The ids of the layers that belong to the dataset
+ const datasetLayerIds = layers.map(({ id }) => id!);
+ // The ids of the layers active on the map, probably not from this dataset
+ const activeLayerIds = layersConfiguration.map(({ id }) => id!);
+ // The id of the layer that belongs to the dataset and is active, if any
+ const activeDatasetLayerId = datasetLayerIds.find((id) => activeLayerIds.includes(id));
+
+ if (activeDatasetLayerId) {
+ return activeDatasetLayerId;
+ }
+
+ return defaultLayerId;
+};
+
+export const getDefaultReturnPeriod = (
+ layerId: number | undefined,
+ layers: DatasetLayersDataItem[],
+ layersConfiguration: ReturnType[0],
+) => {
+ const layerConfiguration = layersConfiguration.find(({ id }) => id === layerId);
+
+ // If the layer is active and already has a selected return period, we return it
+ if (layerConfiguration?.["return-period"] !== undefined) {
+ return layerConfiguration["return-period"];
+ }
+
+ // Else we look for the default return period stored in `params_config`
+ const layer = layers.find(({ id }) => id === layerId);
+ const defaultReturnPeriod = (
+ layer?.attributes!.params_config as LayerParamsConfig | undefined
+ )?.find(({ key }) => key === "return-period");
+
+ if (
+ !defaultReturnPeriod ||
+ defaultReturnPeriod.default === undefined ||
+ defaultReturnPeriod.default === null
+ ) {
+ return undefined;
+ }
+
+ return defaultReturnPeriod.default as number;
+};
+
+export const getReturnPeriods = (layerId: number | undefined, layers: DatasetLayersDataItem[]) => {
+ const layer = layers.find(({ id }) => id === layerId);
+ if (!layer) {
+ return undefined;
+ }
+
+ const returnPeriod = (layer.attributes!.params_config as LayerParamsConfig | undefined)?.find(
+ ({ key }) => key === "return-period",
+ );
+ if (
+ !returnPeriod ||
+ returnPeriod.default === undefined ||
+ returnPeriod.default === null ||
+ returnPeriod.options === undefined ||
+ returnPeriod.options === null
+ ) {
+ return undefined;
+ }
+
+ return {
+ defaultOption: returnPeriod.default as number,
+ options: [...(returnPeriod.options as number[])].sort((a, b) => a - b),
+ };
+};
diff --git a/client/src/components/map/layer-manager/item.tsx b/client/src/components/map/layer-manager/item.tsx
index e89d81a..89daceb 100644
--- a/client/src/components/map/layer-manager/item.tsx
+++ b/client/src/components/map/layer-manager/item.tsx
@@ -17,7 +17,12 @@ const LayerManagerItem = ({ id, beforeId, settings }: LayerManagerItemProps) =>
}
return (
-