All files / hourly-chart/components/HourlyEngagementChart index.jsx

22.22% Statements 2/9
100% Branches 0/0
0% Functions 0/2
22.22% Lines 2/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                                              1x         1x                    
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import ReactHighcharts from 'react-highcharts';
import {
  color,
} from '@bufferapp/analyze-shared-components/style';
import {
  chartConfig,
  primarySeriesConfig,
} from './config';
 
class HourlyEngagementChart extends PureComponent {
  render() {
    const { metric, posts, chartRef } = this.props;
    const hour = moment().startOf('day').subtract(1, 'hour');
    const metricByHour = metric.hourlyMetrics.map((hourlyMetric, index) => {
      hour.add(1, 'hour');
      return {
        x: hour.valueOf(),
        y: hourlyMetric,
        totalUpdates: posts[index],
        color: color[metric.label],
      };
    });
    const config = {
      ...chartConfig,
      series: [{
        ...primarySeriesConfig,
        color: color[metric.label],
        name: metric.label,
        data: metricByHour,
      }],
    };
 
    return <ReactHighcharts ref={chartRef} isPureConfig config={config} />;
  }
}
 
HourlyEngagementChart.defaultProps = {
  posts: [],
  metric: {},
};
 
HourlyEngagementChart.propTypes = {
  posts: PropTypes.arrayOf(PropTypes.number),
  metric: PropTypes.shape({
    label: PropTypes.string,
    hourlyMetrics: PropTypes.arrayOf(PropTypes.number),
  }),
  chartRef: PropTypes.func.isRequired,
};
 
export default HourlyEngagementChart;