Skip to content

Commit

Permalink
More improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Jan 30, 2017
1 parent e0a8ca9 commit 32a6df4
Show file tree
Hide file tree
Showing 17 changed files with 286 additions and 186 deletions.
66 changes: 52 additions & 14 deletions superset/assets/javascripts/explorev2/actions/exploreActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,44 @@ export function setDatasource(datasource) {
return { type: SET_DATASOURCE, datasource };
}

export const FETCH_STARTED = 'FETCH_STARTED';
export function fetchStarted() {
return { type: FETCH_STARTED };
export const SET_DATASOURCES = 'SET_DATASOURCES';
export function setDatasources(datasources) {
return { type: SET_DATASOURCES, datasources };
}

export const FETCH_SUCCEEDED = 'FETCH_SUCCEEDED';
export function fetchSucceeded() {
return { type: FETCH_SUCCEEDED };
export const FETCH_DATASOURCE_STARTED = 'FETCH_DATASOURCE_STARTED';
export function fetchDatasourceStarted() {
return { type: FETCH_DATASOURCE_STARTED };
}

export const FETCH_FAILED = 'FETCH_FAILED';
export function fetchFailed(error) {
return { type: FETCH_FAILED, error };
export const FETCH_DATASOURCE_SUCCEEDED = 'FETCH_DATASOURCE_SUCCEEDED';
export function fetchDatasourceSucceeded() {
return { type: FETCH_DATASOURCE_SUCCEEDED };
}

export const FETCH_DATASOURCE_FAILED = 'FETCH_DATASOURCE_FAILED';
export function fetchDatasourceFailed(error) {
return { type: FETCH_DATASOURCE_FAILED, error };
}

export const FETCH_DATASOURCES_STARTED = 'FETCH_DATASOURCES_STARTED';
export function fetchDatasourcesStarted() {
return { type: FETCH_DATASOURCES_STARTED };
}

export const FETCH_DATASOURCES_SUCCEEDED = 'FETCH_DATASOURCES_SUCCEEDED';
export function fetchDatasourcesSucceeded() {
return { type: FETCH_DATASOURCES_SUCCEEDED };
}

export const FETCH_DATASOURCES_FAILED = 'FETCH_DATASOURCES_FAILED';
export function fetchDatasourcesFailed(error) {
return { type: FETCH_DATASOURCES_FAILED, error };
}

export function fetchDatasourceMetadata(datasourceId, datasourceType) {
return function (dispatch) {
dispatch(fetchStarted());
dispatch(fetchDatasourceStarted());

if (datasourceId) {
const params = [`datasource_id=${datasourceId}`, `datasource_type=${datasourceType}`];
Expand All @@ -40,18 +60,36 @@ export function fetchDatasourceMetadata(datasourceId, datasourceType) {
url,
success: (data) => {
dispatch(setDatasource(data));
dispatch(fetchSucceeded());
dispatch(fetchDatasourceSucceeded());
},
error(error) {
dispatch(fetchFailed(error.responseJSON.error));
dispatch(fetchDatasourceFailed(error.responseJSON.error));
},
});
} else {
dispatch(fetchFailed('Please select a datasource'));
dispatch(fetchDatasourceFailed('Please select a datasource'));
}
};
}

export function fetchDatasources() {
return function (dispatch) {
dispatch(fetchDatasourcesStarted());
const url = '/superset/datasources/';
$.ajax({
type: 'GET',
url,
success: (data) => {
dispatch(setDatasources(data));
dispatch(fetchDatasourcesSucceeded());
},
error(error) {
dispatch(fetchDatasourcesFailed(error.responseJSON.error));
},
});
};
}

export const TOGGLE_FAVE_STAR = 'TOGGLE_FAVE_STAR';
export function toggleFaveStar(isStarred) {
return { type: TOGGLE_FAVE_STAR, isStarred };
Expand Down Expand Up @@ -126,7 +164,7 @@ export function fetchDashboardsSucceeded(choices) {

export const FETCH_DASHBOARDS_FAILED = 'FETCH_DASHBOARDS_FAILED';
export function fetchDashboardsFailed(userId) {
return { type: FETCH_FAILED, userId };
return { type: FETCH_DASHBOARDS_FAILED, userId };
}

export function fetchDashboards(userId) {
Expand Down
48 changes: 23 additions & 25 deletions superset/assets/javascripts/explorev2/components/ChartContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ const CHART_STATUS_MAP = {

const propTypes = {
actions: PropTypes.object.isRequired,
alert: PropTypes.string,
can_download: PropTypes.bool.isRequired,
slice_id: PropTypes.number.isRequired,
slice_name: PropTypes.string.isRequired,
viz_type: PropTypes.string.isRequired,
height: PropTypes.string.isRequired,
containerId: PropTypes.string.isRequired,
query: PropTypes.string,
column_formats: PropTypes.object,
chartStatus: PropTypes.string,
isStarred: PropTypes.bool.isRequired,
chartUpdateStartTime: PropTypes.number.isRequired,
chartUpdateEndTime: PropTypes.number,
alert: PropTypes.string,
chartUpdateStartTime: PropTypes.number.isRequired,
column_formats: PropTypes.object,
containerId: PropTypes.string.isRequired,
height: PropTypes.string.isRequired,
isStarred: PropTypes.bool.isRequired,
slice_id: PropTypes.number.isRequired,
slice_name: PropTypes.string.isRequired,
table_name: PropTypes.string,
viz_type: PropTypes.string.isRequired,
};

class ChartContainer extends React.PureComponent {
Expand All @@ -43,11 +42,11 @@ class ChartContainer extends React.PureComponent {
}

renderViz() {
console.log('rendering viz');
const mockSlice = this.getMockedSliceObject();
this.setState({ mockSlice });
visMap[this.props.viz_type](mockSlice, this.props.queryResponse);
try {
visMap[this.props.viz_type](mockSlice, this.props.queryResponse);
//visMap[this.props.viz_type](mockSlice, this.props.queryResponse);
} catch (e) {
this.props.actions.chartRenderingFailed(e);
}
Expand All @@ -68,7 +67,7 @@ class ChartContainer extends React.PureComponent {
getMockedSliceObject() {
const props = this.props;
return {
viewSqlQuery: props.query,
viewSqlQuery: this.props.queryResponse.query,
containerId: props.containerId,
selector: this.state.selector,
container: {
Expand Down Expand Up @@ -239,22 +238,21 @@ ChartContainer.propTypes = propTypes;

function mapStateToProps(state) {
return {
containerId: `slice-container-${state.viz.form_data.slice_id}`,
slice_id: state.viz.form_data.slice_id,
slice_name: state.viz.form_data.slice_name,
viz_type: state.viz.form_data.viz_type,
form_data: state.viz.form_data,
alert: state.chartAlert,
can_download: state.can_download,
chartUpdateStartTime: state.chartUpdateStartTime,
chartUpdateEndTime: state.chartUpdateEndTime,
query: state.viz.query,
column_formats: state.viz.column_formats,
chartStatus: state.chartStatus,
chartUpdateEndTime: state.chartUpdateEndTime,
chartUpdateStartTime: state.chartUpdateStartTime,
column_formats: state.datasource ? state.datasource.column_formats : null,
containerId: `slice-container-${state.form_data.slice_id}`,
datasource_type: state.datasource_type,
form_data: state.form_data,
isStarred: state.isStarred,
alert: state.chartAlert,
table_name: state.viz.form_data.datasource_name,
queryResponse: state.queryResponse,
datasource_type: state.datasource_type,
slice_id: state.form_data.slice_id,
slice_name: state.form_data.slice_name,
table_name: state.form_data.datasource_name,
viz_type: state.form_data.viz_type,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ class ControlPanelsContainer extends React.Component {
this.getFieldData = this.getFieldData.bind(this);
this.removeAlert = this.removeAlert.bind(this);
}
componentWillMount() {
const datasource_id = this.props.form_data.datasource;
const datasource_type = this.props.datasource_type;
if (datasource_id) {
this.props.actions.fetchDatasourceMetadata(datasource_id, datasource_type);
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.form_data.datasource !== this.props.form_data.datasource) {
if (nextProps.form_data.datasource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class ExploreViewContainer extends React.Component {
componentDidMount() {
window.addEventListener('resize', this.handleResize.bind(this));
this.runQuery();

const datasource_id = this.props.form_data.datasource_id;
const datasource_type = this.props.datasource_type;
if (datasource_id) {
this.props.actions.fetchDatasourceMetadata(datasource_id, datasource_type);
}
this.props.actions.fetchDatasources();
}

componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -156,7 +163,7 @@ function mapStateToProps(state) {
chartStatus: state.chartStatus,
datasource_type: state.datasource_type,
fields: state.fields,
form_data: state.viz.form_data,
form_data: state.form_data,
};
}

Expand Down
28 changes: 17 additions & 11 deletions superset/assets/javascripts/explorev2/components/SelectField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ const defaultProps = {
onChange: () => {},
};

export default class SelectField extends React.Component {
export default class SelectField extends React.PureComponent {
constructor(props) {
super(props);
this.state = { options: this.getOptions() };
this.state = { options: this.getOptions(props) };
this.onChange = this.onChange.bind(this);
this.renderOption = this.renderOption.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.choices !== this.props.choices) {
const options = this.getOptions(nextProps);
this.setState({ options });
}
}
onChange(opt) {
let optionValue = opt ? opt.value : null;
// if multi, return options values as an array
Expand All @@ -40,8 +46,8 @@ export default class SelectField extends React.Component {
}
this.props.onChange(optionValue);
}
getOptions() {
const options = this.props.choices.map((c) => {
getOptions(props) {
const options = props.choices.map((c) => {
const label = c.length > 1 ? c[1] : c[0];
const newOptions = {
value: c[0],
Expand All @@ -50,19 +56,19 @@ export default class SelectField extends React.Component {
if (c[2]) newOptions.imgSrc = c[2];
return newOptions;
});
if (this.props.freeForm) {
if (props.freeForm) {
// For FreeFormSelect, insert value into options if not exist
const values = this.props.choices.map((c) => c[0]);
if (this.props.value) {
if (typeof this.props.value === 'object') {
this.props.value.forEach((v) => {
const values = props.choices.map((c) => c[0]);
if (props.value) {
if (typeof props.value === 'object') {
props.value.forEach((v) => {
if (values.indexOf(v) === -1) {
options.push({ value: v, label: v });
}
});
} else {
if (values.indexOf(this.props.value) === -1) {
options.push({ value: this.props.value, label: this.props.value });
if (values.indexOf(props.value) === -1) {
options.push({ value: props.value, label: props.value });
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions superset/assets/javascripts/explorev2/exploreUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@ function formatFilters(filters) {
return params;
}

export function parseFilters(form_data, prefix = 'flt') {
const filters = [];
for (let i = 0; i <= 10; i++) {
if (form_data[`${prefix}_col_${i}`] && form_data[`${prefix}_op_${i}`]) {
filters.push({
prefix,
col: form_data[`${prefix}_col_${i}`],
op: form_data[`${prefix}_op_${i}`],
value: form_data[`${prefix}_eq_${i}`],
});
}
/* eslint no-param-reassign: 0 */
delete form_data[`${prefix}_col_${i}`];
delete form_data[`${prefix}_op_${i}`];
delete form_data[`${prefix}_eq_${i}`];
}
return filters;
}

export function getFilters(form_data, datasource_type) {
if (datasource_type === 'table') {
return parseFilters(form_data);
}
return parseFilters(form_data).concat(parseFilters(form_data, 'having'));
}

export function getParamObject(form_data, datasource_type, saveNewSlice) {
const data = {
// V2 tag temporarily for updating url
Expand Down
Loading

0 comments on commit 32a6df4

Please sign in to comment.