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 MetaBreakdown extends React.Component { constructor(props) { super(props) let metaKey = props.goal.meta_keys[0] this.storageKey = 'goalMetaTab__' + props.site.domain + props.goal.name const storedKey = window.localStorage[this.storageKey] if (props.goal.meta_keys.includes(storedKey)) { metaKey = storedKey } if (props.query.filters['meta']) { metaKey = Object.keys(props.query.filters['meta'])[0] } this.state = { loading: true, metaKey: metaKey } } componentDidMount() { this.fetchMetaBreakdown() } fetchMetaBreakdown() { if (this.props.query.filters['goal']) { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/meta-breakdown/${encodeURIComponent(this.state.metaKey)}`, this.props.query) .then((res) => this.setState({loading: false, breakdown: res})) } } renderMetadataValue(value) { const query = new URLSearchParams(window.location.search) query.set('meta', JSON.stringify({[this.state.metaKey]: value.name})) return (
{ value.name }
{numberFormatter(value.count)} {numberFormatter(value.total_count)}
) } changeMetaKey(newKey) { window.localStorage[this.storageKey] = newKey this.setState({metaKey: newKey, loading: true}, this.fetchMetaBreakdown) } renderBody() { if (this.state.loading) { return
} else { return this.state.breakdown.map((metaValue) => this.renderMetadataValue(metaValue)) } } renderPill(key) { const isActive = this.state.metaKey === key if (isActive) { return
  • {key}
  • } else { return
  • {key}
  • } } render() { return (
    Breakdown by:
    { this.renderBody() }
    ) } }