Skip to content

Commit

Permalink
Merge pull request #696 from Vizzuality/radio-selection-update
Browse files Browse the repository at this point in the history
Radio-selection-update
  • Loading branch information
cmanciero authored Aug 29, 2024
2 parents 2605319 + 5191032 commit 09c1fea
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,6 @@ function AnalyzeAreasContainer(props) {
layerId: newSelectedOption,
category: newLayerCategory,
});
} else {
layersToToggle = activeLayers.map((l) => ({
layerId: l.title,
}));
}

if (formerSelectedSlug === CLEAR_SELECTIONS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TERRESTRIAL_GLOBAL,
TERRESTRIAL_REGIONAL,
MARINE,
// DEFAULT_RESOLUTIONS,
DEFAULT_RESOLUTIONS,
} from 'constants/biodiversity-layers-constants';
import { LAYERS_CATEGORIES } from 'constants/mol-layers-configs';

Expand All @@ -15,6 +15,7 @@ export const useSelectLayersOnTabOrResolutionChange = ({
previousSelectedResolutions,
layersToggleConfig,
category,
selectedCategory,
isChecked,
allActiveLayerTitles,
activeLayers,
Expand Down Expand Up @@ -76,10 +77,10 @@ export const useSelectLayersOnTabOrResolutionChange = ({
.filter((l) => l.category === LAYERS_CATEGORIES.BIODIVERSITY)
.map((l) => l.title);
const resolution = selectedResolutions[currentResolutionGroup];
// const defaultResolutionLayers =
// layersToggleConfig[biodiversityLayerVariant][currentResolutionGroup][
// DEFAULT_RESOLUTIONS[currentResolutionGroup]
// ];
const defaultResolutionLayers =
layersToggleConfig[biodiversityLayerVariant][currentResolutionGroup][
DEFAULT_RESOLUTIONS[currentResolutionGroup]
];
const availableLayers =
layersToggleConfig[biodiversityLayerVariant][currentResolutionGroup][
resolution
Expand All @@ -95,17 +96,18 @@ export const useSelectLayersOnTabOrResolutionChange = ({
availableLayers &&
availableLayers.find((layer) => layer.value.includes(layerTaxa));

if (hasMatchingLayer) {
if (hasMatchingLayer && selectedCategory === category) {
// select matching layer on selected variant
handleLayerToggle(hasMatchingLayer);
} else if (availableLayers && layerTaxa !== 'all') {
// select first element if there's no matching layer
handleLayerToggle(availableLayers[0]);
} else if(hasMatchingLayer && category === TERRESTRIAL_GLOBAL){
handleLayerToggle(hasMatchingLayer);
} else if(!hasMatchingLayer && category === TERRESTRIAL_GLOBAL){
if(availableLayers){
handleLayerToggle(availableLayers[0]);
} else {
handleLayerToggle(defaultResolutionLayers[0]);
}
}
// else {
// // select first element if there's no maching resolution
// handleLayerToggle(defaultResolutionLayers[0]);
// }
}, [
biodiversityLayerVariant,
selectedResolutions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ const getGroupedLayerOptions = createSelector(
const getSelectedLayerOption = createSelector(
[getCategoryLayerOptions, getSelectedLayer],
(options, selectedLayer) => {
const allTaxaIndex = options.findIndex(item => item.name === 'all taxa');
const optionsSelectedLayer =
options.find((l) => l.value === selectedLayer) || options[0];
options.find((l) => l.value === selectedLayer) || options[allTaxaIndex] || options[0];
if (!optionsSelectedLayer) return null;

// label is needed for grouped layer option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ function BiodiversityLayerToggle(props) {
setSelectedLayer,
allActiveLayerTitles,
category,
setSelectedCategory,
selectedCategory
} = props;
const locale = useLocale();
const layersToggleConfig = useMemo(() => getLayersToggleConfig(), [locale]);
Expand All @@ -60,6 +62,7 @@ function BiodiversityLayerToggle(props) {
const previousSelectedResolutions = usePrevious(selectedResolutions);

const handleLayerToggle = (option) => {
setSelectedCategory(category);
const layer = layersConfig[option.layer];
if (!allActiveLayerTitles) {
// Add layer to empty selection
Expand Down Expand Up @@ -113,6 +116,7 @@ function BiodiversityLayerToggle(props) {
isChecked,
allActiveLayerTitles,
activeLayers,
selectedCategory,
handleLayerToggle,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function BiodiversitySidebarCardComponent({
const firstStep = onboardingStep === 0;
const { title, description, source } = cardMetadata || {};
const [isOpen, setOpen] = useState(false);
const [selectedCategory, setSelectedCategory] = useState(TERRESTRIAL_GLOBAL);
const handleBoxClick = () => setOpen(!isOpen);

// --- Onboarding
Expand Down Expand Up @@ -115,6 +116,8 @@ function BiodiversitySidebarCardComponent({
activeLayers={activeLayers}
selectedLayer={selectedLayer}
setSelectedLayer={setSelectedLayer}
selectedCategory={selectedCategory}
setSelectedCategory={setSelectedCategory}
selectedResolutions={selectedResolutions}
resolutionOptions={categoryResolutionOptions}
selectedResolutionOption={selectedResolutionOptions[category]}
Expand Down

0 comments on commit 09c1fea

Please sign in to comment.