import React from 'react'; import { Link } from 'react-router-dom' import FadeIn from '../../fade-in' import MoreLink from '../more-link' import numberFormatter from '../../number-formatter' import Bar from '../bar' import LazyLoader from '../../lazy-loader' export default class ListReport extends React.Component { constructor(props) { super(props) this.state = {loading: true} this.onVisible = this.onVisible.bind(this) } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.fetchData() } } 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})) } label() { if (this.props.query.period === 'realtime') { return 'Current visitors' } if (this.showConversionRate()) { return 'Conversions' } return 'Visitors' } showConversionRate() { return !!this.props.query.filters.goal } renderListItem(listItem) { const query = new URLSearchParams(window.location.search) Object.entries(this.props.filter).forEach((([key, valueKey]) => { query.set(key, listItem[valueKey]) })) const maxWidthDeduction = this.showConversionRate() ? "10rem" : "5rem" const lightBackground = this.props.color || 'bg-green-50' return (
{this.props.renderIcon && this.props.renderIcon(listItem)} {this.props.renderIcon && ' '} {listItem.name} {numberFormatter(listItem.visitors)} { listItem.percentage >= 0 ? ({listItem.percentage}%) : null } {this.showConversionRate() && {listItem.conversion_rate}%}
) } renderList() { if (this.state.list && this.state.list.length > 0) { return ( <>
{ this.props.keyLabel } {this.label()} {this.showConversionRate() && CR}
{ this.state.list && this.state.list.map(this.renderListItem.bind(this)) } ) } return
No data yet
} render() { return ( { this.state.loading &&
} { this.renderList() } {this.props.detailsLink && !this.state.loading && }
) } }