Fix metric undefined (#2589)

* extract maybeUpdateMetric function

* reset graph metric after fetching top stats

* fix bug

Also make sure we do not allow 'metric' to become undefined anymore

* keep graph hidden when empty metric saved

* reset metric in setState callback

* pass function not call
This commit is contained in:
RobertJoonas 2023-01-18 16:37:03 +02:00 committed by GitHub
parent 818d4404a8
commit c4b8bf8c32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -358,8 +358,8 @@ export default class VisitorGraph extends React.Component {
}
componentDidUpdate(prevProps, prevState) {
const { metric, topStatData } = this.state;
const { query, site } = this.props
const { metric } = this.state;
const { query } = this.props
if (query !== prevProps.query) {
if (this.isGraphCollapsed()) {
@ -373,18 +373,22 @@ export default class VisitorGraph extends React.Component {
if (metric !== prevState.metric) {
this.setState({mainGraphLoadingState: LOADING_STATE.refreshing}, this.fetchGraphData)
}
}
resetMetric() {
const { topStatData } = this.state
const { query, site } = this.props
const savedMetric = storage.getItem(`metric__${site.domain}`)
const topStatLabels = topStatData && topStatData.top_stats.map(({ name }) => METRIC_MAPPING[name]).filter(name => name)
const prevTopStatLabels = prevState.topStatData && prevState.topStatData.top_stats.map(({ name }) => METRIC_MAPPING[name]).filter(name => name)
if (topStatLabels && `${topStatLabels}` !== `${prevTopStatLabels}`) {
if (query.filters.goal && metric !== 'conversions') {
const selectableMetrics = topStatData && topStatData.top_stats.map(({ name }) => METRIC_MAPPING[name]).filter(name => name)
const canSelectSavedMetric = selectableMetrics && selectableMetrics.includes(savedMetric)
if (query.filters.goal) {
this.setState({ metric: 'conversions' })
} else if (topStatLabels.includes(savedMetric) && savedMetric !== "") {
} else if (canSelectSavedMetric || savedMetric === "") {
this.setState({ metric: savedMetric })
} else {
this.setState({ metric: topStatLabels[0] })
}
this.setState({ metric: 'visitors' })
}
}
@ -429,7 +433,7 @@ export default class VisitorGraph extends React.Component {
fetchTopStatData() {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/top-stats`, this.props.query)
.then((res) => {
this.setState({ topStatsLoadingState: LOADING_STATE.loaded, topStatData: res })
this.setState({ topStatsLoadingState: LOADING_STATE.loaded, topStatData: res }, this.resetMetric)
return res
})
}