Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Reports code clean 10-29 #17424

Merged
merged 4 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,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 @@ -30,6 +30,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 @@ -172,7 +177,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),
// };
Comment on lines +167 to +170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// state.users.filter(item => item.id !== action.payload)
// return {
// ...state.filter(report => report.id !== action.reportId),
// };

},
};

Expand Down