Use react Hooks for ListReport

pull/1542/head
Uku Taht 5 years ago
parent 32f5a8d296
commit 6365c6956b

@ -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 (
<div className="items-start justify-between block w-full mt-6 md:flex">
<Conversions site={this.props.site} query={this.props.query} />
<Conversions site={props.site} query={props.query} />
</div>
)
}
@ -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 (
<div className="mb-12">
<div id="stats-container-top"></div>
<div className={`${navClass} top-0 sm:py-3 py-2 z-10 ${this.props.stuck && !this.props.site.embedded ? 'fullwidth-shadow bg-gray-50 dark:bg-gray-850' : ''}`}>
<div className="items-center w-full flex">
<div className="flex items-center w-full">
<SiteSwitcher site={this.props.site} loggedIn={this.props.loggedIn} currentUserRole={this.props.currentUserRole} />
<CurrentVisitors timer={this.props.timer} site={this.props.site} query={this.props.query} />
<Filters className="flex" site={this.props.site} query={this.props.query} history={this.props.history} />
</div>
<Datepicker site={this.props.site} query={this.props.query} />
return (
<div className="mb-12">
<div id="stats-container-top"></div>
<div className={`${navClass} top-0 sm:py-3 py-2 z-10 ${props.stuck && !props.site.embedded ? 'fullwidth-shadow bg-gray-50 dark:bg-gray-850' : ''}`}>
<div className="items-center w-full flex">
<div className="flex items-center w-full">
<SiteSwitcher site={props.site} loggedIn={props.loggedIn} currentUserRole={props.currentUserRole} />
<CurrentVisitors timer={props.timer} site={props.site} query={props.query} />
<Filters className="flex" site={props.site} query={props.query} history={props.history} />
</div>
<Datepicker site={props.site} query={props.query} />
</div>
<VisitorGraph site={this.props.site} query={this.props.query} />
<div className="items-start justify-between block w-full md:flex">
<Sources site={this.props.site} query={this.props.query} />
<Pages site={this.props.site} query={this.props.query} />
</div>
<div className="items-start justify-between block w-full md:flex">
<Locations site={this.props.site} query={this.props.query} />
<Devices site={this.props.site} query={this.props.query} />
</div>
{ this.renderConversions() }
</div>
)
}
<VisitorGraph site={props.site} query={props.query} />
<div className="items-start justify-between block w-full md:flex">
<Sources site={props.site} query={props.query} />
<Pages site={props.site} query={props.query} />
</div>
<div className="items-start justify-between block w-full md:flex">
<Locations site={props.site} query={props.query} />
<Devices site={props.site} query={props.query} />
</div>
{ renderConversions() }
</div>
)
}
export default withPinnedHeader(Historical, '#stats-container-top');

@ -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 (
<a
target="_blank"
rel="noreferrer"
href={dest}
className="hidden group-hover:block"
>
<svg className="inline w-4 h-4 ml-1 -mt-1 text-gray-600 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20"><path d="M11 3a1 1 0 100 2h2.586l-6.293 6.293a1 1 0 101.414 1.414L15 6.414V9a1 1 0 102 0V4a1 1 0 00-1-1h-5z"></path><path d="M5 5a2 2 0 00-2 2v8a2 2 0 002 2h8a2 2 0 002-2v-3a1 1 0 10-2 0v3H5V7h3a1 1 0 000-2H5z"></path></svg>
</a>
)
}
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 (
<a
target="_blank"
rel="noreferrer"
href={dest}
className="hidden group-hover:block"
>
<svg className="inline w-4 h-4 ml-1 -mt-1 text-gray-600 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20"><path d="M11 3a1 1 0 100 2h2.586l-6.293 6.293a1 1 0 101.414 1.414L15 6.414V9a1 1 0 102 0V4a1 1 0 00-1-1h-5z"></path><path d="M5 5a2 2 0 00-2 2v8a2 2 0 002 2h8a2 2 0 002-2v-3a1 1 0 10-2 0v3H5V7h3a1 1 0 000-2H5z"></path></svg>
</a>
)
}
}
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 (
<div className="flex items-center justify-between my-1 text-sm" key={listItem.name}>
<Bar
count={listItem[this.valueKey()]}
all={this.state.list}
count={listItem[valueKey]}
all={state.list}
bg={`${lightBackground} dark:bg-gray-500 dark:bg-opacity-15`}
maxWidthDeduction={maxWidthDeduction}
plot={this.valueKey()}
plot={valueKey}
>
<span className="flex px-2 py-1.5 group dark:text-gray-300 relative z-9 break-all" tooltip={this.props.tooltipText && this.props.tooltipText(listItem)}>
<Link onClick={this.props.onClick || noop} className="md:truncate block hover:underline" to={{search: query.toString()}}>
{this.props.renderIcon && this.props.renderIcon(listItem)}
{this.props.renderIcon && ' '}
<span className="flex px-2 py-1.5 group dark:text-gray-300 relative z-9 break-all" tooltip={props.tooltipText && props.tooltipText(listItem)}>
<Link onClick={props.onClick || noop} className="md:truncate block hover:underline" to={{search: query.toString()}}>
{props.renderIcon && props.renderIcon(listItem)}
{props.renderIcon && ' '}
{listItem.name}
</Link>
{ this.renderExternalLink(listItem) }
<ExternalLink item={listItem} externalLinkDest={props.externalLinkDest} />
</span>
</Bar>
<span className="font-medium dark:text-gray-200 w-20 text-right">
{numberFormatter(listItem[this.valueKey()])}
{numberFormatter(listItem[valueKey])}
{
listItem.percentage >= 0
? <span className="inline-block w-8 pl-1 text-xs text-right">({listItem.percentage}%)</span>
: null
}
</span>
{this.showConversionRate() && <span className="font-medium dark:text-gray-200 w-20 text-right">{listItem.conversion_rate}%</span>}
{showConversionRate && <span className="font-medium dark:text-gray-200 w-20 text-right">{listItem.conversion_rate}%</span>}
</div>
)
}
renderList() {
if (this.state.list && this.state.list.length > 0) {
function renderList() {
if (state.list && state.list.length > 0) {
return (
<>
<div className="flex items-center justify-between mt-3 mb-2 text-xs font-bold tracking-wide text-gray-500 dark:text-gray-400">
<span>{ this.props.keyLabel }</span>
<span>{ props.keyLabel }</span>
<span className="text-right">
<span className="inline-block w-30">{this.label()}</span>
{this.showConversionRate() && <span className="inline-block w-20">CR</span>}
<span className="inline-block w-30">{label()}</span>
{showConversionRate && <span className="inline-block w-20">CR</span>}
</span>
</div>
{ 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 <div className="font-medium text-center text-gray-500 mt-44 dark:text-gray-400">No data yet</div>
}
render() {
return (
<LazyLoader onVisible={this.onVisible} className="flex flex-col flex-grow">
{ this.state.loading && <div className="mx-auto loading mt-44"><div></div></div> }
<FadeIn show={!this.state.loading} className="flex-grow">
{ this.renderList() }
</FadeIn>
{this.props.detailsLink && !this.state.loading && <MoreLink url={this.props.detailsLink} list={this.state.list} />}
</LazyLoader>
)
}
return (
<LazyLoader onVisible={onVisible} className="flex flex-col flex-grow">
{ state.loading && <div className="mx-auto loading mt-44"><div></div></div> }
<FadeIn show={!state.loading} className="flex-grow">
{ renderList() }
</FadeIn>
{props.detailsLink && !state.loading && <MoreLink url={props.detailsLink} list={state.list} />}
</LazyLoader>
)
}

Loading…
Cancel
Save