import React from "react"; import { Link } from 'react-router-dom' import { withRouter } from 'react-router-dom' import Modal from './modal' import * as api from '../../api' import numberFormatter from '../../number-formatter' import {parseQuery} from '../../query' class ExitPagesModal extends React.Component { constructor(props) { super(props) this.state = { loading: true, query: parseQuery(props.location.search, props.site), pages: [], page: 1, moreResultsAvailable: false } } componentDidMount() { this.loadPages(); } loadPages() { const {query, page, pages} = this.state; api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/exit-pages`, query, {limit: 100, page}) .then((res) => this.setState((state) => ({loading: false, pages: state.pages.concat(res), moreResultsAvailable: res.length === 100}))) } loadMore() { this.setState({loading: true, page: this.state.page + 1}, this.loadPages.bind(this)) } formatPercentage(number) { if (typeof(number) === 'number') { return number + '%' } else { return '-' } } renderPage(page) { const query = new URLSearchParams(window.location.search) query.set('exit_page', page.name) return ( {page.name} {numberFormatter(page.count)} {numberFormatter(page.exits)} {this.formatPercentage(page.exit_rate)} ) } renderLoading() { if (this.state.loading) { return
} else if (this.state.moreResultsAvailable) { return (
) } } renderBody() { if (this.state.pages) { return (

Exit Pages

{ this.state.pages.map(this.renderPage.bind(this)) }
Page url Unique Exits Total Exits Exit Rate
) } } render() { return ( { this.renderBody() } { this.renderLoading() } ) } } export default withRouter(ExitPagesModal)