Skip to content

Commit e58a3ab

Browse files
hughhhheschutho
andauthored
fix: permalink save/overwrites in explore (apache#25112)
Co-authored-by: Elizabeth Thompson <[email protected]>
1 parent 18a1c8d commit e58a3ab

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

superset-frontend/src/explore/components/SaveModal.test.jsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import Button from 'src/components/Button';
2727
import fetchMock from 'fetch-mock';
2828

2929
import * as saveModalActions from 'src/explore/actions/saveModalActions';
30-
import SaveModal, { StyledModal } from 'src/explore/components/SaveModal';
30+
import SaveModal, {
31+
PureSaveModal,
32+
StyledModal,
33+
} from 'src/explore/components/SaveModal';
3134
import { BrowserRouter } from 'react-router-dom';
3235

3336
const middlewares = [thunk];
@@ -100,8 +103,12 @@ const queryDefaultProps = {
100103
};
101104

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

104-
beforeAll(() => fetchMock.get(fetchDashboardsEndpoint, mockDashboardData));
108+
beforeAll(() => {
109+
fetchMock.get(fetchDashboardsEndpoint, mockDashboardData);
110+
fetchMock.get(fetchChartEndpoint, { id: 1, dashboards: [1] });
111+
});
105112

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

@@ -226,3 +233,27 @@ test('set dataset name when chart source is query', () => {
226233
expect(wrapper.find('[data-test="new-dataset-name"]')).toExist();
227234
expect(wrapper.state().datasetName).toBe('test');
228235
});
236+
237+
test('make sure slice_id in the URLSearchParams before the redirect', () => {
238+
const myProps = {
239+
...defaultProps,
240+
slice: { slice_id: 1, slice_name: 'title', owners: [1] },
241+
actions: {
242+
setFormData: jest.fn(),
243+
updateSlice: jest.fn(() => Promise.resolve({ id: 1 })),
244+
getSliceDashboards: jest.fn(),
245+
},
246+
user: { userId: 1 },
247+
history: {
248+
replace: jest.fn(),
249+
},
250+
dispatch: jest.fn(),
251+
};
252+
253+
const saveModal = new PureSaveModal(myProps);
254+
const result = saveModal.handleRedirect(
255+
'https://example.com/?name=John&age=30',
256+
{ id: 1 },
257+
);
258+
expect(result.get('slice_id')).toEqual('1');
259+
});

superset-frontend/src/explore/components/SaveModal.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> {
164164
this.props.dispatch(setSaveChartModalVisibility(false));
165165
}
166166

167+
handleRedirect = (windowLocationSearch: string, chart: any) => {
168+
const searchParams = new URLSearchParams(windowLocationSearch);
169+
searchParams.set('save_action', this.state.action);
170+
if (this.state.action !== 'overwrite') {
171+
searchParams.delete('form_data_key');
172+
}
173+
174+
searchParams.set('slice_id', chart.id.toString());
175+
return searchParams;
176+
};
177+
167178
async saveOrOverwrite(gotodash: boolean) {
168179
this.setState({ isLoading: true });
169180

@@ -270,14 +281,7 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> {
270281
return;
271282
}
272283

273-
const searchParams = new URLSearchParams(window.location.search);
274-
searchParams.set('save_action', this.state.action);
275-
if (this.state.action !== 'overwrite') {
276-
searchParams.delete('form_data_key');
277-
}
278-
if (this.state.action === 'saveas') {
279-
searchParams.set('slice_id', value.id.toString());
280-
}
284+
const searchParams = this.handleRedirect(window.location.search, value);
281285
this.props.history.replace(`/explore/?${searchParams.toString()}`);
282286

283287
this.setState({ isLoading: false });
@@ -527,3 +531,6 @@ function mapStateToProps({
527531
}
528532

529533
export default withRouter(connect(mapStateToProps)(SaveModal));
534+
535+
// User for testing purposes need to revisit once we convert this to functional component
536+
export { SaveModal as PureSaveModal };

0 commit comments

Comments
 (0)