Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Edit a custom base map style redux #2281

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/actions/src/action-types.ts
Expand Up @@ -134,6 +134,7 @@ export const ActionTypes = {
INPUT_MAP_STYLE: `${ACTION_PREFIX}INPUT_MAP_STYLE`,
LOAD_CUSTOM_MAP_STYLE: `${ACTION_PREFIX}LOAD_CUSTOM_MAP_STYLE`,
ADD_CUSTOM_MAP_STYLE: `${ACTION_PREFIX}ADD_CUSTOM_MAP_STYLE`,
EDIT_CUSTOM_MAP_STYLE: `${ACTION_PREFIX}EDIT_CUSTOM_MAP_STYLE`,
REMOVE_CUSTOM_MAP_STYLE: `${ACTION_PREFIX}REMOVE_CUSTOM_MAP_STYLE`,
REQUEST_MAP_STYLES: `${ACTION_PREFIX}REQUEST_MAP_STYLES`,
SET_3D_BUILDING_COLOR: `${ACTION_PREFIX}SET_3D_BUILDING_COLOR`,
Expand Down
12 changes: 12 additions & 0 deletions src/actions/src/map-style-actions.ts
Expand Up @@ -48,6 +48,18 @@ export type RemoveCustomMapStyleUpdaterAction = {
};
};

/**
* Edit map style from user input to reducer.
* This action is called when user clicks confirm after editing an existing custom style in the custom map style dialog.
* It should not be called from outside kepler.gl without a valid `inputStyle` in the `mapStyle` reducer.
* param {void}
* @memberof mapStyleActions
* @public
*/
export const editCustomMapStyle: () => {
type: typeof ActionTypes.EDIT_CUSTOM_MAP_STYLE;
} = createAction(ActionTypes.EDIT_CUSTOM_MAP_STYLE);

/**
* Remove a custom map style from `state.mapStyle.mapStyles`.
* @param id
Expand Down
25 changes: 21 additions & 4 deletions src/reducers/src/map-style-updaters.ts
Expand Up @@ -749,7 +749,26 @@ export const addCustomMapStyleUpdater = (state: MapStyle): MapStyle => {
const styleId = state.inputStyle.id;
if (!styleId) return state;

const newState: MapStyle = {
const newState = getNewStateWithCustomMapStyle(state);
// set new style
return mapStyleChangeUpdater(newState, {payload: {styleType: styleId}});
};

/**
* Edit map style from user input to reducer.
* This action is called when user clicks confirm after editing an existing custom style in the custom map style dialog.
* It should not be called from outside kepler.gl without a valid `inputStyle` in the `mapStyle` reducer.
* @memberof mapStyleUpdaters
*/
export const editCustomMapStyleUpdater = (state: MapStyle): MapStyle => {
return getNewStateWithCustomMapStyle(state);
};

function getNewStateWithCustomMapStyle(state: MapStyle): MapStyle {
const styleId = state.inputStyle.id;
if (!styleId) return state;

return {
...state,
// @ts-expect-error Property 'layerGroups' is missing in type 'InputStyle' but required in type 'BaseMapStyle'. Legacy case?
mapStyles: {
Expand All @@ -759,9 +778,7 @@ export const addCustomMapStyleUpdater = (state: MapStyle): MapStyle => {
// set to default
inputStyle: getInitialInputStyle()
};
// set new style
return mapStyleChangeUpdater(newState, {payload: {styleType: styleId}});
};
}

/**
* Remove a custom map style from `state.mapStyle.mapStyles`.
Expand Down
1 change: 1 addition & 0 deletions src/reducers/src/map-style.ts
Expand Up @@ -37,6 +37,7 @@ const actionHandler = {
[ActionTypes.RECEIVE_MAP_CONFIG]: mapStyleUpdaters.receiveMapConfigUpdater,
[ActionTypes.LOAD_CUSTOM_MAP_STYLE]: mapStyleUpdaters.loadCustomMapStyleUpdater,
[ActionTypes.ADD_CUSTOM_MAP_STYLE]: mapStyleUpdaters.addCustomMapStyleUpdater,
[ActionTypes.EDIT_CUSTOM_MAP_STYLE]: mapStyleUpdaters.editCustomMapStyleUpdater,
[ActionTypes.REMOVE_CUSTOM_MAP_STYLE]: mapStyleUpdaters.removeCustomMapStyleUpdater,
[ActionTypes.RESET_MAP_CONFIG]: mapStyleUpdaters.resetMapConfigMapStyleUpdater,
[ActionTypes.SET_3D_BUILDING_COLOR]: mapStyleUpdaters.set3dBuildingColorUpdater,
Expand Down
58 changes: 58 additions & 0 deletions test/node/reducers/map-style-test.js
Expand Up @@ -33,6 +33,8 @@ import {
loadMapStyles,
mapConfigChange,
mapStyleChange,
inputMapStyle,
editCustomMapStyle,
removeCustomMapStyle
} from '@kepler.gl/actions';
import SchemaManager from '@kepler.gl/schemas';
Expand Down Expand Up @@ -831,6 +833,62 @@ test('#mapStyleReducer -> MAP_STYLE_CHANGE -> dark basemap to no basemap', t =>
t.end();
});

test('#mapStyleReducer -> EDIT_CUSTOM_MAP_STYLE', t => {
drainTasksForTesting();
const stateWithToken = reducer(
InitialMapStyle,
keplerGlInit({
mapboxApiAccessToken: 'smoothies_secret_token'
})
);

// state with custom style
let nextState = reducer(stateWithToken, receiveMapConfig(StateWCustomMapStyleLocal));

const styleIdToEdit = 'smoothie_the_cat';
const {label: originalLabel, url, accessToken, icon, custom} = nextState.mapStyles[styleIdToEdit];
const inputMapStyleToEdit = {
id: styleIdToEdit,
label: 'Smoothie the Super Cool Cat',
url,
accessToken,
icon,
custom
};

// set input custom map style and provide an edited label property
nextState = reducer(nextState, inputMapStyle(inputMapStyleToEdit));

t.deepEqual(
nextState.inputStyle,
{...inputMapStyleToEdit, error: false, isValid: true, style: null, uploadedFile: null},
'should set the inputStyle'
);

// edit custom map style
nextState = reducer(nextState, editCustomMapStyle());

t.notEqual(
nextState.mapStyles[styleIdToEdit].label,
originalLabel,
'should commit edit to the label property for custom style with id "smoothie_the_cat"'
);

t.equal(
nextState.mapStyles[styleIdToEdit].id,
styleIdToEdit,
'should not make any changes to the id property of the custom style after committing label property edits'
);

t.deepEqual(
nextState.inputStyle,
getInitialInputStyle(),
'should reset the inputStyle after committing edits'
);

t.end();
});

test('#mapStyleReducer -> REMOVE_CUSTOM_MAP_STYLE -> removal of currently selected custom style', t => {
drainTasksForTesting();
const stateWithToken = reducer(
Expand Down