2020-10-28 12:09:04 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { Link } from 'react-router-dom'
|
|
|
|
|
|
|
|
import Bar from '../bar'
|
|
|
|
import numberFormatter from '../../number-formatter'
|
|
|
|
import * as api from '../../api'
|
|
|
|
|
2020-10-30 11:49:41 +03:00
|
|
|
export default class PropertyBreakdown extends React.Component {
|
2020-10-28 12:09:04 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2020-10-30 11:49:41 +03:00
|
|
|
let propKey = props.goal.prop_names[0]
|
|
|
|
this.storageKey = 'goalPropTab__' + props.site.domain + props.goal.name
|
2020-10-29 16:33:37 +03:00
|
|
|
const storedKey = window.localStorage[this.storageKey]
|
2020-10-30 11:49:41 +03:00
|
|
|
if (props.goal.prop_names.includes(storedKey)) {
|
|
|
|
propKey = storedKey
|
2020-10-29 16:33:37 +03:00
|
|
|
}
|
2020-10-30 11:49:41 +03:00
|
|
|
if (props.query.filters['props']) {
|
|
|
|
propKey = Object.keys(props.query.filters['props'])[0]
|
2020-10-29 16:33:37 +03:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:09:04 +03:00
|
|
|
this.state = {
|
|
|
|
loading: true,
|
2020-10-30 11:49:41 +03:00
|
|
|
propKey: propKey
|
2020-10-28 12:09:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-10-30 11:49:41 +03:00
|
|
|
this.fetchPropBreakdown()
|
2020-10-28 12:09:04 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 11:49:41 +03:00
|
|
|
fetchPropBreakdown() {
|
2020-10-28 12:09:04 +03:00
|
|
|
if (this.props.query.filters['goal']) {
|
2020-10-30 11:49:41 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/property/${encodeURIComponent(this.state.propKey)}`, this.props.query)
|
2020-10-28 12:09:04 +03:00
|
|
|
.then((res) => this.setState({loading: false, breakdown: res}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:49:41 +03:00
|
|
|
renderPropValue(value) {
|
2020-10-28 12:09:04 +03:00
|
|
|
const query = new URLSearchParams(window.location.search)
|
2020-10-30 11:49:41 +03:00
|
|
|
query.set('props', JSON.stringify({[this.state.propKey]: value.name}))
|
2020-10-28 12:09:04 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex items-center justify-between my-2" key={value.name}>
|
2020-10-30 12:26:16 +03:00
|
|
|
<div className="w-full h-8 relative" style={{maxWidth: 'calc(100% - 16rem)'}}>
|
2020-10-28 12:09:04 +03:00
|
|
|
<Bar count={value.count} all={this.state.breakdown} bg="bg-red-50" />
|
|
|
|
<Link to={{search: query.toString()}} style={{marginTop: '-26px'}} className="hover:underline block px-2">
|
|
|
|
{ value.name }
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<span className="font-medium inline-block w-20 text-right">{numberFormatter(value.count)}</span>
|
2020-10-29 16:33:37 +03:00
|
|
|
<span className="font-medium inline-block w-20 text-right">{numberFormatter(value.total_count)}</span>
|
2020-10-30 12:26:16 +03:00
|
|
|
<span className="font-medium inline-block w-20 text-right">{numberFormatter(value.conversion_rate)}%</span>
|
2020-10-28 12:09:04 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:49:41 +03:00
|
|
|
changePropKey(newKey) {
|
2020-10-29 16:33:37 +03:00
|
|
|
window.localStorage[this.storageKey] = newKey
|
2020-10-30 11:49:41 +03:00
|
|
|
this.setState({propKey: newKey, loading: true}, this.fetchPropBreakdown)
|
2020-10-28 12:09:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderBody() {
|
|
|
|
if (this.state.loading) {
|
|
|
|
return <div className="px-4 py-2"><div className="loading sm mx-auto"><div></div></div></div>
|
|
|
|
} else {
|
2020-10-30 11:49:41 +03:00
|
|
|
return this.state.breakdown.map((propValue) => this.renderPropValue(propValue))
|
2020-10-28 12:09:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:33:37 +03:00
|
|
|
renderPill(key) {
|
2020-10-30 11:49:41 +03:00
|
|
|
const isActive = this.state.propKey === key
|
2020-10-29 16:33:37 +03:00
|
|
|
|
|
|
|
if (isActive) {
|
|
|
|
return <li key={key} className="inline-block h-5 text-indigo-700 font-bold border-b-2 border-indigo-700">{key}</li>
|
|
|
|
} else {
|
2020-10-30 11:49:41 +03:00
|
|
|
return <li key={key} className="hover:text-indigo-700 cursor-pointer" onClick={this.changePropKey.bind(this, key)}>{key}</li>
|
2020-10-29 16:33:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:09:04 +03:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="w-full pl-6 mt-4">
|
2020-10-29 16:33:37 +03:00
|
|
|
<div className="flex items-center pb-1">
|
|
|
|
<span className="text-xs font-bold text-gray-600">Breakdown by:</span>
|
|
|
|
<ul className="flex font-medium text-xs text-gray-500 space-x-2 leading-5 pl-1">
|
2020-10-30 11:49:41 +03:00
|
|
|
{ this.props.goal.prop_names.map(this.renderPill.bind(this)) }
|
2020-10-29 16:33:37 +03:00
|
|
|
</ul>
|
2020-10-28 12:09:04 +03:00
|
|
|
</div>
|
|
|
|
{ this.renderBody() }
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|