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}))
}
label() {
return this.state.query.period === 'realtime' ? 'Current visitors' : '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}
|
{numberFormatter(tableItem.visitors)}
{tableItem.percentage >= 0 &&
({tableItem.percentage}%) }
|
)
}
renderBody() {
if (this.state.loading) {
return (
)
}
if (this.state.list) {
return (
<>
{this.props.title}
{this.props.keyLabel}
|
{this.label()}
|
{ this.state.list.map(this.renderTableItem.bind(this)) }
>
)
}
return null
}
render() {
return (
{ this.renderBody() }
)
}
}
export default withRouter(ModalTable)