Skip to content

Commit

Permalink
refactor: Reports code clean 10-29 (#17424)
Browse files Browse the repository at this point in the history
* Add delete functionality

* Report schema restructure progress

* Fix lint

* Removed console.log
  • Loading branch information
lyndsiWilliams authored and AAfghahi committed Mar 22, 2022
1 parent c8474f1 commit b572180
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default function HeaderReportActionsDropDown({
dashboardId={dashboardId}
chart={chart}
/>
{reports ? (
{report ? (
<>
<NoAnimationDropdown
// ref={ref}
Expand Down
7 changes: 6 additions & 1 deletion superset-frontend/src/reports/actions/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)));
});
};
Expand Down
145 changes: 133 additions & 12 deletions superset-frontend/src/reports/reducers/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
// };
},
};

Expand Down

0 comments on commit b572180

Please sign in to comment.