2018-07-29 19:39:00 +03:00
|
|
|
import React from 'react';
|
2018-07-30 21:31:48 +03:00
|
|
|
import { Doughnut, HorizontalBar } from 'react-chartjs-2';
|
2018-07-29 19:39:00 +03:00
|
|
|
import { connect } from 'react-redux';
|
2018-07-29 20:25:22 +03:00
|
|
|
import { pick } from 'ramda';
|
2018-07-30 21:31:48 +03:00
|
|
|
import { Card, CardBody, CardHeader } from 'reactstrap';
|
2018-07-29 20:25:22 +03:00
|
|
|
import { getShortUrlVisits } from './reducers/shortUrlVisits';
|
2018-07-30 21:31:48 +03:00
|
|
|
import VisitsParser from '../visits/services/VisitsParser';
|
2018-07-30 21:52:03 +03:00
|
|
|
import preloader from '@fortawesome/fontawesome-free-solid/faCircleNotch';
|
|
|
|
import FontAwesomeIcon from '@fortawesome/react-fontawesome';
|
2018-07-29 19:39:00 +03:00
|
|
|
|
|
|
|
export class ShortUrlsVisits extends React.Component {
|
2018-07-30 21:31:48 +03:00
|
|
|
state = { startDate: '', endDate: '' };
|
|
|
|
|
2018-07-29 20:25:22 +03:00
|
|
|
componentDidMount() {
|
2018-07-29 19:39:00 +03:00
|
|
|
const { match: { params } } = this.props;
|
2018-07-30 21:31:48 +03:00
|
|
|
this.props.getShortUrlVisits(params.shortCode, this.state);
|
2018-07-29 20:25:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-07-30 21:52:03 +03:00
|
|
|
const { match: { params }, selectedServer, visitsParser, shortUrlVisits: { visits, loading, error } } = this.props;
|
2018-07-29 20:25:22 +03:00
|
|
|
const serverUrl = selectedServer ? selectedServer.url : '';
|
|
|
|
const shortUrl = `${serverUrl}/${params.shortCode}`;
|
2018-07-30 22:12:06 +03:00
|
|
|
const generateGraphData = (stats, label, isBarChart) => ({
|
2018-07-30 21:31:48 +03:00
|
|
|
labels: Object.keys(stats),
|
2018-07-30 22:12:06 +03:00
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
label,
|
|
|
|
data: Object.values(stats),
|
|
|
|
backgroundColor: isBarChart ? 'rgba(200, 26, 80, 0.2)' : [],
|
|
|
|
borderColor: isBarChart ? 'rgba(200, 26, 80, 1)' : [],
|
|
|
|
}
|
|
|
|
]
|
2018-07-30 21:31:48 +03:00
|
|
|
});
|
2018-07-30 22:12:06 +03:00
|
|
|
const renderGraphCard = (title, stats, isBarChart = true) =>
|
|
|
|
<div className="col-md-6">
|
|
|
|
<Card className="mt-4">
|
|
|
|
<CardHeader>{title}</CardHeader>
|
|
|
|
<CardBody>
|
|
|
|
{!isBarChart && <Doughnut data={generateGraphData(stats, title, isBarChart)} />}
|
|
|
|
{isBarChart && <HorizontalBar data={generateGraphData(stats, title, isBarChart)} />}
|
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
</div>;
|
2018-07-30 21:52:03 +03:00
|
|
|
const renderContent = () => {
|
|
|
|
if (loading) {
|
|
|
|
return (
|
|
|
|
<div className="col-md-10 offset-md-1">
|
|
|
|
<Card className="bg-light mt-4" body>
|
|
|
|
<h3 className="text-center text-muted">
|
|
|
|
<FontAwesomeIcon icon={preloader} spin /> Loading...
|
|
|
|
</h3>
|
|
|
|
</Card>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<Card className="mt-4" body inverse color="danger">
|
|
|
|
An error occurred while loading visits :(
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
2018-07-30 21:31:48 +03:00
|
|
|
|
2018-07-30 21:52:03 +03:00
|
|
|
return (
|
2018-07-30 21:31:48 +03:00
|
|
|
<div className="row">
|
2018-07-30 22:12:06 +03:00
|
|
|
{renderGraphCard('Operating systems', visitsParser.processOsStats(visits), false)}
|
|
|
|
{renderGraphCard('Browsers', visitsParser.processBrowserStats(visits), false)}
|
|
|
|
{renderGraphCard('Countries', visitsParser.processCountriesStats(visits))}
|
|
|
|
{renderGraphCard('Referrers', visitsParser.processReferrersStats(visits))}
|
2018-07-29 20:25:22 +03:00
|
|
|
</div>
|
2018-07-30 21:52:03 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="short-urls-container">
|
|
|
|
<Card className="bg-light">
|
|
|
|
<CardBody>
|
|
|
|
<h2>Visit stats for <a target="_blank" href={shortUrl}>{shortUrl}</a></h2>
|
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
{renderContent()}
|
2018-07-29 20:25:22 +03:00
|
|
|
</div>
|
|
|
|
);
|
2018-07-29 19:39:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 21:31:48 +03:00
|
|
|
ShortUrlsVisits.defaultProps = {
|
|
|
|
visitsParser: VisitsParser
|
|
|
|
};
|
|
|
|
|
|
|
|
export default connect(pick(['selectedServer', 'shortUrlVisits']), {
|
2018-07-29 20:25:22 +03:00
|
|
|
getShortUrlVisits
|
|
|
|
})(ShortUrlsVisits);
|