import React from "react";
import { Link, withRouter } from 'react-router-dom'
import Modal from './modal'
import * as api from '../../api'
import numberFormatter from '../../util/number-formatter'
import {parseQuery} from '../../query'
class ModalTable extends React.Component {
constructor(props) {
super(props)
this.state = {
loading: true,
query: parseQuery(props.location.search, props.site)
}
}
componentDidMount() {
api.get(this.props.endpoint, this.state.query, {limit: 100})
.then((res) => this.setState({loading: false, list: res}))
}
showConversionRate() {
return !!this.state.query.filters.goal
}
showPercentage() {
return this.props.showPercentage && !this.showConversionRate()
}
label() {
if (this.state.query.period === 'realtime') {
return 'Current visitors'
}
if (this.showConversionRate()) {
return 'Conversions'
}
return 'Visitors'
}
renderTableItem(tableItem) {
const query = new URLSearchParams(window.location.search)
Object.entries(this.props.filter).forEach((([key, valueKey]) => {
query.set(key, tableItem[valueKey])
}))
return (
|
{this.props.renderIcon && this.props.renderIcon(tableItem)}
{this.props.renderIcon && ' '}
{tableItem.name}
|
{this.showConversionRate() && {numberFormatter(tableItem.total_visitors)} | }
{numberFormatter(tableItem.visitors)} |
{this.showPercentage() && {tableItem.percentage} | }
{this.showConversionRate() && {numberFormatter(tableItem.conversion_rate)}% | }
)
}
renderBody() {
if (this.state.loading) {
return (
)
}
if (this.state.list) {
return (
<>
{this.props.title}
| {this.props.keyLabel} |
{this.showConversionRate() && Total Visitors | }
{this.label()} |
{this.showPercentage() && % | }
{this.showConversionRate() && CR | }
{ this.state.list.map(this.renderTableItem.bind(this)) }
>
)
}
return null
}
render() {
return (
{ this.renderBody() }
)
}
}
export default withRouter(ModalTable)