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

Arash/refactor #17548

Merged
merged 17 commits into from
Nov 29, 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 @@ -25,8 +25,8 @@ import { AlertObject } from 'src/views/CRUD/alert/types';
import { Menu, NoAnimationDropdown } from 'src/common/components';
import { isFeatureEnabled, FeatureFlag } from 'src/featureFlags';
import DeleteModal from 'src/components/DeleteModal';
import { ChartState } from 'src/explore/types';
import ReportModal from 'src/components/ReportModal';
import { ChartState } from 'src/explore/types';
import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
import { fetchUISpecificReport } from 'src/reports/actions/reports';

Expand All @@ -46,24 +46,28 @@ export default function HeaderReportActionsDropDown({
chart?: ChartState;
}) {
const dispatch = useDispatch();
const reports: Record<number, AlertObject> = useSelector<any, AlertObject>(
state => state.reports,
);
const report: AlertObject = Object.values(reports).filter(report => {
const report: AlertObject = useSelector<any, AlertObject>(state => {
if (dashboardId) {
return report.dashboard_id === dashboardId;
return state.reports.dashboards?.[dashboardId];
}
if (chart?.id) {
return state.reports.charts?.[chart.id];
}
return report.chart_id === chart?.id;
})[0];
return {};
});
// const report: ReportObject = Object.values(reports).filter(report => {
// if (dashboardId) {
// return report.dashboards?.[dashboardId];
// }
// // return report.charts?.[chart?.id]
// })[0];

const user: UserWithPermissionsAndRoles = useSelector<
any,
UserWithPermissionsAndRoles
>(state => state.user || state.explore?.user);
const [
currentReportDeleting,
setCurrentReportDeleting,
] = useState<AlertObject | null>(null);
const [currentReportDeleting, setCurrentReportDeleting] =
useState<AlertObject | null>(null);
const theme = useTheme();
const [showModal, setShowModal] = useState<boolean>(false);
const toggleActiveKey = async (data: AlertObject, checked: boolean) => {
Expand Down
70 changes: 2 additions & 68 deletions superset-frontend/src/dashboard/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,6 @@ class Header extends React.PureComponent {
componentDidMount() {
const { refreshFrequency } = this.props;
this.startPeriodicRender(refreshFrequency * 1000);
<<<<<<< HEAD
<<<<<<< HEAD
=======
if (this.canAddReports()) {
// this is in case there is an anonymous user.
this.props.fetchUISpecificReport(
user.userId,
'dashboard_id',
'dashboards',
dashboardInfo.id,
);
}
>>>>>>> refactor progress (#16339)
=======
>>>>>>> code dry (#16358)
}

UNSAFE_componentWillReceiveProps(nextProps) {
Expand All @@ -197,24 +182,6 @@ class Header extends React.PureComponent {
) {
this.props.setMaxUndoHistoryExceeded();
}
<<<<<<< HEAD
<<<<<<< HEAD
=======
if (
this.canAddReports() &&
nextProps.dashboardInfo.id !== this.props.dashboardInfo.id
) {
// this is in case there is an anonymous user.
this.props.fetchUISpecificReport(
user.userId,
'dashboard_id',
'dashboards',
nextProps.dashboardInfo.id,
);
}
>>>>>>> refactor progress (#16339)
=======
>>>>>>> code dry (#16358)
}

componentWillUnmount() {
Expand Down Expand Up @@ -396,36 +363,6 @@ class Header extends React.PureComponent {
this.setState({ showingPropertiesModal: false });
}

<<<<<<< HEAD
<<<<<<< HEAD
showReportModal() {
this.setState({ showingReportModal: true });
}

hideReportModal() {
this.setState({ showingReportModal: false });
=======
canAddReports() {
if (!isFeatureEnabled(FeatureFlag.ALERT_REPORTS)) {
return false;
}
const { user } = this.props;
if (!user) {
// this is in the case that there is an anonymous user.
return false;
}
const roles = Object.keys(user.roles || []);
const permissions = roles.map(key =>
user.roles[key].filter(
perms => perms[0] === 'menu_access' && perms[1] === 'Manage',
),
);
return permissions[0].length > 0;
>>>>>>> refactor progress (#16339)
}

=======
>>>>>>> code dry (#16358)
render() {
const {
dashboardTitle,
Expand Down Expand Up @@ -590,7 +527,6 @@ class Header extends React.PureComponent {
toggleActive={this.props.toggleActive}
deleteActiveReport={this.props.deleteActiveReport}
dashboardId={dashboardInfo.id}
showReportModal={this.showReportModal}
/>
</>
)}
Expand All @@ -602,10 +538,8 @@ class Header extends React.PureComponent {
onHide={this.hidePropertiesModal}
colorScheme={this.props.colorScheme}
onSubmit={updates => {
const {
dashboardInfoChanged,
dashboardTitleChanged
} = this.props;
const { dashboardInfoChanged, dashboardTitleChanged } =
this.props;
dashboardInfoChanged({
slug: updates.slug,
metadata: JSON.parse(updates.jsonMetadata),
Expand Down
20 changes: 12 additions & 8 deletions superset-frontend/src/reports/reducers/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ export default function reportsReducer(state = {}, action) {
[SET_REPORT]() {
// Grabs the first report with a dashboard id that
// matches the parameter report's dashboard_id
const reportWithDashboard = action.report.result.find(
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,
const reportWithChart = action.report.result?.find(
report => !!report.chart?.id,
);

// This organizes report by its type, dashboard or chart
Expand All @@ -64,12 +63,17 @@ export default function reportsReducer(state = {}, action) {
},
};
}
if (reportWithChart) {
return {
...state,
charts: {
...state.chart,
[reportWithChart.chart.id]: reportWithChart,
},
};
}
return {
...state,
charts: {
...state.chart,
[reportWithChart.chart.id]: reportWithChart,
},
};
},

Expand Down