import preloader from '@fortawesome/fontawesome-free-solid/faCircleNotch' import FontAwesomeIcon from '@fortawesome/react-fontawesome' import { isEmpty, mapObjIndexed, pick } from 'ramda' import React from 'react' import { Doughnut, HorizontalBar } from 'react-chartjs-2' import Moment from 'react-moment' import { connect } from 'react-redux' import { Card, CardBody, CardHeader, UncontrolledTooltip } from 'reactstrap' import DateInput from '../common/DateInput' import { processOsStats, processBrowserStats, processCountriesStats, processReferrersStats, } from '../visits/services/VisitsParser' import { getShortUrlVisits } from './reducers/shortUrlVisits' import './ShortUrlVisits.scss' import MutedMessage from '../utils/MuttedMessage'; const defaultProps = { processOsStats, processBrowserStats, processCountriesStats, processReferrersStats, }; export class ShortUrlsVisits extends React.Component { state = { startDate: undefined, endDate: undefined }; loadVisits = () => { const { match: { params } } = this.props; this.props.getShortUrlVisits(params.shortCode, mapObjIndexed( value => value && value.format ? value.format('YYYY-MM-DD') : value, this.state )) }; componentDidMount() { this.loadVisits(); } render() { const { match: { params }, selectedServer, processOsStats, processBrowserStats, processCountriesStats, processReferrersStats, shortUrlVisits: { visits, loading, error, shortUrl } } = this.props; const serverUrl = selectedServer ? selectedServer.url : ''; const shortLink = `${serverUrl}/${params.shortCode}`; const generateGraphData = (stats, label, isBarChart) => ({ labels: Object.keys(stats), datasets: [ { label, data: Object.values(stats), backgroundColor: isBarChart ? 'rgba(70, 150, 229, 0.4)' : [ '#97BBCD', '#DCDCDC', '#F7464A', '#46BFBD', '#FDB45C', '#949FB1', '#4D5360' ], borderColor: isBarChart ? 'rgba(70, 150, 229, 1)' : 'white', borderWidth: 2 } ] }); const renderGraphCard = (title, stats, isBarChart, label) =>
{title} {!isBarChart && } {isBarChart && }
; const renderContent = () => { if (loading) { return Loading...; } if (error) { return ( An error occurred while loading visits :( ); } if (isEmpty(visits)) { return There have been no visits matching current filter :(; } return (
{renderGraphCard('Operating systems', processOsStats(visits), false)} {renderGraphCard('Browsers', processBrowserStats(visits), false)} {renderGraphCard('Countries', processCountriesStats(visits), true, 'Visits')} {renderGraphCard('Referrers', processReferrersStats(visits), true, 'Visits')}
); }; const renderCreated = () => {shortUrl.dateCreated} {shortUrl.dateCreated} ; return (

{ shortUrl.visitsCount && Visits: {shortUrl.visitsCount} } Visit stats for {shortLink}


{shortUrl.dateCreated &&
Created:   {loading && Loading...} {!loading && renderCreated()}
}
Long URL:   {loading && Loading...} {!loading && {shortUrl.longUrl}}
this.setState({ startDate: date }, () => this.loadVisits())} />
this.setState({ endDate: date }, () => this.loadVisits())} className="short-url-visits__date-input" />
{renderContent()}
); } } ShortUrlsVisits.defaultProps = defaultProps; export default connect( pick(['selectedServer', 'shortUrlVisits']), { getShortUrlVisits } )(ShortUrlsVisits);