import React from 'react'; import { Link } from 'react-router-dom' import Bar from '../bar' import numberFormatter from '../../number-formatter' import * as api from '../../api' export default class PropertyBreakdown extends React.Component { constructor(props) { super(props) let propKey = props.goal.prop_names[0] this.storageKey = 'goalPropTab__' + props.site.domain + props.goal.name const storedKey = window.localStorage[this.storageKey] if (props.goal.prop_names.includes(storedKey)) { propKey = storedKey } if (props.query.filters['props']) { propKey = Object.keys(props.query.filters['props'])[0] } this.state = { loading: true, propKey: propKey } } componentDidMount() { this.fetchPropBreakdown() } fetchPropBreakdown() { if (this.props.query.filters['goal']) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/property/${encodeURIComponent(this.state.propKey)}`, this.props.query) .then((res) => this.setState({loading: false, breakdown: res})) } } renderUrl(value) { if (value.is_url) { return ( ) } return null } renderPropValue(value) { const query = new URLSearchParams(window.location.search) query.set('props', JSON.stringify({[this.state.propKey]: value.name})) return (
{ value.name } { this.renderUrl(value) }
{numberFormatter(value.count)} {numberFormatter(value.total_count)} {numberFormatter(value.conversion_rate)}%
) } changePropKey(newKey) { window.localStorage[this.storageKey] = newKey this.setState({propKey: newKey, loading: true}, this.fetchPropBreakdown) } renderBody() { if (this.state.loading) { return
} else { return this.state.breakdown.map((propValue) => this.renderPropValue(propValue)) } } renderPill(key) { const isActive = this.state.propKey === key if (isActive) { return
  • {key}
  • } else { return
  • {key}
  • } } render() { return (
    Breakdown by:
    { this.renderBody() }
    ) } }