Skip to content

Commit

Permalink
Only change handlers as the element changes (#56782) (#56821)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
Corey Robertson and elasticmachine authored Feb 5, 2020
1 parent 1a47927 commit f0194db
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ function selectorFactory(dispatch) {
export const ElementWrapper = compose(
connectAdvanced(selectorFactory),
withPropsOnChange(
(props, nextProps) =>
!isEqual(props.element, nextProps.element) ||
!isEqual(props.selectedPage, nextProps.selectedPage),
(props, nextProps) => !isEqual(props.element, nextProps.element),
props => {
const { element, createHandlers } = props;
const handlers = createHandlers(element, props.selectedPage);
const handlers = createHandlers(element);
// this removes element and createHandlers from passed props
return { handlers };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const createHandlers = dispatch => {
let oldElement;
let completeFn = () => {};

return (element, pageId) => {
return element => {
// reset isComplete when element changes
if (!isEqual(oldElement, element)) {
isComplete = false;
Expand All @@ -25,7 +25,7 @@ export const createHandlers = dispatch => {

return {
setFilter(text) {
dispatch(setFilter(text, element.id, pageId, true));
dispatch(setFilter(text, element.id, true));
},

getFilter() {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/canvas/public/state/actions/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ export const removeElements = createThunk(

export const setFilter = createThunk(
'setFilter',
({ dispatch }, filter, elementId, pageId, doRender = true) => {
({ dispatch }, filter, elementId, doRender = true) => {
const _setFilter = createAction('setFilter');
dispatch(_setFilter({ filter, elementId, pageId }));
dispatch(_setFilter({ filter, elementId }));

if (doRender === true) {
dispatch(fetchAllRenderables());
Expand Down
15 changes: 14 additions & 1 deletion x-pack/legacy/plugins/canvas/public/state/reducers/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ const trimElement = ({ id, position, expression, filter }) => ({
...(filter !== void 0 && { filter }),
});

const getPageWithElementId = (workpad, elementId) => {
const matchingPage = workpad.pages.find(page =>
page.elements.map(element => element.id).includes(elementId)
);

if (matchingPage) {
return matchingPage.id;
}

return undefined;
};

export const elementsReducer = handleActions(
{
// TODO: This takes the entire element, which is not necessary, it could just take the id.
Expand All @@ -95,7 +107,8 @@ export const elementsReducer = handleActions(
return assignNodeProperties(workpadState, pageId, elementId, { expression });
},
[actions.setFilter]: (workpadState, { payload }) => {
const { filter, pageId, elementId } = payload;
const { filter, elementId } = payload;
const pageId = getPageWithElementId(workpadState, elementId);
return assignNodeProperties(workpadState, pageId, elementId, { filter });
},
[actions.setMultiplePositions]: (workpadState, { payload }) =>
Expand Down

0 comments on commit f0194db

Please sign in to comment.