Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: permalink save/overwrites in explore #25112

Merged
merged 12 commits into from
Oct 16, 2023
36 changes: 34 additions & 2 deletions superset-frontend/src/explore/components/SaveModal.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import Button from 'src/components/Button';
import fetchMock from 'fetch-mock';

import * as saveModalActions from 'src/explore/actions/saveModalActions';
import SaveModal, { StyledModal } from 'src/explore/components/SaveModal';
import SaveModal, {
PureSaveModal,
StyledModal,
} from 'src/explore/components/SaveModal';
import { BrowserRouter } from 'react-router-dom';

const middlewares = [thunk];
Expand Down Expand Up @@ -100,8 +103,12 @@ const queryDefaultProps = {
};

const fetchDashboardsEndpoint = `glob:*/dashboardasync/api/read?_flt_0_owners=${1}`;
const fetchChartEndpoint = `glob:*/api/v1/chart/${1}*`;

beforeAll(() => fetchMock.get(fetchDashboardsEndpoint, mockDashboardData));
beforeAll(() => {
fetchMock.get(fetchDashboardsEndpoint, mockDashboardData);
fetchMock.get(fetchChartEndpoint, { id: 1, dashboards: [1] });
});

afterAll(() => fetchMock.restore());

Expand Down Expand Up @@ -226,3 +233,28 @@ test('set dataset name when chart source is query', () => {
expect(wrapper.find('[data-test="new-dataset-name"]')).toExist();
expect(wrapper.state().datasetName).toBe('test');
});

test('make sure slice_id in the URLSearchParams before the redirect', () => {
const myProps = {
...defaultProps,
slice: { slice_id: 1, slice_name: 'title', owners: [1] },
actions: {
setFormData: jest.fn(),
updateSlice: jest.fn(() => Promise.resolve({ id: 1 })),
getSliceDashboards: jest.fn(),
},
user: { userId: 1 },
history: {
replace: jest.fn(),
},
dispatch: jest.fn(),
};

const saveModal = new PureSaveModal(myProps);
saveModal.setState = jest.fn();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need this still?

const result = saveModal.handleRedirect(
'https://example.com/?name=John&age=30',
{ id: 1 },
);
expect(result.get('slice_id')).toEqual('1');
});
26 changes: 18 additions & 8 deletions superset-frontend/src/explore/components/SaveModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> {
this.props.dispatch(setSaveChartModalVisibility(false));
}

handleRedirect = (windowLocationSearch: string, chart: any) => {
console.log(windowLocationSearch);
hughhhh marked this conversation as resolved.
Show resolved Hide resolved
const searchParams = new URLSearchParams(windowLocationSearch);
searchParams.set('save_action', this.state.action);
if (this.state.action !== 'overwrite') {
searchParams.delete('form_data_key');
}

searchParams.set('slice_id', chart.id.toString());
console.log(searchParams);
hughhhh marked this conversation as resolved.
Show resolved Hide resolved
return searchParams;
};

async saveOrOverwrite(gotodash: boolean) {
this.setState({ isLoading: true });

Expand Down Expand Up @@ -261,14 +274,7 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> {
return;
}

const searchParams = new URLSearchParams(window.location.search);
searchParams.set('save_action', this.state.action);
if (this.state.action !== 'overwrite') {
searchParams.delete('form_data_key');
}
if (this.state.action === 'saveas') {
searchParams.set('slice_id', value.id.toString());
}
const searchParams = this.handleRedirect(window.location.search, value);
this.props.history.replace(`/explore/?${searchParams.toString()}`);

this.setState({ isLoading: false });
Expand Down Expand Up @@ -518,3 +524,7 @@ function mapStateToProps({
}

export default withRouter(connect(mapStateToProps)(SaveModal));

// todo(hughhhh): For testing
// - need to revisit once we convert this to functional component
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove the todo and just say that you're exporting this for testing.

export { SaveModal as PureSaveModal };
Loading