Skip to content
Snippets Groups Projects
Commit 79664147 authored by mateusz-winiarczyk's avatar mateusz-winiarczyk
Browse files

feat(plugins): legend tabs (MIN-231)

parent 119c70c2
No related branches found
No related tags found
2 merge requests!223reset the pin numbers before search results are fetch (so the results will be...,!153feat(plugins): legend tabs (MIN-231)
import { PayloadAction } from '@reduxjs/toolkit'; import { PayloadAction } from '@reduxjs/toolkit';
import { LegendState, PluginId } from './legend.types'; import { LegendState, PluginId } from './legend.types';
import { DEFAULT_LEGEND_ID } from './legend.constants';
export const openLegendReducer = (state: LegendState): void => { export const openLegendReducer = (state: LegendState): void => {
state.isOpen = true; state.isOpen = true;
...@@ -9,13 +10,37 @@ export const closeLegendReducer = (state: LegendState): void => { ...@@ -9,13 +10,37 @@ export const closeLegendReducer = (state: LegendState): void => {
state.isOpen = false; state.isOpen = false;
}; };
export const selectLegendPluginIdReducer = ( export const setActiveLegendIdReducer = (
state: LegendState, state: LegendState,
action: PayloadAction<PluginId>, action: PayloadAction<string>,
): void => { ): void => {
state.selectedPluginId = action.payload; state.activeLegendId = action.payload;
}; };
export const selectDefaultLegendReducer = (state: LegendState): void => { export const setDefaultLegendIdReducer = (state: LegendState): void => {
state.selectedPluginId = undefined; state.activeLegendId = DEFAULT_LEGEND_ID;
};
export const setPluginLegendReducer = (
state: LegendState,
{
payload,
}: PayloadAction<{
pluginId: PluginId;
legend: string[];
}>,
): void => {
state.pluginLegend = {
...state.pluginLegend,
[payload.pluginId]: payload.legend,
};
};
export const removePluginLegendReducer = (
state: LegendState,
{ payload }: PayloadAction<string>,
): void => {
if (state.pluginLegend[payload]) {
delete state.pluginLegend[payload];
}
}; };
import { createSelector } from '@reduxjs/toolkit'; import { createSelector } from '@reduxjs/toolkit';
import { BASE_MAP_IMAGES_URL } from '@/constants';
import { defaultLegendImagesSelector } from '../configuration/configuration.selectors'; import { defaultLegendImagesSelector } from '../configuration/configuration.selectors';
import { rootSelector } from '../root/root.selectors'; import { rootSelector } from '../root/root.selectors';
import { activePluginsDataSelector } from '../plugins/plugins.selectors';
import { DEFAULT_LEGEND_ID, DEFAULT_LEGEND_TAB } from './legend.constants';
export const legendSelector = createSelector(rootSelector, state => state.legend); export const legendSelector = createSelector(rootSelector, state => state.legend);
export const isLegendOpenSelector = createSelector(legendSelector, state => state.isOpen); export const isLegendOpenSelector = createSelector(legendSelector, state => state.isOpen);
// TODO: add filter for active plugins export const pluginLegendsSelector = createSelector(legendSelector, state => state.pluginLegend);
export const currentLegendImagesSelector = createSelector( export const currentLegendImagesSelector = createSelector(
legendSelector, legendSelector,
defaultLegendImagesSelector, defaultLegendImagesSelector,
({ selectedPluginId, pluginLegend }, defaultImages) => { ({ activeLegendId, pluginLegend }, defaultImages) => {
if (selectedPluginId) { if (activeLegendId === DEFAULT_LEGEND_ID)
return pluginLegend?.[selectedPluginId] || []; return defaultImages.map(image => `${BASE_MAP_IMAGES_URL}/minerva/${image}`);
if (activeLegendId) {
return pluginLegend?.[activeLegendId] || [];
} }
return defaultImages; return [];
},
);
export const allLegendsNamesAndIdsSelector = createSelector(
activePluginsDataSelector,
pluginLegendsSelector,
(activePlugins, pluginLegends) => {
const allPluginLegendsNamesAndIds = Object.keys(pluginLegends).map(hash => {
const plugin = Object.values(activePlugins).find(activePlugin => activePlugin.hash === hash);
return {
name: plugin?.name || '',
id: plugin?.hash || '',
};
});
return [DEFAULT_LEGEND_TAB, ...allPluginLegendsNamesAndIds];
}, },
); );
export const activeLegendIdSelector = createSelector(legendSelector, state => state.activeLegendId);
export const isActiveLegendSelector = createSelector(
[activeLegendIdSelector, (_, legendId: string): string => legendId],
(activeLegendId, legendId) => activeLegendId === legendId,
);
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from '@reduxjs/toolkit';
import { LEGEND_INITIAL_STATE } from './legend.constants'; import { LEGEND_INITIAL_STATE } from './legend.constants';
import { closeLegendReducer, openLegendReducer } from './legend.reducers'; import {
closeLegendReducer,
openLegendReducer,
removePluginLegendReducer,
setActiveLegendIdReducer,
setDefaultLegendIdReducer,
setPluginLegendReducer,
} from './legend.reducers';
const legendSlice = createSlice({ const legendSlice = createSlice({
name: 'legend', name: 'legend',
...@@ -8,9 +15,20 @@ const legendSlice = createSlice({ ...@@ -8,9 +15,20 @@ const legendSlice = createSlice({
reducers: { reducers: {
openLegend: openLegendReducer, openLegend: openLegendReducer,
closeLegend: closeLegendReducer, closeLegend: closeLegendReducer,
setPluginLegend: setPluginLegendReducer,
setActiveLegendId: setActiveLegendIdReducer,
setDefaultLegendId: setDefaultLegendIdReducer,
removePluginLegend: removePluginLegendReducer,
}, },
}); });
export const { openLegend, closeLegend } = legendSlice.actions; export const {
openLegend,
closeLegend,
setPluginLegend,
setActiveLegendId,
setDefaultLegendId,
removePluginLegend,
} = legendSlice.actions;
export default legendSlice.reducer; export default legendSlice.reducer;
export type PluginId = number; export type PluginId = string;
export type ImageUrl = string; export type ImageUrl = string;
export type LegendState = { export type LegendState = {
isOpen: boolean; isOpen: boolean;
pluginLegend: Record<PluginId, ImageUrl[]>; pluginLegend: Record<PluginId, ImageUrl[]>;
selectedPluginId: PluginId | undefined; activeLegendId: string;
}; };
import { isActiveLegendSelector } from '@/redux/legend/legend.selectors';
import { removePluginLegend, setDefaultLegendId } from '@/redux/legend/legend.slice';
import { store } from '@/redux/store';
export const removeLegend = (pluginId: string): void => {
const isActivePluginLegend = isActiveLegendSelector(store.getState(), pluginId);
const { dispatch } = store;
if (isActivePluginLegend) {
dispatch(setDefaultLegendId());
}
dispatch(removePluginLegend(pluginId));
};
import { setPluginLegend } from '@/redux/legend/legend.slice';
import { store } from '@/redux/store';
export const setLegend = (pluginId: string, legend: string[]): void => {
const { dispatch } = store;
dispatch(
setPluginLegend({
pluginId,
legend,
}),
);
};
...@@ -33,6 +33,8 @@ import { getVersion } from './project/data/getVersion'; ...@@ -33,6 +33,8 @@ import { getVersion } from './project/data/getVersion';
import { getBounds } from './map/data/getBounds'; import { getBounds } from './map/data/getBounds';
import { fitBounds } from './map/fitBounds'; import { fitBounds } from './map/fitBounds';
import { getOpenMapId } from './map/getOpenMapId'; import { getOpenMapId } from './map/getOpenMapId';
import { setLegend } from './legend/setLegend';
import { removeLegend } from './legend/removeLegend';
export const PluginsManager: PluginsManagerType = { export const PluginsManager: PluginsManagerType = {
hashedPlugins: {}, hashedPlugins: {},
...@@ -132,6 +134,10 @@ export const PluginsManager: PluginsManagerType = { ...@@ -132,6 +134,10 @@ export const PluginsManager: PluginsManagerType = {
removeListener: PluginsEventBus.removeListener.bind(this, hash), removeListener: PluginsEventBus.removeListener.bind(this, hash),
removeAllListeners: PluginsEventBus.removeAllListeners.bind(this, hash), removeAllListeners: PluginsEventBus.removeAllListeners.bind(this, hash),
}, },
legend: {
setLegend: setLegend.bind(this, hash),
removeLegend: removeLegend.bind(this, hash),
},
}; };
}, },
createAndGetPluginContent({ hash }) { createAndGetPluginContent({ hash }) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment