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: Arash/new state report #16987

Merged
merged 9 commits into from
Oct 28, 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 @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import React, { useState, useEffect, useRef } from 'react';
import React, { useState, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { t, SupersetTheme, css, useTheme } from '@superset-ui/core';
import Icons from 'src/components/Icons';
Expand Down Expand Up @@ -49,19 +49,23 @@ export default function HeaderReportActionsDropDown({
const reports: Record<number, AlertObject> = useSelector<any, AlertObject>(
state => state.reports,
);
const report: AlertObject = Object.values(reports).filter(report => {
if (dashboardId) {
return report.dashboard_id === dashboardId;
}
return report.chart_id === chart?.id;
})[0];

const user: UserWithPermissionsAndRoles = useSelector<
any,
UserWithPermissionsAndRoles
>(state => state.user || state.explore?.user);
const reportsIds = Object.keys(reports || []);
const report: AlertObject = reports?.[reportsIds[0]];
const [
currentReportDeleting,
setCurrentReportDeleting,
] = useState<AlertObject | null>(null);
const theme = useTheme();
const [showModal, setShowModal] = useState(false);
const dashboardIdRef = useRef(dashboardId);
const [showModal, setShowModal] = useState<boolean>(false);
const toggleActiveKey = async (data: AlertObject, checked: boolean) => {
if (data?.id) {
toggleActive(data, checked);
Expand Down Expand Up @@ -104,17 +108,13 @@ export default function HeaderReportActionsDropDown({
}, []);

useEffect(() => {
if (
canAddReports() &&
dashboardId &&
dashboardId !== dashboardIdRef.current
) {
if (canAddReports()) {
dispatch(
fetchUISpecificReport({
userId: user.userId,
filterField: 'dashboard_id',
creationMethod: 'dashboards',
resourceId: dashboardId,
filterField: dashboardId ? 'dashboard_id' : 'chart_id',
creationMethod: dashboardId ? 'dashboards' : 'charts',
resourceId: dashboardId || chart?.id,
}),
);
}
Expand Down Expand Up @@ -148,14 +148,14 @@ export default function HeaderReportActionsDropDown({
canAddReports() && (
<>
<ReportModal
show={showModal}
onHide={() => setShowModal(false)}
userId={user.userId}
showModal={showModal}
onHide={() => setShowModal(false)}
userEmail={user.email}
dashboardId={dashboardId}
chart={chart}
/>
{report ? (
{reports ? (
<>
<NoAnimationDropdown
// ref={ref}
Expand Down
18 changes: 7 additions & 11 deletions superset-frontend/src/components/ReportModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import React, {
FunctionComponent,
} from 'react';
import { t, SupersetTheme } from '@superset-ui/core';
import { bindActionCreators } from 'redux';
import { connect, useDispatch, useSelector } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { addReport, editReport } from 'src/reports/actions/reports';
import { AlertObject } from 'src/views/CRUD/alert/types';

Expand Down Expand Up @@ -77,14 +76,14 @@ interface ReportProps {
addDangerToast: (msg: string) => void;
addSuccessToast: (msg: string) => void;
addReport: (report?: ReportObject) => {};
onHide: () => {};
onHide: () => void;
onReportAdd: (report?: ReportObject) => {};
show: boolean;
showModal: boolean;
userId: number;
userEmail: string;
dashboardId?: number;
chart?: ChartState;
props: any;
props?: any;
}

interface ReportPayloadType {
Expand Down Expand Up @@ -154,7 +153,7 @@ const reportReducer = (
const ReportModal: FunctionComponent<ReportProps> = ({
onReportAdd,
onHide,
show = false,
showModal = false,
dashboardId,
chart,
userId,
Expand Down Expand Up @@ -291,7 +290,7 @@ const ReportModal: FunctionComponent<ReportProps> = ({

return (
<StyledModal
show={show}
show={showModal}
onHide={onClose}
title={wrappedTitle}
footer={renderModalFooter}
Expand Down Expand Up @@ -381,7 +380,4 @@ const ReportModal: FunctionComponent<ReportProps> = ({
);
};

const mapDispatchToProps = (dispatch: any) =>
bindActionCreators({ addReport, editReport }, dispatch);

export default connect(null, mapDispatchToProps)(withToasts(ReportModal));
export default withToasts(ReportModal);
8 changes: 1 addition & 7 deletions superset-frontend/src/dashboard/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,6 @@ class Header extends React.PureComponent {
this.startPeriodicRender(refreshFrequency * 1000);
}

componentDidUpdate(prevProps) {
if (this.props.refreshFrequency !== prevProps.refreshFrequency) {
const { refreshFrequency } = this.props;
this.startPeriodicRender(refreshFrequency * 1000);
}
}

UNSAFE_componentWillReceiveProps(nextProps) {
if (
UNDO_LIMIT - nextProps.undoLength <= 0 &&
Expand Down Expand Up @@ -524,6 +517,7 @@ class Header extends React.PureComponent {
</span>
)}
<HeaderReportActionsDropdown
key={dashboardInfo.id}
toggleActive={this.props.toggleActive}
deleteActiveReport={this.props.deleteActiveReport}
dashboardId={dashboardInfo.id}
Expand Down
3 changes: 3 additions & 0 deletions superset-frontend/src/reports/reducers/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
/* eslint-disable camelcase */
import { SET_REPORT, ADD_REPORT, EDIT_REPORT } from '../actions/reports';

// Talk about the delete

export default function reportsReducer(state = {}, action) {
const actionHandlers = {
[SET_REPORT]() {
return {
...state,
...action.report.result.reduce(
(obj, report) => ({ ...obj, [report.id]: report }),
{},
Expand Down
2 changes: 2 additions & 0 deletions superset-frontend/src/views/CRUD/alert/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ export type AlertObject = {
chart?: MetaObject;
changed_by?: user;
changed_on_delta_humanized?: string;
chart_id: number;
created_by?: user;
created_on?: string;
crontab?: string;
dashboard?: MetaObject;
dashboard_id?: number;
database?: MetaObject;
description?: string;
grace_period?: number;
Expand Down
2 changes: 2 additions & 0 deletions superset/reports/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ def ensure_alert_reports_enabled(self) -> Optional[Response]:
"changed_by.last_name",
"changed_on",
"changed_on_delta_humanized",
"chart_id",
"created_by.first_name",
"created_by.last_name",
"created_on",
"creation_method",
"crontab",
"crontab_humanized",
"dashboard_id",
"description",
"id",
"last_eval_dttm",
Expand Down