Skip to content

Commit ec2eb3d

Browse files
refactor: Renders addSlice in SPA (apache#20675)
* refactor: Renders addSlice in SPA * Removes unused imports * Fixes test imports * Removes unnecessary imports
1 parent acdb271 commit ec2eb3d

File tree

10 files changed

+52
-158
lines changed

10 files changed

+52
-158
lines changed

superset-frontend/src/addSlice/AddSliceContainer.test.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
* under the License.
1818
*/
1919
import React from 'react';
20-
import { ReactWrapper, mount } from 'enzyme';
20+
import { ReactWrapper } from 'enzyme';
21+
import { styledMount as mount } from 'spec/helpers/theming';
2122
import Button from 'src/components/Button';
2223
import { AsyncSelect } from 'src/components';
2324
import {
@@ -28,8 +29,6 @@ import {
2829
import VizTypeGallery from 'src/explore/components/controls/VizTypeControl/VizTypeGallery';
2930
import { act } from 'spec/helpers/testing-library';
3031
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
31-
import { ThemeProvider } from '@emotion/react';
32-
import { supersetTheme } from '@superset-ui/core';
3332

3433
const datasource = {
3534
value: '1',
@@ -62,13 +61,20 @@ const mockUserWithDatasetWrite: UserWithPermissionsAndRoles = {
6261
isAnonymous: false,
6362
};
6463

64+
// We don't need the actual implementation for the tests
65+
const routeProps = {
66+
history: {} as any,
67+
location: {} as any,
68+
match: {} as any,
69+
};
70+
6571
async function getWrapper(user = mockUser) {
6672
const wrapper = mount(
67-
<AddSliceContainer user={user} addSuccessToast={() => null} />,
68-
{
69-
wrappingComponent: ThemeProvider,
70-
wrappingComponentProps: { theme: supersetTheme },
71-
},
73+
<AddSliceContainer
74+
user={user}
75+
addSuccessToast={() => null}
76+
{...routeProps}
77+
/>,
7278
) as unknown as ReactWrapper<
7379
AddSliceContainerProps,
7480
AddSliceContainerState,

superset-frontend/src/addSlice/AddSliceContainer.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { styled, t, SupersetClient, JsonResponse } from '@superset-ui/core';
2323
import { getUrlParam } from 'src/utils/urlUtils';
2424
import { URL_PARAMS } from 'src/constants';
2525
import { isNullish } from 'src/utils/common';
26+
import { withRouter, RouteComponentProps } from 'react-router-dom';
2627
import Button from 'src/components/Button';
2728
import { AsyncSelect, Steps } from 'src/components';
2829
import { Tooltip } from 'src/components/Tooltip';
@@ -42,10 +43,10 @@ type Dataset = {
4243
datasource_type: string;
4344
};
4445

45-
export type AddSliceContainerProps = {
46+
export interface AddSliceContainerProps extends RouteComponentProps {
4647
user: UserWithPermissionsAndRoles;
4748
addSuccessToast: (arg: string) => void;
48-
};
49+
}
4950

5051
export type AddSliceContainerState = {
5152
datasource?: { label: string; value: string };
@@ -254,7 +255,7 @@ export class AddSliceContainer extends React.PureComponent<
254255
}
255256

256257
gotoSlice() {
257-
window.location.href = this.exploreUrl();
258+
this.props.history.push(this.exploreUrl());
258259
}
259260

260261
changeDatasource(datasource: { label: string; value: string }) {
@@ -418,4 +419,4 @@ export class AddSliceContainer extends React.PureComponent<
418419
}
419420
}
420421

421-
export default withToasts(AddSliceContainer);
422+
export default withRouter(withToasts(AddSliceContainer));

superset-frontend/src/addSlice/App.tsx

Lines changed: 0 additions & 66 deletions
This file was deleted.

superset-frontend/src/addSlice/index.tsx

Lines changed: 0 additions & 23 deletions
This file was deleted.

superset-frontend/src/views/CRUD/chart/ChartList.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import handleResourceExport from 'src/utils/export';
4141
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
4242
import SubMenu, { SubMenuProps } from 'src/views/components/SubMenu';
4343
import FaveStar from 'src/components/FaveStar';
44-
import { Link } from 'react-router-dom';
44+
import { Link, useHistory } from 'react-router-dom';
4545
import ListView, {
4646
Filter,
4747
FilterOperator,
@@ -153,6 +153,8 @@ function ChartList(props: ChartListProps) {
153153
user: { userId },
154154
} = props;
155155

156+
const history = useHistory();
157+
156158
const {
157159
state: {
158160
loading,
@@ -653,7 +655,7 @@ function ChartList(props: ChartListProps) {
653655
),
654656
buttonStyle: 'primary',
655657
onClick: () => {
656-
window.location.assign('/chart/add');
658+
history.push('/chart/add');
657659
},
658660
});
659661

superset-frontend/src/views/components/RightMenu.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,23 @@ const RightMenu = ({
321321
roles,
322322
) && (
323323
<Menu.Item key={menu.label}>
324-
<a href={menu.url}>
325-
<i
326-
data-test={`menu-item-${menu.label}`}
327-
className={`fa ${menu.icon}`}
328-
/>{' '}
329-
{menu.label}
330-
</a>
324+
{isFrontendRoute(menu.url) ? (
325+
<Link to={menu.url || ''}>
326+
<i
327+
data-test={`menu-item-${menu.label}`}
328+
className={`fa ${menu.icon}`}
329+
/>{' '}
330+
{menu.label}
331+
</Link>
332+
) : (
333+
<a href={menu.url}>
334+
<i
335+
data-test={`menu-item-${menu.label}`}
336+
className={`fa ${menu.icon}`}
337+
/>{' '}
338+
{menu.label}
339+
</a>
340+
)}
331341
</Menu.Item>
332342
)
333343
);

superset-frontend/src/views/routes.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import React, { lazy } from 'react';
2121
// not lazy loaded since this is the home page.
2222
import Welcome from 'src/views/CRUD/welcome/Welcome';
2323

24+
const AddSliceContainer = lazy(
25+
() =>
26+
import(
27+
/* webpackChunkName: "AddSliceContainer" */ 'src/addSlice/AddSliceContainer'
28+
),
29+
);
2430
const AnnotationLayersList = lazy(
2531
() =>
2632
import(
@@ -117,6 +123,10 @@ export const routes: Routes = [
117123
path: '/superset/dashboard/:idOrSlug/',
118124
Component: DashboardRoute,
119125
},
126+
{
127+
path: '/chart/add',
128+
Component: AddSliceContainer,
129+
},
120130
{
121131
path: '/chart/list/',
122132
Component: ChartList,

superset-frontend/webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ const config = {
209209
menu: addPreamble('src/views/menu.tsx'),
210210
spa: addPreamble('/src/views/index.tsx'),
211211
embedded: addPreamble('/src/embedded/index.tsx'),
212-
addSlice: addPreamble('/src/addSlice/index.tsx'),
213212
sqllab: addPreamble('/src/SqlLab/index.tsx'),
214213
profile: addPreamble('/src/profile/index.tsx'),
215214
showSavedQuery: [path.join(APP_DIR, '/src/showSavedQuery/index.jsx')],

superset/templates/superset/add_slice.html

Lines changed: 0 additions & 35 deletions
This file was deleted.

superset/views/chart/views.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
import json
18-
19-
from flask import g
2017
from flask_appbuilder import expose, has_access
2118
from flask_appbuilder.models.sqla.interface import SQLAInterface
2219
from flask_babel import lazy_gettext as _
@@ -26,9 +23,8 @@
2623
from superset.models.slice import Slice
2724
from superset.superset_typing import FlaskResponse
2825
from superset.utils import core as utils
29-
from superset.views.base import common_bootstrap_payload, DeleteMixin, SupersetModelView
26+
from superset.views.base import DeleteMixin, SupersetModelView
3027
from superset.views.chart.mixin import SliceMixin
31-
from superset.views.utils import bootstrap_user_data
3228

3329

3430
class SliceModelView(
@@ -57,13 +53,7 @@ def pre_delete(self, item: "SliceModelView") -> None:
5753
@expose("/add", methods=["GET", "POST"])
5854
@has_access
5955
def add(self) -> FlaskResponse:
60-
payload = {
61-
"common": common_bootstrap_payload(),
62-
"user": bootstrap_user_data(g.user, include_perms=True),
63-
}
64-
return self.render_template(
65-
"superset/add_slice.html", bootstrap_data=json.dumps(payload)
66-
)
56+
return super().render_app_template()
6757

6858
@expose("/list/")
6959
@has_access

0 commit comments

Comments
 (0)