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

Add a Async Select that fetches options from given endpoint #1909

Merged
merged 2 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 0 additions & 61 deletions superset/assets/javascripts/SqlLab/components/DatabaseSelect.jsx

This file was deleted.

50 changes: 25 additions & 25 deletions superset/assets/javascripts/SqlLab/components/QuerySearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import React from 'react';
import { Button } from 'react-bootstrap';
import Select from 'react-select';
import QueryTable from './QueryTable';
import DatabaseSelect from './DatabaseSelect';
import { now, epochTimeXHoursAgo,
epochTimeXDaysAgo, epochTimeXYearsAgo } from '../../modules/dates';
import { STATUS_OPTIONS, TIME_OPTIONS } from '../constants';
import AsyncSelect from '../../components/AsyncSelect';

const propTypes = {
actions: React.PropTypes.object.isRequired,
Expand All @@ -28,9 +28,6 @@ class QuerySearch extends React.PureComponent {
queriesLoading: true,
};
}
componentWillMount() {
this.fetchUsers();
}
componentDidMount() {
this.refreshQueries();
}
Expand Down Expand Up @@ -89,18 +86,23 @@ class QuerySearch extends React.PureComponent {
changeSearch(event) {
this.setState({ searchText: event.target.value });
}
fetchUsers() {
this.setState({ userLoading: true });
const url = '/users/api/read';
$.getJSON(url, (data, status) => {
if (status === 'success') {
const options = [];
for (let i = 0; i < data.pks.length; i++) {
options.push({ value: data.pks[i], label: data.result[i].username });
}
this.setState({ userOptions: options, userLoading: false });
}
});
userMutator(data) {
const options = [];
for (let i = 0; i < data.pks.length; i++) {
options.push({ value: data.pks[i], label: data.result[i].username });
}
return options;
}
dbMutator(data) {
const options = data.result.map((db) => ({ value: db.id, label: db.database_name }));
this.props.actions.setDatabases(data.result);
if (data.result.length === 0) {
this.props.actions.addAlert({
bsStyle: 'danger',
msg: "It seems you don't have access to any database",
});
}
return options;
}
refreshQueries() {
this.setState({ queriesLoading: true });
Expand All @@ -125,21 +127,19 @@ class QuerySearch extends React.PureComponent {
<div>
<div id="search-header" className="row space-1">
<div className="col-sm-2">
<Select
name="select-user"
placeholder="[User]"
options={this.state.userOptions}
<AsyncSelect
dataEndpoint="/users/api/read"
mutator={this.userMutator}
value={this.state.userId}
isLoading={this.state.userLoading}
autosize={false}
onChange={this.changeUser.bind(this)}
/>
</div>
<div className="col-sm-2">
<DatabaseSelect
<AsyncSelect
onChange={this.onChange.bind(this)}
databaseId={this.state.databaseId}
actions={this.props.actions}
dataEndpoint="/databaseasync/api/read?_flt_0_expose_in_sqllab=1"
value={this.state.databaseId}
mutator={this.dbMutator.bind(this)}
/>
</div>
<div className="col-sm-4">
Expand Down
21 changes: 17 additions & 4 deletions superset/assets/javascripts/SqlLab/components/SqlEditorLeftBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import Select from 'react-select';
import { Label, Button } from 'react-bootstrap';
import TableElement from './TableElement';
import DatabaseSelect from './DatabaseSelect';
import AsyncSelect from '../../components/AsyncSelect';

const propTypes = {
queryEditor: React.PropTypes.object.isRequired,
Expand Down Expand Up @@ -44,6 +44,17 @@ class SqlEditorLeftBar extends React.PureComponent {
this.fetchSchemas(val);
}
}
dbMutator(data) {
const options = data.result.map((db) => ({ value: db.id, label: db.database_name }));
this.props.actions.setDatabases(data.result);
if (data.result.length === 0) {
this.props.actions.addAlert({
bsStyle: 'danger',
msg: "It seems you don't have access to any database",
});
}
return options;
}
resetState() {
this.props.actions.resetState();
}
Expand Down Expand Up @@ -103,15 +114,17 @@ class SqlEditorLeftBar extends React.PureComponent {
<div className="clearfix sql-toolbar scrollbar-content">
{networkAlert}
<div>
<DatabaseSelect
<AsyncSelect
dataEndpoint="/databaseasync/api/read?_flt_0_expose_in_sqllab=1"
onChange={this.onChange.bind(this)}
databaseId={this.props.queryEditor.dbId}
actions={this.props.actions}
value={this.props.queryEditor.dbId}
valueRenderer={(o) => (
<div>
<span className="text-muted">Database:</span> {o.label}
</div>
)}
mutator={this.dbMutator.bind(this)}
placeholder="Select a database"
/>
</div>
<div className="m-t-5">
Expand Down
59 changes: 59 additions & 0 deletions superset/assets/javascripts/components/AsyncSelect.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const $ = window.$ = require('jquery');
import React from 'react';
import Select from 'react-select';

const propTypes = {
dataEndpoint: React.PropTypes.string.isRequired,
onChange: React.PropTypes.func.isRequired,
mutator: React.PropTypes.func.isRequired,
value: React.PropTypes.number,
valueRenderer: React.PropTypes.func,
placeholder: React.PropTypes.string,
};

const defaultProps = {
placeholder: 'Select ...',
valueRenderer: (o) => (<div>{o.label}</div>),
};

class AsyncSelect extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
isLoading: false,
options: [],
};
}
componentDidMount() {
this.fetchOptions();
}
fetchOptions() {
this.setState({ isLoading: true });
const mutator = this.props.mutator;
$.get(this.props.dataEndpoint, (data) => {
this.setState({ options: mutator ? mutator(data) : data, isLoading: false });
});
}
onChange(opt) {
this.props.onChange(opt);
}
render() {
return (
<div>
<Select
Copy link

Choose a reason for hiding this comment

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

what do we show when the select is loading data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isLoading is true when loading the data, Select will render a spinner

placeholder={this.props.placeholder}
options={this.state.options}
value={this.props.value}
isLoading={this.state.isLoading}
onChange={this.onChange.bind(this)}
valueRenderer={this.props.valueRenderer}
/>
</div>
);
}
}

AsyncSelect.propTypes = propTypes;
AsyncSelect.defaultProps = defaultProps;

export default AsyncSelect;
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import React from 'react';
import Select from 'react-select';
import DatabaseSelect from '../../../javascripts/SqlLab/components/DatabaseSelect';
import AsyncSelect from '../../../javascripts/components/AsyncSelect';
import { shallow } from 'enzyme';
import { describe, it } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';

describe('DatabaseSelect', () => {
describe('AsyncSelect', () => {
const mockedProps = {
actions: {},
dataEndpoint: '/slicemodelview/api/read',
onChange: sinon.spy(),
mutator: () => {},
};
it('is valid element', () => {
expect(
React.isValidElement(<DatabaseSelect {...mockedProps} />)
React.isValidElement(<AsyncSelect {...mockedProps} />)
).to.equal(true);
});

it('has one select', () => {
const wrapper = shallow(
<DatabaseSelect {...mockedProps} />
<AsyncSelect {...mockedProps} />
);
expect(wrapper.find(Select)).to.have.length(1);
});

it('calls onChange on select change', () => {
const onChange = sinon.spy();
const wrapper = shallow(
<DatabaseSelect onChange={onChange} />
<AsyncSelect {...mockedProps} />
);
wrapper.find(Select).simulate('change', { value: 1 });
expect(onChange).to.have.property('callCount', 1);
expect(mockedProps.onChange).to.have.property('callCount', 1);
});
});
10 changes: 2 additions & 8 deletions superset/assets/spec/javascripts/sqllab/QuerySearch_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ describe('QuerySearch', () => {
});
const wrapper = shallow(<QuerySearch {...mockedProps} />);

it('should have four Select', () => {
expect(wrapper.find(Select)).to.have.length(4);
});

it('updates userId on user selects change', () => {
wrapper.find('[name="select-user"]')
.simulate('change', { value: 1 });
expect(wrapper.state().userId).to.equal(1);
it('should have three Select', () => {
expect(wrapper.find(Select)).to.have.length(3);
Copy link

Choose a reason for hiding this comment

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

should this assert 3 or 4 selects? it was 4 before this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

after we switched the user select to AsyncSelect it became 3

Copy link

Choose a reason for hiding this comment

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

ok cool. we should update the it statement on L21 to read should have three selects in that case.

});

it('updates fromTime on user selects from time', () => {
Expand Down