Skip to content

Commit

Permalink
[Feat] add setMapControlVisibility action to set mapControl visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
heshan0131 committed Aug 29, 2021
1 parent d8db8f6 commit 79992cb
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 13 deletions.
17 changes: 15 additions & 2 deletions src/actions/ui-state-actions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,23 @@ export type ToggleMapControlUpdaterAction = {
};
};
export function toggleMapControl(
panelId,
index
panelId: string,
index: number
): Merge<ToggleMapControlUpdaterAction, {type: ActionTypes.TOGGLE_MAP_CONTROL}>;


/** SET_MAP_CONTROL_VISIBILITY */
export type setMapControlVisibilityUpdaterAction = {
payload: {
panelId: string;
show: boolean;
};
};
export function setMapControlVisibility(
panelId: string,
show: boolean
): Merge<setMapControlVisibilityUpdaterAction, {type: ActionTypes.SET_MAP_CONTROL_VISIBILITY}>;

/** OPEN_DELETE_MODAL */
export type OpenDeleteModalUpdaterAction = {
payload: string;
Expand Down
15 changes: 15 additions & 0 deletions src/actions/ui-state-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ export const toggleMapControl = createAction(ActionTypes.TOGGLE_MAP_CONTROL, (pa
index
}));

/**
* Toggle active map control panel
* @memberof uiStateActions
* @param panelId - map control panel id, one of the keys of: [`DEFAULT_MAP_CONTROLS`](#default_map_controls)
* @type {typeof import('./ui-state-actions').setMapControlVisibility}
* @public
*/
export const setMapControlVisibility = createAction(
ActionTypes.SET_MAP_CONTROL_VISIBILITY,
(panelId, show) => ({
panelId,
show
})
);

