Skip to content

Commit

Permalink
fix(explore): Metric control breaks when saved metric deleted from da…
Browse files Browse the repository at this point in the history
…taset (#17503)
  • Loading branch information
kgabryje authored and AAfghahi committed Feb 9, 2022
1 parent 98bb7f9 commit 2908169
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ 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,
Expand Down
4 changes: 4 additions & 0 deletions superset-frontend/src/components/ReportModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ interface ReportProps {
userEmail: string;
dashboardId?: number;
chart?: ChartState;
<<<<<<< HEAD
props?: any;
=======
props: any;
>>>>>>> be2e1ecf6... code dry (#16358)
}

interface ReportPayloadType {
Expand Down
7 changes: 7 additions & 0 deletions superset-frontend/src/dashboard/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -123,31 +123,26 @@ export class ExploreChartHeader extends React.PureComponent {

async fetchChartDashboardData() {
const { dashboardId, slice } = this.props;
await SupersetClient.get({
const response = await SupersetClient.get({
endpoint: `/api/v1/chart/${slice.slice_id}`,
})
.then(res => {
const response = res?.json?.result;
if (response && response.dashboards && response.dashboards.length) {
const { dashboards } = response;
const dashboard =
dashboardId &&
dashboards.length &&
dashboards.find(d => d.id === dashboardId);
});
const chart = response.json.result;
const dashboards = chart.dashboards || [];
const dashboard =
dashboardId &&
dashboards.length &&
dashboards.find(d => d.id === dashboardId);

if (dashboard && dashboard.json_metadata) {
// setting the chart to use the dashboard custom label colors if any
const labelColors =
JSON.parse(dashboard.json_metadata).label_colors || {};
const categoricalNamespace = CategoricalColorNamespace.getNamespace();
if (dashboard && dashboard.json_metadata) {
// setting the chart to use the dashboard custom label colors if any
const labelColors =
JSON.parse(dashboard.json_metadata).label_colors || {};
const categoricalNamespace = CategoricalColorNamespace.getNamespace();

Object.keys(labelColors).forEach(label => {
categoricalNamespace.setColor(label, labelColors[label]);
});
}
}
})
.catch(() => {});
Object.keys(labelColors).forEach(label => {
categoricalNamespace.setColor(label, labelColors[label]);
});
}
}

getSliceName() {
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

0 comments on commit 2908169

Please sign in to comment.