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

[Maps] data load event handlers #49373

Merged
merged 6 commits into from
Oct 31, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 45 additions & 5 deletions x-pack/legacy/plugins/maps/public/actions/map_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ import { FLYOUT_STATE } from '../reducers/ui';
import {
cancelRequest,
registerCancelCallback,
unregisterCancelCallback
unregisterCancelCallback,
getEventHandlers,
} from '../reducers/non_serializable_instances';
import { updateFlyout } from '../actions/ui_actions';
import { FEATURE_ID_PROPERTY_NAME, SOURCE_DATA_ID_ORIGIN } from '../../common/constants';
import {
FEATURE_ID_PROPERTY_NAME,
LAYER_TYPE,
SOURCE_DATA_ID_ORIGIN
} from '../../common/constants';

export const SET_SELECTED_LAYER = 'SET_SELECTED_LAYER';
export const SET_TRANSIENT_LAYER = 'SET_TRANSIENT_LAYER';
Expand Down Expand Up @@ -456,6 +461,14 @@ export function startDataLoad(layerId, dataId, requestToken, meta = {}) {
dispatch(cancelRequest(layer.getPrevRequestToken(dataId)));
}

const eventHandlers = getEventHandlers(getState());
if (eventHandlers && eventHandlers.onDataLoad) {
eventHandlers.onDataLoad({
layerId,
dataId,
});
}

dispatch({
meta,
type: LAYER_DATA_LOAD_STARTED,
Expand All @@ -480,9 +493,26 @@ export function updateSourceDataRequest(layerId, newData) {
}

export function endDataLoad(layerId, dataId, requestToken, data, meta) {
return async (dispatch) => {
return async (dispatch, getState) => {
dispatch(unregisterCancelCallback(requestToken));
const features = data ? data.features : [];

const features = data && data.features ? data.features : [];

const eventHandlers = getEventHandlers(getState());
if (eventHandlers && eventHandlers.onDataLoadEnd) {
const layer = getLayerById(layerId, getState());
const resultMeta = {};
if (layer && layer.getType() === LAYER_TYPE.VECTOR) {
resultMeta.featuresCount = features.length;
}

eventHandlers.onDataLoadEnd({
layerId,
dataId,
resultMeta
});
}

dispatch(cleanTooltipStateForLayer(layerId, features));
dispatch({
type: LAYER_DATA_LOAD_ENDED,
Expand All @@ -503,8 +533,18 @@ export function endDataLoad(layerId, dataId, requestToken, data, meta) {
}

export function onDataLoadError(layerId, dataId, requestToken, errorMessage) {
return async (dispatch) => {
return async (dispatch, getState) => {
dispatch(unregisterCancelCallback(requestToken));

const eventHandlers = getEventHandlers(getState());
if (eventHandlers && eventHandlers.onDataLoadError) {
eventHandlers.onDataLoadError({
layerId,
dataId,
errorMessage,
});
}

dispatch(cleanTooltipStateForLayer(layerId));
dispatch({
type: LAYER_DATA_LOAD_ERROR,
Expand Down
18 changes: 18 additions & 0 deletions x-pack/legacy/plugins/maps/public/embeddable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ const renderTooltipContent = ({ addFilters, closeTooltip, features, isLocked, lo

const mapEmbeddable = await factory.createFromState(state, input, parent, renderTooltipContent);
```


#### Event handlers
```
const eventHandlers = {
onDataLoad: (layerId: string, dataId: string) => {
// take action on data load
},
onDataLoadEnd: (layerId: string, dataId: string, resultMeta: object) => {
// take action on data load end
},
onDataLoadError: (layerId: string, dataId: string, errorMessage: string) => {
// take action on data load error
},
}

const mapEmbeddable = await factory.createFromState(state, input, parent, renderTooltipContent, eventHandlers);
```
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import {
setOpenTOCDetails,
} from '../actions/ui_actions';
import { getIsLayerTOCOpen, getOpenTOCDetails } from '../selectors/ui_selectors';
import { getInspectorAdapters } from '../reducers/non_serializable_instances';
import { getInspectorAdapters, setEventHandlers } from '../reducers/non_serializable_instances';
import { getMapCenter, getMapZoom } from '../selectors/map_selectors';
import { MAP_SAVED_OBJECT_TYPE } from '../../common/constants';

export class MapEmbeddable extends Embeddable {
type = MAP_SAVED_OBJECT_TYPE;

constructor(config, initialInput, parent, renderTooltipContent) {
constructor(config, initialInput, parent, renderTooltipContent, eventHandlers) {
super(
initialInput,
{
Expand All @@ -50,6 +50,7 @@ export class MapEmbeddable extends Embeddable {
parent);

this._renderTooltipContent = renderTooltipContent;
this._eventHandlers = eventHandlers;
this._layerList = config.layerList;
this._store = createMapStore();

Expand Down Expand Up @@ -98,6 +99,7 @@ export class MapEmbeddable extends Embeddable {
* @param {ContainerState} containerState
*/
render(domNode) {
this._store.dispatch(setEventHandlers(this._eventHandlers));
this._store.dispatch(setReadOnly(true));
this._store.dispatch(disableScrollZoom());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class MapEmbeddableFactory extends EmbeddableFactory {
input,
parent,
renderTooltipContent,
eventHandlers,
) {
const layerList = state && state.layerList ? state.layerList : getInitialLayers();
const indexPatterns = await this._getIndexPatterns(layerList);
Expand All @@ -137,7 +138,8 @@ export class MapEmbeddableFactory extends EmbeddableFactory {
},
input,
parent,
renderTooltipContent
renderTooltipContent,
eventHandlers,
);
}

Expand Down
4 changes: 4 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,9 @@ export class AbstractLayer {
mbMap.setLayoutProperty(mbLayerId, 'visibility', this.isVisible() ? 'visible' : 'none');
}

getType() {
return this._descriptor.type;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MapAdapter } from '../inspector/adapters/map_adapter';

const REGISTER_CANCEL_CALLBACK = 'REGISTER_CANCEL_CALLBACK';
const UNREGISTER_CANCEL_CALLBACK = 'UNREGISTER_CANCEL_CALLBACK';
const SET_EVENT_HANDLERS = 'SET_EVENT_HANDLERS';

function createInspectorAdapters() {
const inspectorAdapters = {
Expand All @@ -27,6 +28,7 @@ export function nonSerializableInstances(state, action = {}) {
return {
inspectorAdapters: createInspectorAdapters(),
cancelRequestCallbacks: new Map(), // key is request token, value is cancel callback
eventHandlers: {},
};
}

Expand All @@ -41,6 +43,12 @@ export function nonSerializableInstances(state, action = {}) {
return {
...state,
};
case SET_EVENT_HANDLERS: {
return {
...state,
eventHandlers: action.eventHandlers
};
}
default:
return state;

Expand All @@ -57,6 +65,10 @@ export const getCancelRequestCallbacks = ({ nonSerializableInstances }) => {
return nonSerializableInstances.cancelRequestCallbacks;
};

export const getEventHandlers = ({ nonSerializableInstances }) => {
return nonSerializableInstances.eventHandlers;
};

// Actions
export const registerCancelCallback = (requestToken, callback) => {
return {
Expand Down Expand Up @@ -86,3 +98,10 @@ export const cancelRequest = (requestToken) => {
}
};
};

export const setEventHandlers = (eventHandlers = {}) => {
return {
type: SET_EVENT_HANDLERS,
eventHandlers,
};
};