Skip to content

Commit

Permalink
Fetch current page first (elastic#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rashid Khan authored May 10, 2018
1 parent 7111c07 commit 261720d
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions public/state/actions/elements.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createAction } from 'redux-actions';
import { get, pick, cloneDeep } from 'lodash';
import { get, pick, cloneDeep, without } from 'lodash';
import { set, del } from 'object-path-immutable';
import { createThunk } from 'redux-thunks';
import { getPages, getElementById } from '../selectors/workpad';
import { getPages, getElementById, getSelectedPageIndex } from '../selectors/workpad';
import { getValue } from '../selectors/resolved_args';
import { getDefaultElement } from '../defaults';
import { toExpression, safeElementFromExpression } from '../../../common/lib/ast';
Expand Down Expand Up @@ -135,33 +135,42 @@ export const fetchRenderable = createThunk('fetchRenderable', ({ dispatch }, ele
});

export const fetchAllRenderables = createThunk('fetchAllRenderables', ({ dispatch, getState }) => {
const pages = getPages(getState());
const workpadPages = getPages(getState());
const currentPageIndex = getSelectedPageIndex(getState());

const elements = [];
pages.forEach(page => {
page.elements.forEach(element => {
elements.push(element);
});
});
const currentPage = workpadPages[currentPageIndex];
const otherPages = without(workpadPages, currentPage);

dispatch(args.inFlightActive());

const renderablePromises = elements.map(element => {
const ast = element.ast || safeElementFromExpression(element.expression);
const argumentPath = [element.id, 'expressionRenderable'];

return runInterpreter(ast, null, { castToRender: true })
.then(renderable => ({ path: argumentPath, value: renderable }))
.catch(err => {
notify.error(err);
return { path: argumentPath, value: err };
function fetchElementsOnPages(pages) {
const elements = [];
pages.forEach(page => {
page.elements.forEach(element => {
elements.push(element);
});
});
});

const renderablePromises = elements.map(element => {
const ast = element.ast || safeElementFromExpression(element.expression);
const argumentPath = [element.id, 'expressionRenderable'];

return runInterpreter(ast, null, { castToRender: true })
.then(renderable => ({ path: argumentPath, value: renderable }))
.catch(err => {
notify.error(err);
return { path: argumentPath, value: err };
});
});

return Promise.all(renderablePromises).then(renderables => {
dispatch(args.setValues(renderables));
});
}

Promise.all(renderablePromises).then(renderables => {
dispatch(args.inFlightComplete());
dispatch(args.setValues(renderables));
});
fetchElementsOnPages([currentPage])
.then(() => fetchElementsOnPages(otherPages))
.then(() => dispatch(args.inFlightComplete()));
});

export const duplicateElement = createThunk(
Expand Down

0 comments on commit 261720d

Please sign in to comment.