diff --git a/assets/js/dashboard/historical.js b/assets/js/dashboard/historical.js index a4b3b4b21..b910af02a 100644 --- a/assets/js/dashboard/historical.js +++ b/assets/js/dashboard/historical.js @@ -12,12 +12,12 @@ import Devices from './stats/devices' import Conversions from './stats/conversions' import { withPinnedHeader } from './pinned-header-hoc'; -class Historical extends React.Component { - renderConversions() { - if (this.props.site.hasGoals) { +function Historical(props) { + function renderConversions() { + if (props.site.hasGoals) { return (
- +
) } @@ -25,35 +25,33 @@ class Historical extends React.Component { return null } - render() { - const navClass = this.props.site.embedded ? 'relative' : 'sticky' + const navClass = props.site.embedded ? 'relative' : 'sticky' - return ( -
-
-
-
-
- - - -
- + return ( +
+
+
+
+
+ + +
+
- -
- - -
-
- - -
- { this.renderConversions() }
- ) - } + +
+ + +
+
+ + +
+ { renderConversions() } +
+ ) } export default withPinnedHeader(Historical, '#stats-container-top'); diff --git a/assets/js/dashboard/stats/reports/list.js b/assets/js/dashboard/stats/reports/list.js index b52194fd1..4888a1d48 100644 --- a/assets/js/dashboard/stats/reports/list.js +++ b/assets/js/dashboard/stats/reports/list.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import { Link } from 'react-router-dom' import FadeIn from '../../fade-in' @@ -7,125 +7,115 @@ import numberFormatter from '../../util/number-formatter' import Bar from '../bar' import LazyLoader from '../../components/lazy-loader' -export default class ListReport extends React.Component { - constructor(props) { - super(props) - this.state = {loading: true} - this.onVisible = this.onVisible.bind(this) +function ExternalLink({item, externalLinkDest}) { + if (externalLinkDest) { + const dest = externalLinkDest(item) + + return ( + + + + ) } - componentDidUpdate(prevProps) { - if (this.props.query !== prevProps.query) { - this.fetchData() + return null +} + +export default function ListReport(props) { + const [state, setState] = useState({loading: true, list: null}) + const valueKey = props.valueKey || 'visitors' + const showConversionRate = !!props.query.filters.goal + const prevProps = useRef(); + + function fetchData() { + if (typeof(prevProps.current) === 'undefined' || prevProps.current !== props) { + prevProps.current = props; + setState({loading: true, list: null}) + props.fetchData() + .then((res) => setState({loading: false, list: res})) } } - onVisible() { - this.fetchData() - if (this.props.timer) this.props.timer.onTick(this.fetchData.bind(this)) - } - fetchData() { - this.setState({loading: true, list: null}) - this.props.fetchData() - .then((res) => this.setState({loading: false, list: res})) + function onVisible() { + fetchData() + if (props.timer) props.timer.onTick(fetchData) } - label() { - if (this.props.query.period === 'realtime') { + + function label() { + if (props.query.period === 'realtime') { return 'Current visitors' } - if (this.showConversionRate()) { + if (showConversionRate) { return 'Conversions' } - return this.valueLabel() - } - - showConversionRate() { - return !!this.props.query.filters.goal - } - - valueKey() { - return this.props.valueKey || 'visitors' + return props.valueLabel || 'Visitors' } - valueLabel() { - return this.props.valueLabel || 'Visitors' - } - - renderExternalLink(item) { - if (this.props.externalLinkDest) { - const dest = this.props.externalLinkDest(item) + useEffect(fetchData, [props.query]); - return ( - - - - ) - } - } - - renderListItem(listItem) { + function renderListItem(listItem) { const query = new URLSearchParams(window.location.search) - Object.entries(this.props.filter).forEach((([key, valueKey]) => { + Object.entries(props.filter).forEach((([key, valueKey]) => { query.set(key, listItem[valueKey]) })) - const maxWidthDeduction = this.showConversionRate() ? "10rem" : "5rem" - const lightBackground = this.props.color || 'bg-green-50' + const maxWidthDeduction = showConversionRate ? "10rem" : "5rem" + const lightBackground = props.color || 'bg-green-50' const noop = () => {} return (
- - - {this.props.renderIcon && this.props.renderIcon(listItem)} - {this.props.renderIcon && ' '} + + + {props.renderIcon && props.renderIcon(listItem)} + {props.renderIcon && ' '} {listItem.name} - { this.renderExternalLink(listItem) } + - {numberFormatter(listItem[this.valueKey()])} + {numberFormatter(listItem[valueKey])} { listItem.percentage >= 0 ? ({listItem.percentage}%) : null } - {this.showConversionRate() && {listItem.conversion_rate}%} + {showConversionRate && {listItem.conversion_rate}%}
) } - renderList() { - if (this.state.list && this.state.list.length > 0) { + function renderList() { + if (state.list && state.list.length > 0) { return ( <>
- { this.props.keyLabel } + { props.keyLabel } - {this.label()} - {this.showConversionRate() && CR} + {label()} + {showConversionRate && CR}
- { this.state.list && this.state.list.map(this.renderListItem.bind(this)) } + { state.list && state.list.map(renderListItem) } ) } @@ -133,15 +123,13 @@ export default class ListReport extends React.Component { return
No data yet
} - render() { - return ( - - { this.state.loading &&
} - - { this.renderList() } - - {this.props.detailsLink && !this.state.loading && } -
- ) - } + return ( + + { state.loading &&
} + + { renderList() } + + {props.detailsLink && !state.loading && } +
+ ) }