You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by aa...@apache.org on 2022/02/09 23:03:22 UTC

[superset] 14/18: refactor: Reports code clean 10-29 (#17424)

This is an automated email from the ASF dual-hosted git repository.

aafghahi pushed a commit to branch refactorReports
in repository https://gitbox.apache.org/repos/asf/superset.git

commit d8f4733fd2b81d96aa488d689d9d8aa5991acbf2
Author: Lyndsi Kay Williams <55...@users.noreply.github.com>
AuthorDate: Fri Nov 19 11:31:20 2021 -0600

    refactor: Reports code clean 10-29 (#17424)
    
    * Add delete functionality
    
    * Report schema restructure progress
    
    * Fix lint
    
    * Removed console.log
---
 .../HeaderReportActionsDropdown/index.tsx          |   2 +-
 superset-frontend/src/reports/actions/reports.js   |   7 +-
 superset-frontend/src/reports/reducers/reports.js  | 145 +++++++++++++++++++--
 3 files changed, 140 insertions(+), 14 deletions(-)

diff --git a/superset-frontend/src/components/ReportModal/HeaderReportActionsDropdown/index.tsx b/superset-frontend/src/components/ReportModal/HeaderReportActionsDropdown/index.tsx
index 5eb4448..c85262f 100644
--- a/superset-frontend/src/components/ReportModal/HeaderReportActionsDropdown/index.tsx
+++ b/superset-frontend/src/components/ReportModal/HeaderReportActionsDropdown/index.tsx
@@ -155,7 +155,7 @@ export default function HeaderReportActionsDropDown({
           dashboardId={dashboardId}
           chart={chart}
         />
-        {reports ? (
+        {report ? (
           <>
             <NoAnimationDropdown
               // ref={ref}
diff --git a/superset-frontend/src/reports/actions/reports.js b/superset-frontend/src/reports/actions/reports.js
index 669c720..158d8a8 100644
--- a/superset-frontend/src/reports/actions/reports.js
+++ b/superset-frontend/src/reports/actions/reports.js
@@ -29,6 +29,11 @@ export function setReport(report) {
   return { type: SET_REPORT, report };
 }
 
+export const DELETE_REPORT = 'DELETE_REPORT';
+export function deleteReport(reportId) {
+  return { type: DELETE_REPORT, reportId };
+}
+
 export function fetchUISpecificReport({
   userId,
   filterField,
@@ -159,7 +164,7 @@ export function deleteActiveReport(report) {
         dispatch(addDangerToast(t('Your report could not be deleted')));
       })
       .finally(() => {
-        dispatch(structureFetchAction);
+        dispatch(deleteReport(report.id));
         dispatch(addSuccessToast(t('Deleted: %s', report.name)));
       });
   };
diff --git a/superset-frontend/src/reports/reducers/reports.js b/superset-frontend/src/reports/reducers/reports.js
index 54cf493..de23f57 100644
--- a/superset-frontend/src/reports/reducers/reports.js
+++ b/superset-frontend/src/reports/reducers/reports.js
@@ -17,36 +17,157 @@
  * under the License.
  */
 /* eslint-disable camelcase */
-import { SET_REPORT, ADD_REPORT, EDIT_REPORT } from '../actions/reports';
+// eslint-disable-next-line import/no-extraneous-dependencies
+import { report } from 'process';
+// import { allowCrossDomain } from 'src/utils/hostNamesConfig';
+import {
+  SET_REPORT,
+  ADD_REPORT,
+  EDIT_REPORT,
+  DELETE_REPORT,
+} from '../actions/reports';
 
-// Talk about the delete
+/* -- Report schema --
+reports: {
+  dashboards: {
+    [dashboardId]: {...reportObject}
+  },
+  charts: {
+    [chartId]: {...reportObject}
+  },
+}
+*/
 
 export default function reportsReducer(state = {}, action) {
   const actionHandlers = {
     [SET_REPORT]() {
+      // Grabs the first report with a dashboard id that
+      // matches the parameter report's dashboard_id
+      const reportWithDashboard = action.report.result.find(
+        report => !!report.dashboard_id,
+      );
+
+      // Grabs the first report with a chart id that
+      // matches the parameter report's chart.id
+      const reportWithChart = action.report.result.find(
+        report => !!report.chart.id,
+      );
+
+      // This organizes report by its type, dashboard or chart
+      // and indexes it by the dashboard/chart id
+      if (reportWithDashboard) {
+        return {
+          ...state,
+          dashboards: {
+            ...state.dashboards,
+            [reportWithDashboard.dashboard_id]: reportWithDashboard,
+          },
+        };
+      }
       return {
         ...state,
-        ...action.report.result.reduce(
-          (obj, report) => ({ ...obj, [report.id]: report }),
-          {},
-        ),
+        charts: {
+          ...state.chart,
+          [reportWithChart.chart.id]: reportWithChart,
+        },
       };
     },
+
     [ADD_REPORT]() {
-      const report = action.json.result;
-      report.id = action.json.id;
+      // Grab first matching report by matching dashboard id
+      const reportWithDashboard = action.json.result.find(
+        report => !!report.dashboard_id,
+      );
+      // Assign the report's id
+      reportWithDashboard.id = action.json.id;
+
+      // Grab first matching report by matching chart id
+      const reportWithChart = action.json.result.find(
+        report => !!report.chart.id,
+      );
+      // Assign the report's id
+      reportWithChart.id = action.json.id;
+
+      // This adds the report by its type, dashboard or chart
+      if (reportWithDashboard) {
+        return {
+          ...state,
+          dashboards: {
+            ...state.dashboards,
+            [reportWithDashboard.dashboard_id]: report,
+          },
+        };
+      }
       return {
         ...state,
-        [action.json.id]: report,
+        charts: {
+          ...state.chart,
+          [reportWithChart.chart.id]: report,
+        },
       };
     },
+
     [EDIT_REPORT]() {
-      const report = action.json.result;
-      report.id = action.json.id;
+      // Grab first matching report by matching dashboard id
+      const reportWithDashboard = action.json.result.find(
+        report => !!report.dashboard_id,
+      );
+      // Assign the report's id
+      reportWithDashboard.id = action.json.id;
+
+      // Grab first matching report by matching chart id
+      const reportWithChart = action.json.result.find(
+        report => !!report.chart.id,
+      );
+      // Assign the report's id
+      reportWithChart.id = action.json.id;
+
+      // This updates the report by its type, dashboard or chart
+      if (reportWithDashboard) {
+        return {
+          ...state,
+          dashboards: {
+            ...state.dashboards,
+            [reportWithDashboard.dashboard_id]: report,
+          },
+        };
+      }
+      return {
+        ...state,
+        charts: {
+          ...state.chart,
+          [reportWithChart.chart.id]: report,
+        },
+      };
+    },
+
+    [DELETE_REPORT]() {
+      // Grabs the first report with a dashboard id that
+      // matches the parameter report's dashboard_id
+      const reportWithDashboard = action.report.result.find(
+        report => !!report.dashboard_id,
+      );
+
+      // This deletes the report by its type, dashboard or chart
+      if (reportWithDashboard) {
+        return {
+          ...state,
+          dashboards: {
+            ...state.dashboards.filter(report => report.id !== action.reportId),
+          },
+        };
+      }
       return {
         ...state,
-        [action.json.id]: report,
+        charts: {
+          ...state.charts.filter(chart => chart.id !== action.reportId),
+        },
       };
+
+      // state.users.filter(item => item.id !== action.payload)
+      // return {
+      //   ...state.filter(report => report.id !== action.reportId),
+      // };
     },
   };