Skip to content

Commit

Permalink
Added isFirstLoad to transient state. (elastic#30150)
Browse files Browse the repository at this point in the history
  • Loading branch information
cqliu1 committed Feb 11, 2019
1 parent 5dc9106 commit b6d22ea
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
20 changes: 13 additions & 7 deletions x-pack/plugins/canvas/public/apps/workpad/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { setWorkpad } from '../../state/actions/workpad';
import { setAssets, resetAssets } from '../../state/actions/assets';
import { gotoPage } from '../../state/actions/pages';
import { getWorkpad } from '../../state/selectors/workpad';
import { setCanUserWrite } from '../../state/actions/transient';
import { isFirstLoad } from '../../state/selectors/app';
import { setCanUserWrite, setFirstLoad } from '../../state/actions/transient';
import { WorkpadApp } from './workpad_app';

export const routes = [
Expand Down Expand Up @@ -48,7 +49,9 @@ export const routes = [
path: '/:id(/page/:page)',
action: (dispatch, getState) => async ({ params, router }) => {
// load workpad if given a new id via url param
const currentWorkpad = getWorkpad(getState());
const state = getState();
const currentWorkpad = getWorkpad(state);
const firstLoad = isFirstLoad(state);
if (params.id !== currentWorkpad.id) {
try {
const fetchedWorkpad = await workpadService.get(params.id);
Expand All @@ -60,11 +63,14 @@ export const routes = [
// tests if user has permissions to write to workpads
// TODO: remove this and switch to checking user privileges when canvas loads when granular app privileges are introduced
// https://github.com/elastic/kibana/issues/20277
workpadService.update(params.id, fetchedWorkpad).catch(err => {
if (err.response && err.response.status === 403) {
dispatch(setCanUserWrite(false));
}
});
if (firstLoad) {
workpadService.update(params.id, fetchedWorkpad).catch(err => {
if (err.response && err.response.status === 403) {
dispatch(setCanUserWrite(false));
}
});
dispatch(setFirstLoad(false));
}
} catch (err) {
notify.error(err, { title: `Couldn't load workpad with ID` });
return router.redirectTo('home');
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/canvas/public/state/actions/transient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import { createAction } from 'redux-actions';
export const setCanUserWrite = createAction('setCanUserWrite');
export const setFullscreen = createAction('setFullscreen');
export const selectElement = createAction('selectElement');
export const setFirstLoad = createAction('setFirstLoad');
1 change: 1 addition & 0 deletions x-pack/plugins/canvas/public/state/initial_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const getInitialState = path => {
app: {}, // Kibana stuff in here
assets: {}, // assets end up here
transient: {
isFirstLoad: true,
canUserWrite: true,
fullscreen: false,
selectedElement: null,
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/canvas/public/state/reducers/transient.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const transientReducer = handleActions(
return set(transientState, 'canUserWrite', Boolean(payload));
},

[actions.setFirstLoad]: (transientState, { payload }) => {
return set(transientState, 'isFirstLoad', Boolean(payload));
},

[actions.setFullscreen]: (transientState, { payload }) => {
return set(transientState, 'fullscreen', Boolean(payload));
},
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/canvas/public/state/selectors/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function canUserWrite(state) {
return get(state, 'transient.canUserWrite', true);
}

export function isFirstLoad(state) {
return get(state, 'transient.isFirstLoad', true);
}

export function getFullscreen(state) {
return get(state, 'transient.fullscreen', false);
}
Expand Down

0 comments on commit b6d22ea

Please sign in to comment.