/**
* Toggle active map control panel
* @memberof uiStateActions
Expand Down
6 changes: 5 additions & 1 deletion src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export {default as LayerSelectorPanelFactory} from './map/layer-selector-panel';
export {default as LocalePanelFactory} from './map/locale-panel';
export {default as MapControlPanelFactory} from './map/map-control-panel';
export {default as MapControlTooltipFactory} from './map/map-control-tooltip';
export {default as MapLegendFactory} from './map/map-legend';
export {
default as MapLegendFactory,
LayerLegendHeaderFactory,
LayerLegendContentFactory
} from './map/map-legend';
export {default as MapDrawPanelFactory} from './map/map-draw-panel';
export {default as SplitMapButtonFactory} from './map/split-map-button';
export {default as MapLegendPanelFactory} from './map/map-legend-panel';
Expand Down
1 change: 1 addition & 0 deletions src/constants/action-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type ActionType = {
HIDE_EXPORT_DROPDOWN: string;
OPEN_DELETE_MODAL: string;
TOGGLE_MAP_CONTROL: string;
SET_MAP_CONTROL_VISIBILITY: string;
ADD_NOTIFICATION: string;
REMOVE_NOTIFICATION: string;
SET_LOCALE: string;
Expand Down
1 change: 1 addition & 0 deletions src/constants/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const ActionTypes = {
HIDE_EXPORT_DROPDOWN: `${ACTION_PREFIX}HIDE_EXPORT_DROPDOWN`,
OPEN_DELETE_MODAL: `${ACTION_PREFIX}OPEN_DELETE_MODAL`,
TOGGLE_MAP_CONTROL: `${ACTION_PREFIX}TOGGLE_MAP_CONTROL`,
SET_MAP_CONTROL_VISIBILITY: `${ACTION_PREFIX}SET_MAP_CONTROL_VISIBILITY`,
ADD_NOTIFICATION: `${ACTION_PREFIX}ADD_NOTIFICATION`,
REMOVE_NOTIFICATION: `${ACTION_PREFIX}REMOVE_NOTIFICATION`,
SET_LOCALE: `${ACTION_PREFIX}SET_LOCALE`,
Expand Down
10 changes: 10 additions & 0 deletions src/constants/default-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,13 @@ export const DATASET_FORMATS = keyMirror({
csv: null,
keplergl: null
});

export const MAP_CONTROLS = keyMirror({
visibleLayers: null,
mapLegend: null,
toggle3d: null,
toggleGlobe: null,
splitMap: null,
mapDraw: null,
mapLocale: null
});
3 changes: 2 additions & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export {
DEFAULT_TIME_FORMAT,
SPEED_CONTROL_RANGE,
RESOLUTIONS,
SORT_ORDER
SORT_ORDER,
MAP_CONTROLS
} from './default-settings';

export {BUG_REPORT_LINK, USER_GUIDE_DOC} from './user-guides';
Expand Down
4 changes: 4 additions & 0 deletions src/reducers/ui-state-updaters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export function toggleMapControlUpdater(
state: UiState,
action: UiStateActions.ToggleMapControlUpdaterAction
): UiState;
export function setMapControlVisibilityUpdater(
state: UiState,
action: UiStateActions.setMapControlVisibilityUpdaterAction
): UiState;
export function openDeleteModalUpdater(
state: UiState,
action: UiStateActions.OpenDeleteModalUpdaterAction
Expand Down
39 changes: 30 additions & 9 deletions src/reducers/ui-state-updaters.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
EXPORT_HTML_MAP_MODES,
EXPORT_IMG_RATIOS,
EXPORT_MAP_FORMATS,
RESOLUTIONS
RESOLUTIONS,
MAP_CONTROLS
} from 'constants/default-settings';
import {LOCALE_CODES} from 'localization/locales';
import {createNotification, errorNotification} from 'utils/notifications-utils';
Expand Down Expand Up @@ -99,14 +100,7 @@ const DEFAULT_MAP_CONTROLS_FEATURES = {
* @type {import('./ui-state-updaters').MapControls}
* @public
*/
export const DEFAULT_MAP_CONTROLS = [
'visibleLayers',
'mapLegend',
'toggle3d',
'splitMap',
'mapDraw',
'mapLocale'
].reduce(
export const DEFAULT_MAP_CONTROLS = Object.keys(MAP_CONTROLS).reduce(
(final, current) => ({
...final,
[current]: DEFAULT_MAP_CONTROLS_FEATURES
Expand Down Expand Up @@ -359,6 +353,33 @@ export const toggleMapControlUpdater = (state, {payload: {panelId, index = 0}})
}
});

/**
* Toggle map control visibility
* @memberof uiStateUpdaters
* @param state `uiState`
* @param action action
* @param action.payload map control panel id, one of the keys of: [`DEFAULT_MAP_CONTROLS`](#default_map_controls)
* @returns nextState
* @type {typeof import('./ui-state-updaters').setMapControlVisibilityUpdater}
* @public
*/
export const setMapControlVisibilityUpdater = (state, {payload: {panelId, show}}) => {
if (!state.mapControls?.[panelId]) {
return state;
}

return {
...state,
mapControls: {
...state.mapControls,
[panelId]: {
...state.mapControls[panelId],
show: Boolean(show)
}
}
};
};

/**
* Toggle active map control panel
* @memberof uiStateUpdaters
Expand Down
1 change: 1 addition & 0 deletions src/reducers/ui-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const actionHandler = {
[ActionTypes.HIDE_EXPORT_DROPDOWN]: uiStateUpdaters.hideExportDropdownUpdater,
[ActionTypes.OPEN_DELETE_MODAL]: uiStateUpdaters.openDeleteModalUpdater,
[ActionTypes.TOGGLE_MAP_CONTROL]: uiStateUpdaters.toggleMapControlUpdater,
[ActionTypes.SET_MAP_CONTROL_VISIBILITY]: uiStateUpdaters.setMapControlVisibilityUpdater,
[ActionTypes.ADD_NOTIFICATION]: uiStateUpdaters.addNotificationUpdater,
[ActionTypes.REMOVE_NOTIFICATION]: uiStateUpdaters.removeNotificationUpdater,

Expand Down
31 changes: 31 additions & 0 deletions test/node/reducers/ui-state-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
openDeleteModal,
setExportImageSetting,
toggleMapControl,
setMapControlVisibility,
setExportSelectedDataset,
setExportDataType,
setExportFiltered,
Expand Down Expand Up @@ -181,6 +182,36 @@ test('#uiStateReducer -> TOGGLE_MAP_CONTROL', t => {
t.end();
});

test('#uiStateReducer -> SET_MAP_CONTROL_VISIBILITY', t => {
const newReducer = reducer(INITIAL_UI_STATE, setMapControlVisibility('mapLegend', false));

const expectedMapControl = {
...INITIAL_UI_STATE.mapControls,
mapLegend: {
...INITIAL_UI_STATE.mapControls.mapLegend,
show: false
}
};

t.deepEqual(newReducer.mapControls, expectedMapControl, 'should set map legend show to be false');

const newReducer1 = reducer(newReducer, setMapControlVisibility('mapLegend', true));

const expectedMapControl1 = {
...INITIAL_UI_STATE.mapControls,
mapLegend: {
...INITIAL_UI_STATE.mapControls.mapLegend,
show: true
}
};

t.deepEqual(newReducer1.mapControls, expectedMapControl1, 'should set map legend show to true');

const newReducer2 = reducer(newReducer1, setMapControlVisibility('something', true));
t.equal(newReducer1, newReducer2, 'should note update state');
t.end();
});

test('#uiStateReducer -> SET_EXPORT_SELECTED_DATASET', t => {
const newReducer = reducer(INITIAL_UI_STATE, setExportSelectedDataset('a'));

Expand Down

0 comments on commit 79992cb

Please sign in to comment.