import React from 'react'; import { Doughnut, HorizontalBar } from 'react-chartjs-2'; import { connect } from 'react-redux'; import { pick } from 'ramda'; import { Card, CardBody, CardHeader } from 'reactstrap'; import { getShortUrlVisits } from './reducers/shortUrlVisits'; import VisitsParser from '../visits/services/VisitsParser'; import preloader from '@fortawesome/fontawesome-free-solid/faCircleNotch'; import FontAwesomeIcon from '@fortawesome/react-fontawesome'; export class ShortUrlsVisits extends React.Component { state = { startDate: '', endDate: '' }; componentDidMount() { const { match: { params } } = this.props; this.props.getShortUrlVisits(params.shortCode, this.state); } render() { const { match: { params }, selectedServer, visitsParser, shortUrlVisits: { visits, loading, error } } = this.props; const serverUrl = selectedServer ? selectedServer.url : ''; const shortUrl = `${serverUrl}/${params.shortCode}`; const generateGraphData = (stats, label, isBarChart) => ({ labels: Object.keys(stats), datasets: [ { label, data: Object.values(stats), backgroundColor: isBarChart ? 'rgba(200, 26, 80, 0.2)' : [], borderColor: isBarChart ? 'rgba(200, 26, 80, 1)' : [], } ] }); const renderGraphCard = (title, stats, isBarChart = true) =>
{title} {!isBarChart && } {isBarChart && }
; const renderContent = () => { if (loading) { return (

Loading...

); } if (error) { return ( An error occurred while loading visits :( ); } return (
{renderGraphCard('Operating systems', visitsParser.processOsStats(visits), false)} {renderGraphCard('Browsers', visitsParser.processBrowserStats(visits), false)} {renderGraphCard('Countries', visitsParser.processCountriesStats(visits))} {renderGraphCard('Referrers', visitsParser.processReferrersStats(visits))}
); }; return (

Visit stats for {shortUrl}

{renderContent()}
); } } ShortUrlsVisits.defaultProps = { visitsParser: VisitsParser }; export default connect(pick(['selectedServer', 'shortUrlVisits']), { getShortUrlVisits })(ShortUrlsVisits);