From 0e49577b46c6450ae37ec6fea292706833a77a17 Mon Sep 17 00:00:00 2001 From: lyndsiWilliams Date: Tue, 2 Nov 2021 17:49:00 -0500 Subject: [PATCH 1/4] Add delete functionality --- .../HeaderReportActionsDropdown/index.tsx | 2 +- superset-frontend/src/reports/actions/reports.js | 7 ++++++- superset-frontend/src/reports/reducers/reports.js | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/superset-frontend/src/components/ReportModal/HeaderReportActionsDropdown/index.tsx b/superset-frontend/src/components/ReportModal/HeaderReportActionsDropdown/index.tsx index 5eb4448e5b4a5..c85262f6c6126 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 ? ( <> { - 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 54cf493fd5cea..cd785388a6e0f 100644 --- a/superset-frontend/src/reports/reducers/reports.js +++ b/superset-frontend/src/reports/reducers/reports.js @@ -17,7 +17,13 @@ * under the License. */ /* eslint-disable camelcase */ -import { SET_REPORT, ADD_REPORT, EDIT_REPORT } from '../actions/reports'; +import { allowCrossDomain } from 'src/utils/hostNamesConfig'; +import { + SET_REPORT, + ADD_REPORT, + EDIT_REPORT, + DELETE_REPORT, +} from '../actions/reports'; // Talk about the delete @@ -48,6 +54,13 @@ export default function reportsReducer(state = {}, action) { [action.json.id]: report, }; }, + [DELETE_REPORT]() { + console.log('findme REPORT IN REDUCER', action.reportId); + // state.users.filter(item => item.id !== action.payload) + return { + ...state.filter(report => report.id !== action.reportId), + }; + }, }; if (action.type in actionHandlers) { From f74a7b2507c33ad6b1b0f1721a1d7c83a1ef757b Mon Sep 17 00:00:00 2001 From: lyndsiWilliams Date: Wed, 3 Nov 2021 17:34:14 -0500 Subject: [PATCH 2/4] Report schema restructure progress --- .../src/components/ReportModal/index.tsx | 2 + .../src/reports/reducers/reports.js | 131 ++++++++++++++++-- 2 files changed, 119 insertions(+), 14 deletions(-) diff --git a/superset-frontend/src/components/ReportModal/index.tsx b/superset-frontend/src/components/ReportModal/index.tsx index 6c392930e3165..18ae8ea51ae41 100644 --- a/superset-frontend/src/components/ReportModal/index.tsx +++ b/superset-frontend/src/components/ReportModal/index.tsx @@ -178,6 +178,8 @@ const ReportModal: FunctionComponent = ({ const reports = useSelector(state => state.reports); const isEditMode = reports && Object.keys(reports).length; + console.log('---------- REPORT ---------- ', reports); + useEffect(() => { if (isEditMode) { const reportsIds = Object.keys(reports); diff --git a/superset-frontend/src/reports/reducers/reports.js b/superset-frontend/src/reports/reducers/reports.js index cd785388a6e0f..8f1d6b0a71d63 100644 --- a/superset-frontend/src/reports/reducers/reports.js +++ b/superset-frontend/src/reports/reducers/reports.js @@ -17,6 +17,7 @@ * under the License. */ /* eslint-disable camelcase */ +import { report } from 'process'; import { allowCrossDomain } from 'src/utils/hostNamesConfig'; import { SET_REPORT, @@ -25,41 +26,143 @@ import { 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, - [action.json.id]: report, + charts: { + ...state.chart, + [reportWithChart.chart.id]: report, + }, }; }, + [DELETE_REPORT]() { - console.log('findme REPORT IN REDUCER', action.reportId); - // state.users.filter(item => item.id !== action.payload) + // 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.filter(report => report.id !== action.reportId), + ...state, + 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), + // }; }, }; From bcb4c592c900854bb22551587c271fc0d1551066 Mon Sep 17 00:00:00 2001 From: lyndsiWilliams Date: Thu, 4 Nov 2021 14:44:46 -0500 Subject: [PATCH 3/4] Fix lint --- superset-frontend/src/reports/reducers/reports.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/superset-frontend/src/reports/reducers/reports.js b/superset-frontend/src/reports/reducers/reports.js index 8f1d6b0a71d63..de23f572a94fc 100644 --- a/superset-frontend/src/reports/reducers/reports.js +++ b/superset-frontend/src/reports/reducers/reports.js @@ -17,8 +17,9 @@ * under the License. */ /* eslint-disable camelcase */ +// eslint-disable-next-line import/no-extraneous-dependencies import { report } from 'process'; -import { allowCrossDomain } from 'src/utils/hostNamesConfig'; +// import { allowCrossDomain } from 'src/utils/hostNamesConfig'; import { SET_REPORT, ADD_REPORT, @@ -54,7 +55,7 @@ export default function reportsReducer(state = {}, action) { // This organizes report by its type, dashboard or chart // and indexes it by the dashboard/chart id - if (reportWithDashboard) + if (reportWithDashboard) { return { ...state, dashboards: { @@ -62,6 +63,7 @@ export default function reportsReducer(state = {}, action) { [reportWithDashboard.dashboard_id]: reportWithDashboard, }, }; + } return { ...state, charts: { @@ -87,7 +89,7 @@ export default function reportsReducer(state = {}, action) { reportWithChart.id = action.json.id; // This adds the report by its type, dashboard or chart - if (reportWithDashboard) + if (reportWithDashboard) { return { ...state, dashboards: { @@ -95,6 +97,7 @@ export default function reportsReducer(state = {}, action) { [reportWithDashboard.dashboard_id]: report, }, }; + } return { ...state, charts: { @@ -120,7 +123,7 @@ export default function reportsReducer(state = {}, action) { reportWithChart.id = action.json.id; // This updates the report by its type, dashboard or chart - if (reportWithDashboard) + if (reportWithDashboard) { return { ...state, dashboards: { @@ -128,6 +131,7 @@ export default function reportsReducer(state = {}, action) { [reportWithDashboard.dashboard_id]: report, }, }; + } return { ...state, charts: { @@ -145,13 +149,14 @@ export default function reportsReducer(state = {}, action) { ); // This deletes the report by its type, dashboard or chart - if (reportWithDashboard) + if (reportWithDashboard) { return { ...state, dashboards: { ...state.dashboards.filter(report => report.id !== action.reportId), }, }; + } return { ...state, charts: { From 74074baceaecfff00d3b513af0dc6cc7493d742b Mon Sep 17 00:00:00 2001 From: lyndsiWilliams Date: Fri, 12 Nov 2021 07:44:20 -0600 Subject: [PATCH 4/4] Removed console.log --- superset-frontend/src/components/ReportModal/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/superset-frontend/src/components/ReportModal/index.tsx b/superset-frontend/src/components/ReportModal/index.tsx index 18ae8ea51ae41..6c392930e3165 100644 --- a/superset-frontend/src/components/ReportModal/index.tsx +++ b/superset-frontend/src/components/ReportModal/index.tsx @@ -178,8 +178,6 @@ const ReportModal: FunctionComponent = ({ const reports = useSelector(state => state.reports); const isEditMode = reports && Object.keys(reports).length; - console.log('---------- REPORT ---------- ', reports); - useEffect(() => { if (isEditMode) { const reportsIds = Object.keys(reports);