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/10/18 17:26:22 UTC

[GitHub] [superset] ktmud commented on a diff in pull request #21819: feat(dashboard): confirm overwrite to prevent unintended changes

ktmud commented on code in PR #21819:
URL: https://github.com/apache/superset/pull/21819#discussion_r998471022


##########
superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts:
##########
@@ -48,6 +48,7 @@ export enum FeatureFlag {
   ENABLE_JAVASCRIPT_CONTROLS = 'ENABLE_JAVASCRIPT_CONTROLS',
   ENABLE_TEMPLATE_PROCESSING = 'ENABLE_TEMPLATE_PROCESSING',
   ENABLE_TEMPLATE_REMOVE_FILTERS = 'ENABLE_TEMPLATE_REMOVE_FILTERS',
+  ENABLE_CONFIRM_OVERWRITE_DASHBOARD_METADATA = 'ENABLE_CONFIRM_OVERWRITE_DASHBOARD_METADATA',

Review Comment:
   ```suggestion
     CONFIRM_DASHBOARD_DIFF = 'CONFIRM_DASHBOARD_DIFF',
   ```



##########
superset-frontend/spec/fixtures/mockDashboardState.js:
##########
@@ -30,3 +31,34 @@ export default {
   focusedFilterField: null,
   refreshFrequency: 0,
 };
+
+export const overwriteConfirmMetadata = {
+  latestUpdatedAt: '2022-10-07T16:35:30.924212',
+  latestUpdatedBy: 'Superset Admin',

Review Comment:
   ```suggestion
     updatedAt: '2022-10-07T16:35:30.924212',
     updatedBy: 'Superset Admin'
   ```
   
   Nit: I think this is clear enough.



##########
superset-frontend/src/dashboard/actions/dashboardState.js:
##########
@@ -340,34 +355,87 @@ export function saveDashboardRequest(data, id, saveType) {
       dispatch(addDangerToast(errorText));
     };
 
-    if (saveType === SAVE_TYPE_OVERWRITE) {
+    if (
+      [SAVE_TYPE_OVERWRITE, SAVE_TYPE_OVERWRITE_CONFIRMED].includes(saveType)
+    ) {
       let chartConfiguration = {};
       if (isFeatureEnabled(FeatureFlag.DASHBOARD_CROSS_FILTERS)) {
         chartConfiguration = handleChartConfiguration();
       }
-      const updatedDashboard = {
-        certified_by: cleanedData.certified_by,
-        certification_details: cleanedData.certification_details,
-        css: cleanedData.css,
-        dashboard_title: cleanedData.dashboard_title,
-        slug: cleanedData.slug,
-        owners: cleanedData.owners,
-        roles: cleanedData.roles,
-        json_metadata: safeStringify({
-          ...(cleanedData?.metadata || {}),
-          default_filters: safeStringify(serializedFilters),
-          filter_scopes: serializedFilterScopes,
-          chart_configuration: chartConfiguration,
-        }),
-      };
+      const updatedDashboard =
+        saveType === SAVE_TYPE_OVERWRITE_CONFIRMED
+          ? data
+          : {
+              certified_by: cleanedData.certified_by,
+              certification_details: cleanedData.certification_details,
+              css: cleanedData.css,
+              dashboard_title: cleanedData.dashboard_title,
+              slug: cleanedData.slug,
+              owners: cleanedData.owners,
+              roles: cleanedData.roles,
+              json_metadata: safeStringify({
+                ...(cleanedData?.metadata || {}),
+                default_filters: safeStringify(serializedFilters),
+                filter_scopes: serializedFilterScopes,
+                chart_configuration: chartConfiguration,
+              }),
+            };
+
+      const updateDashboard = () =>
+        SupersetClient.put({
+          endpoint: `/api/v1/dashboard/${id}`,
+          headers: { 'Content-Type': 'application/json' },
+          body: JSON.stringify(updatedDashboard),
+        })
+          .then(response => onUpdateSuccess(response))
+          .catch(response => onError(response));
+      return new Promise((resolve, reject) => {
+        if (
+          !isFeatureEnabled(
+            FeatureFlag.ENABLE_CONFIRM_OVERWRITE_DASHBOARD_METADATA,
+          ) ||
+          saveType === SAVE_TYPE_OVERWRITE_CONFIRMED
+        ) {
+          // skip overwrite precheck
+          resolve();
+          return;
+        }
 
-      return SupersetClient.put({

Review Comment:
   If this dispatch function returns a promise, can we just rewrite it with `async/await`?



##########
superset-frontend/spec/fixtures/mockDashboardState.js:
##########
@@ -30,3 +31,34 @@ export default {
   focusedFilterField: null,
   refreshFrequency: 0,
 };
+
+export const overwriteConfirmMetadata = {
+  latestUpdatedAt: '2022-10-07T16:35:30.924212',
+  latestUpdatedBy: 'Superset Admin',
+  overwriteConfirmItems: [
+    {
+      keyPath: 'css',
+      oldValue: '',
+      newValue:
+        ".navbar {\n    transition: opacity 0.5s ease;\n    opacity: 0.05;\n}\n.navbar:hover {\n    opacity: 1;\n}\n.chart-header .header{\n    font-weight: @font-weight-normal;\n    font-size: 12px;\n}\n/*\nvar bnbColors = [\n    //rausch    hackb      kazan      babu      lima        beach     tirol\n    '#ff5a5f', '#7b0051', '#007A87', '#00d1c1', '#8ce071', '#ffb400', '#b4a76c',\n    '#ff8083', '#cc0086', '#00a1b3', '#00ffeb', '#bbedab', '#ffd266', '#cbc29a',\n    '#ff3339', '#ff1ab1', '#005c66', '#00b3a5', '#55d12e', '#b37e00', '#988b4e',\n ];\n*/\n",

Review Comment:
   If this file is manually generated, could we use template literals to display multiline strings instead?



##########
superset-frontend/src/dashboard/components/OverwriteConfirm/OverwriteConfirmModal.tsx:
##########
@@ -0,0 +1,212 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useMemo, useCallback, RefObject, createRef } from 'react';
+import moment from 'moment';
+import { useDispatch } from 'react-redux';
+import ReactDiffViewer from 'react-diff-viewer';
+import { useInView } from 'react-intersection-observer';
+import Modal from 'src/components/Modal';
+import Button from 'src/components/Button';
+import { DashboardState } from 'src/dashboard/types';
+import {
+  saveDashboardRequest,
+  setOverrideConfirm,
+} from 'src/dashboard/actions/dashboardState';
+import { t, styled } from '@superset-ui/core';
+import { SAVE_TYPE_OVERWRITE_CONFIRMED } from 'src/dashboard/util/constants';
+
+const STICKY_HEADER_TOP = 16;
+const STICKY_HEADER_HEIGHT = 32;
+
+const StyledTitle = styled.h2`
+  ${({ theme }) => `
+     color:  ${theme.colors.grayscale.dark1}
+   `}
+`;
+
+const StyledEditor = styled.div`
+  ${({ theme }) => `
+     table {
+       border: 1px ${theme.colors.grayscale.light2} solid;
+     }
+     pre {
+       font-size: 11px;
+       padding: 0px;
+       background-color: transparent;
+       border: 0px;
+       line-height: 110%;
+     }
+   `}
+`;
+
+const StackableHeader = styled(Button)<{ top: number }>`
+  ${({ theme, top }) => `
+     position: sticky;
+     top: ${top}px;
+     background-color: ${theme.colors.grayscale.light5};
+     margin: 0px;
+     padding: 8px 4px;
+     z-index: 1;
+     border: 0px;
+     border-radius: 0px;
+     width: 100%;
+     justify-content: flex-start;
+     border-bottom: 1px ${theme.colors.grayscale.light1} solid;
+     &::before {
+       display: inline-block;
+       position: relative;
+       opacity: 1;
+       content: "\\00BB";
+     }
+   `}
+`;
+
+const StyledBottom = styled.div<{ inView: boolean }>`
+  ${({ inView }) => `
+     margin: 8px auto;
+     text-align: center;
+     opacity: ${inView ? 0 : 1};
+  `}
+`;
+
+type Props = {
+  overwriteConfirmMetadata: DashboardState['overwriteConfirmMetadata'];
+};
+
+const OverrideConfirmModal = ({ overwriteConfirmMetadata }: Props) => {
+  const [bottomRef, hasReviewed] = useInView({ triggerOnce: true });
+  const dispatch = useDispatch();
+  const onHide = useCallback(
+    () => dispatch(setOverrideConfirm(undefined)),
+    [dispatch],
+  );
+  const anchors = useMemo<RefObject<HTMLDivElement>[]>(
+    () =>
+      overwriteConfirmMetadata
+        ? overwriteConfirmMetadata.overwriteConfirmItems.map(() =>
+            createRef<HTMLDivElement>(),
+          )
+        : [],
+    [overwriteConfirmMetadata],
+  );
+  const onAnchorClicked = useCallback(
+    (index: number) => {
+      anchors[index]?.current?.scrollIntoView({ behavior: 'smooth' });
+    },
+    [anchors],
+  );
+  const onConfirmOverwrite = useCallback(() => {
+    if (overwriteConfirmMetadata) {
+      dispatch(
+        saveDashboardRequest(
+          overwriteConfirmMetadata.data,
+          overwriteConfirmMetadata.dashboardId,
+          SAVE_TYPE_OVERWRITE_CONFIRMED,
+        ),
+      );
+    }
+  }, [dispatch, overwriteConfirmMetadata]);
+
+  return (
+    <Modal
+      width="auto"
+      height="50vh"
+      show={Boolean(overwriteConfirmMetadata)}
+      title={t('Confirm overwrite')}
+      footer={
+        <>
+          {t('* Scroll down to the bottom to complete the review. ')}

Review Comment:
   ```suggestion
             {t('Scroll down to the bottom to complete the review. ')}
   ```
   
   Not sure if `*` is needed.



##########
superset-frontend/src/dashboard/components/OverwriteConfirm/OverwriteConfirmModal.tsx:
##########
@@ -0,0 +1,212 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useMemo, useCallback, RefObject, createRef } from 'react';
+import moment from 'moment';
+import { useDispatch } from 'react-redux';
+import ReactDiffViewer from 'react-diff-viewer';
+import { useInView } from 'react-intersection-observer';
+import Modal from 'src/components/Modal';
+import Button from 'src/components/Button';
+import { DashboardState } from 'src/dashboard/types';
+import {
+  saveDashboardRequest,
+  setOverrideConfirm,
+} from 'src/dashboard/actions/dashboardState';
+import { t, styled } from '@superset-ui/core';
+import { SAVE_TYPE_OVERWRITE_CONFIRMED } from 'src/dashboard/util/constants';
+
+const STICKY_HEADER_TOP = 16;
+const STICKY_HEADER_HEIGHT = 32;
+
+const StyledTitle = styled.h2`
+  ${({ theme }) => `
+     color:  ${theme.colors.grayscale.dark1}
+   `}
+`;
+
+const StyledEditor = styled.div`
+  ${({ theme }) => `
+     table {
+       border: 1px ${theme.colors.grayscale.light2} solid;
+     }
+     pre {
+       font-size: 11px;
+       padding: 0px;
+       background-color: transparent;
+       border: 0px;
+       line-height: 110%;
+     }
+   `}
+`;
+
+const StackableHeader = styled(Button)<{ top: number }>`
+  ${({ theme, top }) => `
+     position: sticky;
+     top: ${top}px;
+     background-color: ${theme.colors.grayscale.light5};
+     margin: 0px;
+     padding: 8px 4px;
+     z-index: 1;
+     border: 0px;
+     border-radius: 0px;
+     width: 100%;
+     justify-content: flex-start;
+     border-bottom: 1px ${theme.colors.grayscale.light1} solid;
+     &::before {
+       display: inline-block;
+       position: relative;
+       opacity: 1;
+       content: "\\00BB";
+     }
+   `}
+`;
+
+const StyledBottom = styled.div<{ inView: boolean }>`
+  ${({ inView }) => `
+     margin: 8px auto;
+     text-align: center;
+     opacity: ${inView ? 0 : 1};
+  `}
+`;
+
+type Props = {
+  overwriteConfirmMetadata: DashboardState['overwriteConfirmMetadata'];
+};
+
+const OverrideConfirmModal = ({ overwriteConfirmMetadata }: Props) => {
+  const [bottomRef, hasReviewed] = useInView({ triggerOnce: true });
+  const dispatch = useDispatch();
+  const onHide = useCallback(
+    () => dispatch(setOverrideConfirm(undefined)),
+    [dispatch],
+  );
+  const anchors = useMemo<RefObject<HTMLDivElement>[]>(
+    () =>
+      overwriteConfirmMetadata
+        ? overwriteConfirmMetadata.overwriteConfirmItems.map(() =>
+            createRef<HTMLDivElement>(),
+          )
+        : [],
+    [overwriteConfirmMetadata],
+  );
+  const onAnchorClicked = useCallback(
+    (index: number) => {
+      anchors[index]?.current?.scrollIntoView({ behavior: 'smooth' });
+    },
+    [anchors],
+  );
+  const onConfirmOverwrite = useCallback(() => {
+    if (overwriteConfirmMetadata) {
+      dispatch(
+        saveDashboardRequest(
+          overwriteConfirmMetadata.data,
+          overwriteConfirmMetadata.dashboardId,
+          SAVE_TYPE_OVERWRITE_CONFIRMED,
+        ),
+      );
+    }
+  }, [dispatch, overwriteConfirmMetadata]);
+
+  return (
+    <Modal
+      width="auto"
+      height="50vh"
+      show={Boolean(overwriteConfirmMetadata)}
+      title={t('Confirm overwrite')}
+      footer={
+        <>
+          {t('* Scroll down to the bottom to complete the review. ')}
+          <Button
+            htmlType="button"
+            buttonSize="small"
+            onClick={onHide}
+            data-test="override-confirm-modal-cancel-button"
+            cta
+          >
+            {t('No')}
+          </Button>
+          <Button
+            data-test="overwrite-confirm-save-button"
+            htmlType="button"
+            buttonSize="small"
+            cta
+            buttonStyle="primary"
+            onClick={onConfirmOverwrite}
+            disabled={!hasReviewed}

Review Comment:
   Add a tooltip to explain the disabled state?



##########
superset-frontend/src/dashboard/components/OverwriteConfirm/OverwriteConfirmModal.tsx:
##########
@@ -0,0 +1,212 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useMemo, useCallback, RefObject, createRef } from 'react';
+import moment from 'moment';
+import { useDispatch } from 'react-redux';
+import ReactDiffViewer from 'react-diff-viewer';
+import { useInView } from 'react-intersection-observer';
+import Modal from 'src/components/Modal';
+import Button from 'src/components/Button';
+import { DashboardState } from 'src/dashboard/types';
+import {
+  saveDashboardRequest,
+  setOverrideConfirm,
+} from 'src/dashboard/actions/dashboardState';
+import { t, styled } from '@superset-ui/core';
+import { SAVE_TYPE_OVERWRITE_CONFIRMED } from 'src/dashboard/util/constants';
+
+const STICKY_HEADER_TOP = 16;
+const STICKY_HEADER_HEIGHT = 32;
+
+const StyledTitle = styled.h2`
+  ${({ theme }) => `
+     color:  ${theme.colors.grayscale.dark1}
+   `}
+`;
+
+const StyledEditor = styled.div`
+  ${({ theme }) => `
+     table {
+       border: 1px ${theme.colors.grayscale.light2} solid;
+     }
+     pre {
+       font-size: 11px;
+       padding: 0px;
+       background-color: transparent;
+       border: 0px;
+       line-height: 110%;
+     }
+   `}
+`;
+
+const StackableHeader = styled(Button)<{ top: number }>`
+  ${({ theme, top }) => `
+     position: sticky;
+     top: ${top}px;
+     background-color: ${theme.colors.grayscale.light5};
+     margin: 0px;
+     padding: 8px 4px;
+     z-index: 1;
+     border: 0px;
+     border-radius: 0px;
+     width: 100%;
+     justify-content: flex-start;
+     border-bottom: 1px ${theme.colors.grayscale.light1} solid;
+     &::before {
+       display: inline-block;
+       position: relative;
+       opacity: 1;
+       content: "\\00BB";
+     }
+   `}
+`;
+
+const StyledBottom = styled.div<{ inView: boolean }>`
+  ${({ inView }) => `
+     margin: 8px auto;
+     text-align: center;
+     opacity: ${inView ? 0 : 1};
+  `}
+`;
+
+type Props = {
+  overwriteConfirmMetadata: DashboardState['overwriteConfirmMetadata'];
+};
+
+const OverrideConfirmModal = ({ overwriteConfirmMetadata }: Props) => {
+  const [bottomRef, hasReviewed] = useInView({ triggerOnce: true });
+  const dispatch = useDispatch();
+  const onHide = useCallback(
+    () => dispatch(setOverrideConfirm(undefined)),
+    [dispatch],
+  );
+  const anchors = useMemo<RefObject<HTMLDivElement>[]>(
+    () =>
+      overwriteConfirmMetadata
+        ? overwriteConfirmMetadata.overwriteConfirmItems.map(() =>
+            createRef<HTMLDivElement>(),
+          )
+        : [],
+    [overwriteConfirmMetadata],
+  );
+  const onAnchorClicked = useCallback(
+    (index: number) => {
+      anchors[index]?.current?.scrollIntoView({ behavior: 'smooth' });
+    },
+    [anchors],
+  );
+  const onConfirmOverwrite = useCallback(() => {
+    if (overwriteConfirmMetadata) {
+      dispatch(
+        saveDashboardRequest(
+          overwriteConfirmMetadata.data,
+          overwriteConfirmMetadata.dashboardId,
+          SAVE_TYPE_OVERWRITE_CONFIRMED,
+        ),
+      );
+    }
+  }, [dispatch, overwriteConfirmMetadata]);
+
+  return (
+    <Modal
+      width="auto"
+      height="50vh"
+      show={Boolean(overwriteConfirmMetadata)}
+      title={t('Confirm overwrite')}
+      footer={
+        <>
+          {t('* Scroll down to the bottom to complete the review. ')}
+          <Button
+            htmlType="button"
+            buttonSize="small"
+            onClick={onHide}
+            data-test="override-confirm-modal-cancel-button"
+            cta
+          >
+            {t('No')}
+          </Button>
+          <Button
+            data-test="overwrite-confirm-save-button"
+            htmlType="button"
+            buttonSize="small"
+            cta
+            buttonStyle="primary"
+            onClick={onConfirmOverwrite}
+            disabled={!hasReviewed}
+          >
+            {t('Yes, overwrite changes')}
+          </Button>
+        </>
+      }
+      onHide={onHide}
+    >
+      <>

Review Comment:
   Is this fragment needed?



##########
superset-frontend/src/dashboard/components/OverwriteConfirm/OverwriteConfirmModal.tsx:
##########
@@ -0,0 +1,212 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import React, { useMemo, useCallback, RefObject, createRef } from 'react';
+import moment from 'moment';
+import { useDispatch } from 'react-redux';
+import ReactDiffViewer from 'react-diff-viewer';
+import { useInView } from 'react-intersection-observer';
+import Modal from 'src/components/Modal';
+import Button from 'src/components/Button';
+import { DashboardState } from 'src/dashboard/types';
+import {
+  saveDashboardRequest,
+  setOverrideConfirm,
+} from 'src/dashboard/actions/dashboardState';
+import { t, styled } from '@superset-ui/core';
+import { SAVE_TYPE_OVERWRITE_CONFIRMED } from 'src/dashboard/util/constants';
+
+const STICKY_HEADER_TOP = 16;
+const STICKY_HEADER_HEIGHT = 32;
+
+const StyledTitle = styled.h2`
+  ${({ theme }) => `
+     color:  ${theme.colors.grayscale.dark1}
+   `}
+`;
+
+const StyledEditor = styled.div`
+  ${({ theme }) => `
+     table {
+       border: 1px ${theme.colors.grayscale.light2} solid;
+     }
+     pre {
+       font-size: 11px;
+       padding: 0px;
+       background-color: transparent;
+       border: 0px;
+       line-height: 110%;
+     }
+   `}
+`;
+
+const StackableHeader = styled(Button)<{ top: number }>`

Review Comment:
   This header is currently rendered as all caps (probably inherited style from `Button`). Is that what we want?



-- 
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