-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
fix(explore): fix chart save when dashboard deleted #21497
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ | |||||
* specific language governing permissions and limitations | ||||||
* under the License. | ||||||
*/ | ||||||
import rison from 'rison'; | ||||||
import { SupersetClient, t } from '@superset-ui/core'; | ||||||
import { addSuccessToast } from 'src/components/MessageToasts/actions'; | ||||||
import { buildV1ChartDataPayload } from '../exploreUtils'; | ||||||
|
@@ -66,6 +67,7 @@ export function removeSaveModalAlert() { | |||||
export const getSlicePayload = ( | ||||||
sliceName, | ||||||
formDataWithNativeFilters, | ||||||
dashboards, | ||||||
owners, | ||||||
) => { | ||||||
const adhocFilters = Object.entries(formDataWithNativeFilters).reduce( | ||||||
|
@@ -78,6 +80,7 @@ export const getSlicePayload = ( | |||||
const formData = { | ||||||
...formDataWithNativeFilters, | ||||||
...adhocFilters, | ||||||
dashboards, | ||||||
}; | ||||||
|
||||||
const [datasourceId, datasourceType] = formData.datasource.split('__'); | ||||||
|
@@ -87,7 +90,7 @@ export const getSlicePayload = ( | |||||
viz_type: formData.viz_type, | ||||||
datasource_id: parseInt(datasourceId, 10), | ||||||
datasource_type: datasourceType, | ||||||
dashboards: formData.dashboards, | ||||||
dashboards, | ||||||
owners, | ||||||
query_context: JSON.stringify( | ||||||
buildV1ChartDataPayload({ | ||||||
|
@@ -142,7 +145,7 @@ const addToasts = (isNewSlice, sliceName, addedToDashboard) => { | |||||
|
||||||
// Update existing slice | ||||||
export const updateSlice = | ||||||
({ slice_id: sliceId, owners }, sliceName, addedToDashboard) => | ||||||
({ slice_id: sliceId, owners }, sliceName, dashboards, addedToDashboard) => | ||||||
async (dispatch, getState) => { | ||||||
const { | ||||||
explore: { | ||||||
|
@@ -152,7 +155,7 @@ export const updateSlice = | |||||
try { | ||||||
const response = await SupersetClient.put({ | ||||||
endpoint: `/api/v1/chart/${sliceId}`, | ||||||
jsonPayload: getSlicePayload(sliceName, formData, owners), | ||||||
jsonPayload: getSlicePayload(sliceName, formData, dashboards, owners), | ||||||
}); | ||||||
|
||||||
dispatch(saveSliceSuccess()); | ||||||
|
@@ -166,7 +169,7 @@ export const updateSlice = | |||||
|
||||||
// Create new slice | ||||||
export const createSlice = | ||||||
(sliceName, addedToDashboard) => async (dispatch, getState) => { | ||||||
(sliceName, dashboards, addedToDashboard) => async (dispatch, getState) => { | ||||||
const { | ||||||
explore: { | ||||||
form_data: { url_params: _, ...formData }, | ||||||
|
@@ -175,7 +178,7 @@ export const createSlice = | |||||
try { | ||||||
const response = await SupersetClient.post({ | ||||||
endpoint: `/api/v1/chart/`, | ||||||
jsonPayload: getSlicePayload(sliceName, formData), | ||||||
jsonPayload: getSlicePayload(sliceName, formData, dashboards), | ||||||
}); | ||||||
|
||||||
dispatch(saveSliceSuccess()); | ||||||
|
@@ -215,3 +218,23 @@ export const getDashboard = dashboardId => async dispatch => { | |||||
throw error; | ||||||
} | ||||||
}; | ||||||
|
||||||
// Get dashboards the slice is added to | ||||||
export const getSliceDashboards = slice => async dispatch => { | ||||||
if (slice) { | ||||||
try { | ||||||
const response = await SupersetClient.get({ | ||||||
endpoint: `/api/v1/chart/${slice.slice_id}?q=${rison.encode({ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I check above that |
||||||
columns: ['dashboards.id'], | ||||||
})}`, | ||||||
}); | ||||||
|
||||||
return response.json.result.dashboards.map(({ id }) => id); | ||||||
} catch (error) { | ||||||
dispatch(saveSliceFailed()); | ||||||
throw error; | ||||||
} | ||||||
} | ||||||
|
||||||
return []; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to remove the |
||||||
}; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -49,7 +49,6 @@ interface SaveModalProps extends RouteComponentProps { | |||||||||||||||||||||
slice?: Record<string, any>; | ||||||||||||||||||||||
datasource?: Record<string, any>; | ||||||||||||||||||||||
dashboardId: '' | number | null; | ||||||||||||||||||||||
sliceDashboards: number[]; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
type ActionType = 'overwrite' | 'saveas'; | ||||||||||||||||||||||
|
@@ -183,6 +182,14 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> { | |||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Get chart dashboards | ||||||||||||||||||||||
let sliceDashboards: number[]; | ||||||||||||||||||||||
promise = promise | ||||||||||||||||||||||
.then(() => this.props.actions.getSliceDashboards(this.props.slice)) | ||||||||||||||||||||||
.then(dashboards => { | ||||||||||||||||||||||
sliceDashboards = dashboards; | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
|
||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I didn't realize this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually going to leave out turning the promises into |
||||||||||||||||||||||
let dashboard: DashboardGetResponse | null = null; | ||||||||||||||||||||||
if (this.state.newDashboardName || this.state.saveToDashboardId) { | ||||||||||||||||||||||
let saveToDashboardId = this.state.saveToDashboardId || null; | ||||||||||||||||||||||
|
@@ -200,12 +207,13 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> { | |||||||||||||||||||||
.then(() => this.props.actions.getDashboard(saveToDashboardId)) | ||||||||||||||||||||||
.then((response: { result: DashboardGetResponse }) => { | ||||||||||||||||||||||
dashboard = response.result; | ||||||||||||||||||||||
const dashboards = new Set<number>(this.props.sliceDashboards); | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits: sliceDashboards = originalDashboards.includes(dashboard.id) ? originalDashboards : [...originalDashboards, dashboard.id], There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much cleaner, thanks. |
||||||||||||||||||||||
dashboards.add(dashboard.id); | ||||||||||||||||||||||
sliceDashboards = sliceDashboards.includes(dashboard.id) | ||||||||||||||||||||||
? sliceDashboards | ||||||||||||||||||||||
: [...sliceDashboards, dashboard.id]; | ||||||||||||||||||||||
const { url_params, ...formData } = this.props.form_data || {}; | ||||||||||||||||||||||
this.props.actions.setFormData({ | ||||||||||||||||||||||
...formData, | ||||||||||||||||||||||
dashboards: Array.from(dashboards), | ||||||||||||||||||||||
dashboards: sliceDashboards, | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
@@ -216,6 +224,7 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> { | |||||||||||||||||||||
this.props.actions.updateSlice( | ||||||||||||||||||||||
this.props.slice, | ||||||||||||||||||||||
this.state.newSliceName, | ||||||||||||||||||||||
sliceDashboards, | ||||||||||||||||||||||
dashboard | ||||||||||||||||||||||
? { | ||||||||||||||||||||||
title: dashboard.dashboard_title, | ||||||||||||||||||||||
|
@@ -228,6 +237,7 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> { | |||||||||||||||||||||
promise = promise.then(() => | ||||||||||||||||||||||
this.props.actions.createSlice( | ||||||||||||||||||||||
this.state.newSliceName, | ||||||||||||||||||||||
sliceDashboards, | ||||||||||||||||||||||
dashboard | ||||||||||||||||||||||
? { | ||||||||||||||||||||||
title: dashboard.dashboard_title, | ||||||||||||||||||||||
|
@@ -276,7 +286,7 @@ class SaveModal extends React.Component<SaveModalProps, SaveModalState> { | |||||||||||||||||||||
type="warning" | ||||||||||||||||||||||
message={ | ||||||||||||||||||||||
<> | ||||||||||||||||||||||
{this.state.alert ? this.state.alert : this.props.alert} | ||||||||||||||||||||||
{this.state.alert || this.props.alert} | ||||||||||||||||||||||
<i | ||||||||||||||||||||||
role="button" | ||||||||||||||||||||||
aria-label="Remove alert" | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
if
clause is redundant because thetry...cache
handled exceptionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intention was for the
try...catch
clause to just handle request errors or other unexpected errors. Theif
clause is there because this function might be called with noslice
param, in the case of a chart that hasn't yet been saved, and I wanted it to return[]
in that case as part of normal, non-exception behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if there is no
slice
param, the request will also raise an exception, so thecatch
statement alway catches an exception although an invalid input param.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But, I don't want to send the request at all if there's no
slice
param, I just want to return an empty array.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you don't want to send a request, you shouldn't call this action. e.g.:
just a personal suggestion, optimize it for the future. thanks for the fixing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that makes sense, thanks.