Skip to content

Commit

Permalink
Store last selected dashboard in sessionStorage (#7181)
Browse files Browse the repository at this point in the history
* Store last selected dashboard in sessionStorage

* Fix tests
  • Loading branch information
leakingoxide authored and Grace Guo committed Apr 24, 2019
1 parent 3a359fd commit f83b979
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import React from 'react';
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { bindActionCreators } from 'redux';

import { shallow, mount } from 'enzyme';
import { Modal, Button, Radio } from 'react-bootstrap';
Expand Down Expand Up @@ -52,7 +53,12 @@ describe('SaveModal', () => {

const defaultProps = {
onHide: () => ({}),
actions: saveModalActions,
actions: bindActionCreators(saveModalActions, (arg) => {
if (typeof arg === 'function') {
return arg(jest.fn);
}
return arg;
}),
form_data: { datasource: '107__table' },
};
const mockEvent = {
Expand Down Expand Up @@ -108,15 +114,15 @@ describe('SaveModal', () => {

it('componentDidMount', () => {
sinon.spy(SaveModal.prototype, 'componentDidMount');
sinon.spy(saveModalActions, 'fetchDashboards');
sinon.spy(defaultProps.actions, 'fetchDashboards');
mount(<SaveModal {...defaultProps} />, {
context: { store },
});
expect(SaveModal.prototype.componentDidMount.calledOnce).toBe(true);
expect(saveModalActions.fetchDashboards.calledOnce).toBe(true);
expect(defaultProps.actions.fetchDashboards.calledOnce).toBe(true);

SaveModal.prototype.componentDidMount.restore();
saveModalActions.fetchDashboards.restore();
defaultProps.actions.fetchDashboards.restore();
});

it('onChange', () => {
Expand All @@ -139,21 +145,21 @@ describe('SaveModal', () => {
.callsFake(() => ({ url: 'mockURL', payload: defaultProps.form_data }));

sinon
.stub(saveModalActions, 'saveSlice')
.stub(defaultProps.actions, 'saveSlice')
.callsFake(() =>
Promise.resolve({ data: { dashboard: '/mock/', slice: { slice_url: '/mock/' } } }),
);
});

afterEach(() => {
exploreUtils.getExploreUrlAndPayload.restore();
saveModalActions.saveSlice.restore();
defaultProps.actions.saveSlice.restore();
});

it('should save slice', () => {
const wrapper = getWrapper();
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);
});

Expand All @@ -167,7 +173,7 @@ describe('SaveModal', () => {

wrapper.setState({ saveToDashboardId });
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);
});

Expand All @@ -181,7 +187,7 @@ describe('SaveModal', () => {

wrapper.setState({ newDashboardName });
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);
});
});
Expand Down Expand Up @@ -251,13 +257,13 @@ describe('SaveModal', () => {
});

it('removeAlert', () => {
sinon.spy(saveModalActions, 'removeSaveModalAlert');
sinon.spy(defaultProps.actions, 'removeSaveModalAlert');
const wrapper = getWrapper();
wrapper.setProps({ alert: 'old alert' });

wrapper.instance().removeAlert();
expect(saveModalActions.removeSaveModalAlert.callCount).toBe(1);
expect(defaultProps.actions.removeSaveModalAlert.callCount).toBe(1);
expect(wrapper.state().alert).toBeNull();
saveModalActions.removeSaveModalAlert.restore();
defaultProps.actions.removeSaveModalAlert.restore();
});
});
14 changes: 13 additions & 1 deletion superset/assets/src/explore/components/SaveModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ class SaveModal extends React.Component {
};
}
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) {
switch (name) {
Expand Down Expand Up @@ -125,6 +132,11 @@ class SaveModal extends React.Component {
sliceParams.goto_dash = gotodash;

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
if (gotodash) {
window.location = supersetURL(data.dashboard);
Expand Down
1 change: 1 addition & 0 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,7 @@ def save_or_overwrite_slice(
'can_overwrite': is_owner(slc, g.user),
'form_data': slc.form_data,
'slice': slc.data,
'dashboard_id': dash.id if dash else None,
}

if request.args.get('goto_dash') == 'true':
Expand Down

0 comments on commit f83b979

Please sign in to comment.