import React from "react";
import { Link, withRouter } from 'react-router-dom'
import Modal from './modal'
import * as api from '../../api'
import numberFormatter from '../../number-formatter'
import {parseQuery} from '../../query'
class ReferrersModal extends React.Component {
constructor(props) {
super(props)
this.state = {
loading: true,
query: parseQuery(props.location.search, props.site)
}
}
componentDidMount() {
const include = this.showBounceRate() ? 'bounce_rate' : null
api.get(`/api/stats/${this.props.site.domain}/referrers`, this.state.query, {limit: 100, include: include})
.then((res) => this.setState({loading: false, referrers: res}))
}
showBounceRate() {
return !this.state.query.filters.goal
}
formatBounceRate(page) {
if (page.bounce_rate) {
return page.bounce_rate + '%'
} else {
return '-'
}
}
renderReferrer(referrer) {
return (
{ referrer.name }
|
{numberFormatter(referrer.count)} |
{this.showBounceRate() && {this.formatBounceRate(referrer)} | }
)
}
renderBody() {
if (this.state.loading) {
return (
)
} else if (this.state.referrers) {
return (
Referrer |
Visitors |
{this.showBounceRate() && Bounce rate | }
{ this.state.referrers.map(this.renderReferrer.bind(this)) }
)
}
}
render() {
return (
{ this.renderBody() }
)
}
}
export default withRouter(ReferrersModal)