Skip to content

Commit

Permalink
fix: fetch datasets list after dataset created successfully (#10290)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lily Kuang authored Jul 12, 2020
1 parent 1f5eec1 commit 9d75740
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 66 deletions.
111 changes: 56 additions & 55 deletions superset-frontend/src/components/Menu/SubMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import React, { useState } from 'react';
import styled from '@superset-ui/style';
import DatasetModal from 'src/views/datasetList/DatasetModal';
import { Button, Nav, Navbar, MenuItem } from 'react-bootstrap';
Expand Down Expand Up @@ -64,71 +64,72 @@ const StyledHeader = styled.header`
`;

interface SubMenuProps {
createButton?: { name: string; url: string | null };
canCreate?: boolean;
name: string;
childs?: Array<{ label: string; name: string; url: string }>;
createButton?: { name: string; url: string | null };
fetchData?: () => void;
name: string;
}

interface SubMenuState {
selectedMenu: string;
isModalOpen: boolean;
}

class SubMenu extends React.PureComponent<SubMenuProps, SubMenuState> {
state: SubMenuState = {
selectedMenu:
this.props.childs && this.props.childs[0]
? this.props.childs[0].label
: '',
isModalOpen: false,
};
const SubMenu = ({
canCreate,
childs,
createButton,
fetchData,
name,
}: SubMenuProps) => {
const [isModalOpen, setIsModalOpen] = useState(false);
const [selectedMenu, setSelectedMenu] = useState<string | undefined>(
childs?.[0]?.label,
);

onOpen = () => {
this.setState({ isModalOpen: true });
const onOpen = () => {
setIsModalOpen(true);
};

onClose = () => {
this.setState({ isModalOpen: false });
const onClose = () => {
setIsModalOpen(false);
};

handleClick = (item: string) => () => {
this.setState({ selectedMenu: item });
const handleClick = (item: string) => () => {
setSelectedMenu(item);
};

render() {
return (
<StyledHeader>
<Navbar inverse fluid role="navigation">
<Navbar.Header>
<Navbar.Brand>{this.props.name}</Navbar.Brand>
</Navbar.Header>
<DatasetModal show={this.state.isModalOpen} onHide={this.onClose} />
<Nav>
{this.props.childs &&
this.props.childs.map(child => (
<MenuItem
active={child.label === this.state.selectedMenu}
key={`${child.label}`}
eventKey={`${child.name}`}
href={child.url}
onClick={this.handleClick(child.label)}
>
{child.label}
</MenuItem>
))}
return (
<StyledHeader>
<Navbar inverse fluid role="navigation">
<Navbar.Header>
<Navbar.Brand>{name}</Navbar.Brand>
</Navbar.Header>
<DatasetModal
fetchData={fetchData}
onHide={onClose}
show={isModalOpen}
/>
<Nav>
{childs &&
childs.map(child => (
<MenuItem
active={child.label === selectedMenu}
eventKey={`${child.name}`}
href={child.url}
key={`${child.label}`}
onClick={handleClick(child.label)}
>
{child.label}
</MenuItem>
))}
</Nav>
{canCreate && createButton && (
<Nav className="navbar-right">
<Button onClick={onOpen}>
<i className="fa fa-plus" /> {createButton.name}
</Button>
</Nav>
{this.props.canCreate && this.props.createButton && (
<Nav className="navbar-right">
<Button onClick={this.onOpen}>
<i className="fa fa-plus" /> {this.props.createButton.name}
</Button>
</Nav>
)}
</Navbar>
</StyledHeader>
);
}
}
)}
</Navbar>
</StyledHeader>
);
};

export default SubMenu;
6 changes: 5 additions & 1 deletion superset-frontend/src/views/datasetList/DatasetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,11 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({

return (
<>
<SubMenu {...menu} canCreate={canCreate()} />
<SubMenu
{...menu}
canCreate={canCreate()}
fetchData={() => lastFetchDataConfig && fetchData(lastFetchDataConfig)}
/>
<ConfirmStatusChange
title={t('Please confirm')}
description={t(
Expand Down
24 changes: 14 additions & 10 deletions superset-frontend/src/views/datasetList/DatasetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
import React, { FunctionComponent, useState } from 'react';
import styled from '@superset-ui/style';
import { SupersetClient } from '@superset-ui/connection';
import { t } from '@superset-ui/translation';
import { isEmpty, isNil } from 'lodash';
import { t } from '@superset-ui/translation';
import Icon from 'src/components/Icon';
import TableSelector from 'src/components/TableSelector';
import Modal from 'src/components/Modal';
import TableSelector from 'src/components/TableSelector';
import withToasts from '../../messageToasts/enhancers/withToasts';

interface DatasetModalProps {
addDangerToast: (msg: string) => void;
addSuccessToast: (msg: string) => void;
fetchData?: () => void;
onHide: () => void;
show: boolean;
}
Expand All @@ -47,13 +48,14 @@ const TableSelectorContainer = styled.div`
const DatasetModal: FunctionComponent<DatasetModalProps> = ({
addDangerToast,
addSuccessToast,
fetchData,
onHide,
show,
}) => {
const [datasourceId, setDatasourceId] = useState<number | null>(null);
const [disableSave, setDisableSave] = useState(true);
const [currentSchema, setSchema] = useState('');
const [currentTableName, setTableName] = useState('');
const [datasourceId, setDatasourceId] = useState<number | null>(null);
const [disableSave, setDisableSave] = useState(true);

const onChange = ({
dbId,
Expand All @@ -71,17 +73,19 @@ const DatasetModal: FunctionComponent<DatasetModalProps> = ({
};

const onSave = () => {
const data = {
database: datasourceId,
schema: currentSchema,
table_name: currentTableName,
};
SupersetClient.post({
endpoint: '/api/v1/dataset/',
body: JSON.stringify(data),
body: JSON.stringify({
database: datasourceId,
schema: currentSchema,
table_name: currentTableName,
}),
headers: { 'Content-Type': 'application/json' },
})
.then(() => {
if (fetchData) {
fetchData();
}
addSuccessToast(t('The dataset has been saved'));
onHide();
})
Expand Down

0 comments on commit 9d75740

Please sign in to comment.