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

Allow user update slice title in visualize flow #3466

Merged
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
8 changes: 8 additions & 0 deletions superset/assets/javascripts/components/EditableTitle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class EditableTitle extends React.PureComponent {
isEditing: false,
});

if (!this.state.title.length) {
this.setState({
title: this.state.lastTitle,
});

return;
}

if (this.state.lastTitle !== this.state.title) {
this.setState({
lastTitle: this.state.title,
Expand Down
5 changes: 5 additions & 0 deletions superset/assets/javascripts/explore/actions/exploreActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,8 @@ export const RENDER_TRIGGERED = 'RENDER_TRIGGERED';
export function renderTriggered() {
return { type: RENDER_TRIGGERED };
}

export const CREATE_NEW_SLICE = 'CREATE_NEW_SLICE';
export function createNewSlice(can_add, can_download, can_overwrite, slice, form_data) {
return { type: CREATE_NEW_SLICE, can_add, can_download, can_overwrite, slice, form_data };
}
19 changes: 13 additions & 6 deletions superset/assets/javascripts/explore/components/ChartContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,22 @@ class ChartContainer extends React.PureComponent {
this.props.actions.runQuery(this.props.formData, true, this.props.timeout);
}

updateChartTitle(newTitle) {
updateChartTitleOrSaveSlice(newTitle) {
const isNewSlice = !this.props.slice;
const params = {
slice_name: newTitle,
action: 'overwrite',
action: isNewSlice ? 'saveas' : 'overwrite',
};
const saveUrl = getExploreUrl(this.props.formData, 'base', false, null, params);
this.props.actions.saveSlice(saveUrl)
.then(() => {
this.props.actions.updateChartTitle(newTitle);
.then((data) => {
if (isNewSlice) {
this.props.actions.createNewSlice(
data.can_add, data.can_download, data.can_overwrite,
data.slice, data.form_data);
} else {
this.props.actions.updateChartTitle(newTitle);
}
});
}

Expand Down Expand Up @@ -262,8 +269,8 @@ class ChartContainer extends React.PureComponent {
>
<EditableTitle
title={this.renderChartTitle()}
canEdit={this.props.can_overwrite}
onSaveTitle={this.updateChartTitle.bind(this)}
canEdit={!this.props.slice || this.props.can_overwrite}
onSaveTitle={this.updateChartTitleOrSaveSlice.bind(this)}
/>

{this.props.slice &&
Expand Down
3 changes: 2 additions & 1 deletion superset/assets/javascripts/explore/components/SaveModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class SaveModal extends React.Component {
this.props.actions.saveSlice(saveUrl)
.then((data) => {
// Go to new slice url or dashboard url
window.location = data;
window.location = data.slice.slice_url;
});
this.props.onHide();
}
Expand Down Expand Up @@ -184,6 +184,7 @@ class SaveModal extends React.Component {
Add slice to existing dashboard
</Radio>
<Select
className="save-modal-selector"
options={this.props.dashboards}
onChange={this.onChange.bind(this, 'saveToDashboardId')}
autoSize={false}
Expand Down
3 changes: 3 additions & 0 deletions superset/assets/javascripts/explore/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,6 @@
right: 0;
top: 12px;
}
.save-modal-selector {
margin: 10px 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ export default function exploreReducer(state = {}, action) {
[actions.RENDER_TRIGGERED]() {
return Object.assign({}, state, { triggerRender: false });
},
[actions.CREATE_NEW_SLICE]() {
return Object.assign({}, state, {
slice: action.slice,
controls: getControlsState(state, action.form_data),
can_add: action.can_add,
can_download: action.can_download,
can_overwrite: action.can_overwrite,
});
},
};
if (action.type in actionHandlers) {
return actionHandlers[action.type]();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@ describe('EditableTitle', () => {
// no change
expect(callback.callCount).to.equal(0);
});
it('should not save empty title', () => {
editableWrapper.setState({ title: '' });
editableWrapper.find('input').simulate('blur');
expect(editableWrapper.find('input').props().type).to.equal('button');
expect(editableWrapper.find('input').props().value).to.equal('my title');
expect(callback.callCount).to.equal(0);
});
});
});
14 changes: 11 additions & 3 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,17 +1067,19 @@ def explore(self, datasource_type, datasource_id):
slice_overwrite_perm = is_owner(slc, g.user)
slice_download_perm = self.can_access('can_download', 'SliceModelView')

form_data['datasource'] = str(datasource_id) + '__' + datasource_type

# handle save or overwrite
action = request.args.get('action')
if action in ('saveas', 'overwrite'):
return self.save_or_overwrite_slice(
request.args,
slc, slice_add_perm,
slice_overwrite_perm,
slice_download_perm,
datasource_id,
datasource_type)

form_data['datasource'] = str(datasource_id) + '__' + datasource_type
standalone = request.args.get("standalone") == "true"
bootstrap_data = {
"can_add": slice_add_perm,
Expand Down Expand Up @@ -1133,7 +1135,7 @@ def filter(self, datasource_type, datasource_id, column):
return json_success(payload)

def save_or_overwrite_slice(
self, args, slc, slice_add_perm, slice_overwrite_perm,
self, args, slc, slice_add_perm, slice_overwrite_perm, slice_download_perm,
datasource_id, datasource_type):
"""Save or overwrite a slice"""
slice_name = args.get('slice_name')
Expand Down Expand Up @@ -1188,7 +1190,13 @@ def save_or_overwrite_slice(
if request.args.get('goto_dash') == 'true':
return dash.url
else:
return slc.slice_url
return json_success(json.dumps({
"can_add": slice_add_perm,
"can_download": slice_download_perm,
"can_overwrite": is_owner(slc, g.user),
'form_data': form_data,
'slice': slc.data,
}))

def save_slice(self, slc):
session = db.session()
Expand Down