import React from 'react'; import { Link } from 'react-router-dom' import FlipMove from 'react-flip-move'; import FadeIn from '../fade-in' import Bar from './bar' import MoreLink from './more-link' import numberFormatter from '../number-formatter' import * as api from '../api' export default class Referrers extends React.Component { constructor(props) { super(props) this.state = {loading: true} } componentDidMount() { this.fetchReferrers() if (this.props.timer) this.props.timer.onTick(this.fetchReferrers.bind(this)) } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, referrers: null}) this.fetchReferrers() } } fetchReferrers() { if (this.props.query.filters.goal) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/goal/referrers`, this.props.query) .then((res) => this.setState({loading: false, referrers: res})) } else { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers`, this.props.query) .then((res) => this.setState({loading: false, referrers: res})) } } renderReferrer(referrer) { return (
{ referrer.name }
{numberFormatter(referrer.count)}
) } label() { return this.props.query.period === 'realtime' ? 'Active visitors' : 'Visitors' } renderList() { if (this.state.referrers.length > 0) { return (
Referrer { this.label() }
{this.state.referrers.map(this.renderReferrer.bind(this))}
) } else { return
No data yet
} } renderContent() { if (this.state.referrers) { return (

Top Referrers

{ this.renderList() }
) } } render() { return (
{ this.state.loading &&
} { this.renderContent() }
) } }