mirror of
https://github.com/apache/superset.git
synced 2024-09-17 11:09:47 -04:00
Store last selected dashboard in sessionStorage (#7181)
* Store last selected dashboard in sessionStorage * Fix tests
This commit is contained in:
parent
3a359fd892
commit
f83b979342
@ -19,6 +19,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import configureStore from 'redux-mock-store';
|
import configureStore from 'redux-mock-store';
|
||||||
import thunk from 'redux-thunk';
|
import thunk from 'redux-thunk';
|
||||||
|
import { bindActionCreators } from 'redux';
|
||||||
|
|
||||||
import { shallow, mount } from 'enzyme';
|
import { shallow, mount } from 'enzyme';
|
||||||
import { Modal, Button, Radio } from 'react-bootstrap';
|
import { Modal, Button, Radio } from 'react-bootstrap';
|
||||||
@ -52,7 +53,12 @@ describe('SaveModal', () => {
|
|||||||
|
|
||||||
const defaultProps = {
|
const defaultProps = {
|
||||||
onHide: () => ({}),
|
onHide: () => ({}),
|
||||||
actions: saveModalActions,
|
actions: bindActionCreators(saveModalActions, (arg) => {
|
||||||
|
if (typeof arg === 'function') {
|
||||||
|
return arg(jest.fn);
|
||||||
|
}
|
||||||
|
return arg;
|
||||||
|
}),
|
||||||
form_data: { datasource: '107__table' },
|
form_data: { datasource: '107__table' },
|
||||||
};
|
};
|
||||||
const mockEvent = {
|
const mockEvent = {
|
||||||
@ -108,15 +114,15 @@ describe('SaveModal', () => {
|
|||||||
|
|
||||||
it('componentDidMount', () => {
|
it('componentDidMount', () => {
|
||||||
sinon.spy(SaveModal.prototype, 'componentDidMount');
|
sinon.spy(SaveModal.prototype, 'componentDidMount');
|
||||||
sinon.spy(saveModalActions, 'fetchDashboards');
|
sinon.spy(defaultProps.actions, 'fetchDashboards');
|
||||||
mount(<SaveModal {...defaultProps} />, {
|
mount(<SaveModal {...defaultProps} />, {
|
||||||
context: { store },
|
context: { store },
|
||||||
});
|
});
|
||||||
expect(SaveModal.prototype.componentDidMount.calledOnce).toBe(true);
|
expect(SaveModal.prototype.componentDidMount.calledOnce).toBe(true);
|
||||||
expect(saveModalActions.fetchDashboards.calledOnce).toBe(true);
|
expect(defaultProps.actions.fetchDashboards.calledOnce).toBe(true);
|
||||||
|
|
||||||
SaveModal.prototype.componentDidMount.restore();
|
SaveModal.prototype.componentDidMount.restore();
|
||||||
saveModalActions.fetchDashboards.restore();
|
defaultProps.actions.fetchDashboards.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('onChange', () => {
|
it('onChange', () => {
|
||||||
@ -139,7 +145,7 @@ describe('SaveModal', () => {
|
|||||||
.callsFake(() => ({ url: 'mockURL', payload: defaultProps.form_data }));
|
.callsFake(() => ({ url: 'mockURL', payload: defaultProps.form_data }));
|
||||||
|
|
||||||
sinon
|
sinon
|
||||||
.stub(saveModalActions, 'saveSlice')
|
.stub(defaultProps.actions, 'saveSlice')
|
||||||
.callsFake(() =>
|
.callsFake(() =>
|
||||||
Promise.resolve({ data: { dashboard: '/mock/', slice: { slice_url: '/mock/' } } }),
|
Promise.resolve({ data: { dashboard: '/mock/', slice: { slice_url: '/mock/' } } }),
|
||||||
);
|
);
|
||||||
@ -147,13 +153,13 @@ describe('SaveModal', () => {
|
|||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
exploreUtils.getExploreUrlAndPayload.restore();
|
exploreUtils.getExploreUrlAndPayload.restore();
|
||||||
saveModalActions.saveSlice.restore();
|
defaultProps.actions.saveSlice.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should save slice', () => {
|
it('should save slice', () => {
|
||||||
const wrapper = getWrapper();
|
const wrapper = getWrapper();
|
||||||
wrapper.instance().saveOrOverwrite(true);
|
wrapper.instance().saveOrOverwrite(true);
|
||||||
const args = saveModalActions.saveSlice.getCall(0).args;
|
const args = defaultProps.actions.saveSlice.getCall(0).args;
|
||||||
expect(args[0]).toEqual(defaultProps.form_data);
|
expect(args[0]).toEqual(defaultProps.form_data);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -167,7 +173,7 @@ describe('SaveModal', () => {
|
|||||||
|
|
||||||
wrapper.setState({ saveToDashboardId });
|
wrapper.setState({ saveToDashboardId });
|
||||||
wrapper.instance().saveOrOverwrite(true);
|
wrapper.instance().saveOrOverwrite(true);
|
||||||
const args = saveModalActions.saveSlice.getCall(0).args;
|
const args = defaultProps.actions.saveSlice.getCall(0).args;
|
||||||
expect(args[1].save_to_dashboard_id).toBe(saveToDashboardId);
|
expect(args[1].save_to_dashboard_id).toBe(saveToDashboardId);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -181,7 +187,7 @@ describe('SaveModal', () => {
|
|||||||
|
|
||||||
wrapper.setState({ newDashboardName });
|
wrapper.setState({ newDashboardName });
|
||||||
wrapper.instance().saveOrOverwrite(true);
|
wrapper.instance().saveOrOverwrite(true);
|
||||||
const args = saveModalActions.saveSlice.getCall(0).args;
|
const args = defaultProps.actions.saveSlice.getCall(0).args;
|
||||||
expect(args[1].new_dashboard_name).toBe(newDashboardName);
|
expect(args[1].new_dashboard_name).toBe(newDashboardName);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -251,13 +257,13 @@ describe('SaveModal', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('removeAlert', () => {
|
it('removeAlert', () => {
|
||||||
sinon.spy(saveModalActions, 'removeSaveModalAlert');
|
sinon.spy(defaultProps.actions, 'removeSaveModalAlert');
|
||||||
const wrapper = getWrapper();
|
const wrapper = getWrapper();
|
||||||
wrapper.setProps({ alert: 'old alert' });
|
wrapper.setProps({ alert: 'old alert' });
|
||||||
|
|
||||||
wrapper.instance().removeAlert();
|
wrapper.instance().removeAlert();
|
||||||
expect(saveModalActions.removeSaveModalAlert.callCount).toBe(1);
|
expect(defaultProps.actions.removeSaveModalAlert.callCount).toBe(1);
|
||||||
expect(wrapper.state().alert).toBeNull();
|
expect(wrapper.state().alert).toBeNull();
|
||||||
saveModalActions.removeSaveModalAlert.restore();
|
defaultProps.actions.removeSaveModalAlert.restore();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -54,7 +54,14 @@ class SaveModal extends React.Component {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
this.props.actions.fetchDashboards(this.props.userId);
|
this.props.actions.fetchDashboards(this.props.userId).then(() => {
|
||||||
|
const dashboardIds = this.props.dashboards.map(dashboard => dashboard.value);
|
||||||
|
let recentDashboard = sessionStorage.getItem('save_chart_recent_dashboard');
|
||||||
|
recentDashboard = recentDashboard && parseInt(recentDashboard, 10);
|
||||||
|
if (recentDashboard !== null && dashboardIds.indexOf(recentDashboard) !== -1) {
|
||||||
|
this.setState({ saveToDashboardId: recentDashboard, addToDash: 'existing' });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
onChange(name, event) {
|
onChange(name, event) {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
@ -125,6 +132,11 @@ class SaveModal extends React.Component {
|
|||||||
sliceParams.goto_dash = gotodash;
|
sliceParams.goto_dash = gotodash;
|
||||||
|
|
||||||
this.props.actions.saveSlice(this.props.form_data, sliceParams).then(({ data }) => {
|
this.props.actions.saveSlice(this.props.form_data, sliceParams).then(({ data }) => {
|
||||||
|
if (data.dashboard_id === null) {
|
||||||
|
sessionStorage.removeItem('save_chart_recent_dashboard');
|
||||||
|
} else {
|
||||||
|
sessionStorage.setItem('save_chart_recent_dashboard', data.dashboard_id);
|
||||||
|
}
|
||||||
// Go to new slice url or dashboard url
|
// Go to new slice url or dashboard url
|
||||||
if (gotodash) {
|
if (gotodash) {
|
||||||
window.location = supersetURL(data.dashboard);
|
window.location = supersetURL(data.dashboard);
|
||||||
|
@ -1489,6 +1489,7 @@ class Superset(BaseSupersetView):
|
|||||||
'can_overwrite': is_owner(slc, g.user),
|
'can_overwrite': is_owner(slc, g.user),
|
||||||
'form_data': slc.form_data,
|
'form_data': slc.form_data,
|
||||||
'slice': slc.data,
|
'slice': slc.data,
|
||||||
|
'dashboard_id': dash.id if dash else None,
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.args.get('goto_dash') == 'true':
|
if request.args.get('goto_dash') == 'true':
|
||||||
|
Loading…
Reference in New Issue
Block a user