/* eslint camel-case: 0 */ import React, { PropTypes } from 'react'; import $ from 'jquery'; import { Modal, Alert, Button, Radio } from 'react-bootstrap'; import Select from 'react-select'; import { connect } from 'react-redux'; import { getParamObject } from '../../modules/utils.js'; const propTypes = { can_edit: PropTypes.bool, onHide: PropTypes.func.isRequired, actions: PropTypes.object.isRequired, form_data: PropTypes.object, datasource_type: PropTypes.string.isRequired, user_id: PropTypes.string.isRequired, dashboards: PropTypes.array.isRequired, alert: PropTypes.string, }; class SaveModal extends React.Component { constructor(props) { super(props); this.state = { saveToDashboardId: null, newDashboardName: '', newSliceName: '', dashboards: [], alert: null, action: 'overwrite', addToDash: 'noSave', }; } componentDidMount() { this.props.actions.fetchDashboards(this.props.user_id); } onChange(name, event) { switch (name) { case 'newSliceName': this.setState({ newSliceName: event.target.value }); break; case 'saveToDashboardId': this.setState({ saveToDashboardId: event.value }); this.changeDash('existing'); break; case 'newDashboardName': this.setState({ newDashboardName: event.target.value }); break; default: break; } } changeAction(action) { this.setState({ action }); } changeDash(dash) { this.setState({ addToDash: dash }); } saveOrOverwrite(gotodash) { this.setState({ alert: null }); this.props.actions.removeSaveModalAlert(); const params = getParamObject(this.props.form_data, this.props.datasource_type); const sliceParams = {}; params.datasource_name = this.props.form_data.datasource_name; let sliceName = null; sliceParams.action = this.state.action; if (sliceParams.action === 'saveas') { sliceName = this.state.newSliceName; if (sliceName === '') { this.setState({ alert: 'Please enter a slice name' }); return; } sliceParams.slice_name = sliceName; } else { sliceParams.slice_name = this.props.form_data.slice_name; } const addToDash = this.state.addToDash; sliceParams.add_to_dash = addToDash; let dashboard = null; switch (addToDash) { case ('existing'): dashboard = this.state.saveToDashboardId; if (!dashboard) { this.setState({ alert: 'Please select a dashboard' }); return; } sliceParams.save_to_dashboard_id = dashboard; break; case ('new'): dashboard = this.state.newDashboardName; if (dashboard === '') { this.setState({ alert: 'Please enter a dashboard name' }); return; } sliceParams.new_dashboard_name = dashboard; break; default: dashboard = null; } sliceParams.goto_dash = gotodash; const baseUrl = '/superset/explore/' + `${this.props.datasource_type}/${this.props.form_data.datasource}/`; const saveUrl = `${baseUrl}?${$.param(params, true)}&${$.param(sliceParams, true)}`; this.props.actions.saveSlice(saveUrl); this.props.onHide(); } removeAlert() { if (this.props.alert) { this.props.actions.removeSaveModalAlert(); } this.setState({ alert: null }); } render() { return ( Save A Slice {(this.state.alert || this.props.alert) && {this.state.alert ? this.state.alert : this.props.alert} } {`Overwrite slice ${this.props.form_data.slice_name}`} Save as  

Do not add to a dashboard Add slice to existing dashboard
); } } SaveModal.propTypes = propTypes; function mapStateToProps(state) { return { can_edit: state.can_edit, user_id: state.user_id, dashboards: state.dashboards, alert: state.saveModalAlert, }; } export { SaveModal }; export default connect(mapStateToProps, () => ({}))(SaveModal);