You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/07/08 16:59:46 UTC
[GitHub] [superset] codyml commented on a diff in pull request #20498: chore(explore): Update chart save to use API endpoints
codyml commented on code in PR #20498:
URL: https://github.com/apache/superset/pull/20498#discussion_r916994954
##########
superset-frontend/src/explore/actions/saveModalActions.js:
##########
@@ -60,36 +61,143 @@ export function removeSaveModalAlert() {
return { type: REMOVE_SAVE_MODAL_ALERT };
}
-export function saveSlice(formData, requestParams) {
- return dispatch => {
- let url = getExploreUrl({
- formData,
- endpointType: 'base',
- force: false,
- curUrl: null,
- requestParams,
+export const getSlicePayload = (
+ sliceName,
+ formDataWithNativeFilters,
+ owners,
+) => {
+ const formData = {
+ ...formDataWithNativeFilters,
+ adhoc_filters: formDataWithNativeFilters.adhoc_filters?.filter(
+ f => !f.isExtra,
+ ),
+ };
+
+ const [datasourceId, datasourceType] = formData.datasource.split('__');
+ const payload = {
+ params: JSON.stringify(formData),
+ slice_name: sliceName,
+ viz_type: formData.viz_type,
+ datasource_id: parseInt(datasourceId, 10),
+ datasource_type: datasourceType,
+ dashboards: formData.dashboards,
+ owners,
+ query_context: JSON.stringify(
+ buildV1ChartDataPayload({
+ formData,
+ force: false,
+ resultFormat: 'json',
+ resultType: 'full',
+ setDataMask: null,
+ ownState: null,
+ }),
+ ),
+ };
+ return payload;
+};
+
+const addToasts = (isNewSlice, sliceName, addedToDashboard) => {
+ const toasts = [];
+ if (isNewSlice) {
+ toasts.push(addSuccessToast(t('Chart [%s] has been saved', sliceName)));
+ } else {
+ toasts.push(
+ addSuccessToast(t('Chart [%s] has been overwritten', sliceName)),
+ );
+ }
+
+ if (addedToDashboard) {
+ if (addedToDashboard.new) {
+ toasts.push(
+ addSuccessToast(
+ t(
+ 'Dashboard [%s] just got created and chart [%s] was added to it',
+ addedToDashboard.title,
+ sliceName,
+ ),
+ ),
+ );
+ } else {
+ toasts.push(
+ addSuccessToast(
+ t(
+ 'Chart [%s] was added to dashboard [%s]',
+ sliceName,
+ addedToDashboard.title,
+ ),
+ ),
+ );
+ }
+ }
+
+ return toasts;
+};
+
+// Update existing slice
+export const updateSlice =
+ ({ slice_id: sliceId, owners }, sliceName, formData, addedToDashboard) =>
+ async dispatch => {
+ try {
+ const response = await SupersetClient.put({
+ endpoint: `/api/v1/chart/${sliceId}`,
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(getSlicePayload(sliceName, formData, owners)),
+ });
+
+ dispatch(saveSliceSuccess());
+ addToasts(false, sliceName, addedToDashboard).map(dispatch);
+ return response.json;
+ } catch (error) {
+ dispatch(saveSliceFailed());
+ throw error;
+ }
+ };
+
+// Create new slice
+export const createSlice =
+ (sliceName, formData, addedToDashboard) => async dispatch => {
+ try {
+ const response = await SupersetClient.post({
+ endpoint: `/api/v1/chart/`,
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(getSlicePayload(sliceName, formData)),
Review Comment:
Oh thanks, had no idea!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org