2018-08-26 00:39:27 +03:00
|
|
|
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 { connect } from 'react-redux';
|
2018-09-01 12:08:27 +03:00
|
|
|
import { Card } from 'reactstrap';
|
2018-08-26 00:39:27 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import DateInput from '../common/DateInput';
|
2018-09-01 11:33:16 +03:00
|
|
|
import MutedMessage from '../utils/MuttedMessage';
|
|
|
|
import { getShortUrlVisits, shortUrlVisitsType } from './reducers/shortUrlVisits';
|
2018-08-25 00:55:53 +03:00
|
|
|
import {
|
|
|
|
processBrowserStats,
|
|
|
|
processCountriesStats,
|
2018-09-01 12:08:27 +03:00
|
|
|
processOsStats,
|
2018-08-25 00:55:53 +03:00
|
|
|
processReferrersStats,
|
2018-09-01 11:33:16 +03:00
|
|
|
} from './services/VisitsParser';
|
2018-09-01 12:08:27 +03:00
|
|
|
import { VisitsHeader } from './VisitsHeader';
|
|
|
|
import { GraphCard } from './GraphCard';
|
2018-09-01 12:26:35 +03:00
|
|
|
import { getShortUrlDetail, shortUrlDetailType } from './reducers/shortUrlDetail';
|
|
|
|
import './ShortUrlVisits.scss';
|
2018-07-29 19:39:00 +03:00
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
export class ShortUrlsVisitsComponent extends React.Component {
|
2018-08-27 17:53:09 +03:00
|
|
|
static propTypes = {
|
|
|
|
processOsStats: PropTypes.func,
|
|
|
|
processBrowserStats: PropTypes.func,
|
|
|
|
processCountriesStats: PropTypes.func,
|
|
|
|
processReferrersStats: PropTypes.func,
|
2018-09-08 14:28:40 +03:00
|
|
|
match: PropTypes.shape({
|
|
|
|
params: PropTypes.object,
|
|
|
|
}),
|
2018-09-01 12:26:35 +03:00
|
|
|
getShortUrlVisits: PropTypes.func,
|
2018-08-27 17:53:09 +03:00
|
|
|
shortUrlVisits: shortUrlVisitsType,
|
2018-09-01 12:26:35 +03:00
|
|
|
getShortUrlDetail: PropTypes.func,
|
|
|
|
shortUrlDetail: shortUrlDetailType,
|
2018-08-27 17:53:09 +03:00
|
|
|
};
|
|
|
|
static defaultProps = {
|
|
|
|
processOsStats,
|
|
|
|
processBrowserStats,
|
|
|
|
processCountriesStats,
|
|
|
|
processReferrersStats,
|
|
|
|
};
|
|
|
|
|
2018-07-31 23:04:20 +03:00
|
|
|
state = { startDate: undefined, endDate: undefined };
|
2018-08-01 19:32:21 +03:00
|
|
|
loadVisits = () => {
|
2018-08-26 00:39:27 +03:00
|
|
|
const { match: { params }, getShortUrlVisits } = this.props;
|
|
|
|
|
|
|
|
getShortUrlVisits(params.shortCode, mapObjIndexed(
|
|
|
|
(value) => value && value.format ? value.format('YYYY-MM-DD') : value,
|
2018-08-01 19:32:21 +03:00
|
|
|
this.state
|
2018-08-26 00:39:27 +03:00
|
|
|
));
|
2018-07-31 23:04:20 +03:00
|
|
|
};
|
2018-07-30 21:31:48 +03:00
|
|
|
|
2018-07-29 20:25:22 +03:00
|
|
|
componentDidMount() {
|
2018-09-01 12:26:35 +03:00
|
|
|
const { match: { params }, getShortUrlDetail } = this.props;
|
|
|
|
|
2018-07-31 23:04:20 +03:00
|
|
|
this.loadVisits();
|
2018-09-01 12:26:35 +03:00
|
|
|
getShortUrlDetail(params.shortCode);
|
2018-07-29 20:25:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-07-30 22:34:06 +03:00
|
|
|
const {
|
2018-08-25 00:55:53 +03:00
|
|
|
processOsStats,
|
|
|
|
processBrowserStats,
|
|
|
|
processCountriesStats,
|
|
|
|
processReferrersStats,
|
2018-09-01 12:08:27 +03:00
|
|
|
shortUrlVisits,
|
2018-09-01 12:26:35 +03:00
|
|
|
shortUrlDetail,
|
2018-07-30 22:34:06 +03:00
|
|
|
} = this.props;
|
2018-09-01 12:08:27 +03:00
|
|
|
|
2018-09-01 12:26:35 +03:00
|
|
|
const renderVisitsContent = () => {
|
|
|
|
const { visits, loading, error } = shortUrlVisits;
|
|
|
|
|
2018-07-30 21:52:03 +03:00
|
|
|
if (loading) {
|
2018-07-31 23:04:20 +03:00
|
|
|
return <MutedMessage><FontAwesomeIcon icon={preloader} spin /> Loading...</MutedMessage>;
|
2018-07-30 21:52:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-31 23:04:20 +03:00
|
|
|
if (isEmpty(visits)) {
|
2018-09-01 12:26:35 +03:00
|
|
|
return <MutedMessage>There are no visits matching current filter :(</MutedMessage>;
|
2018-07-31 23:04:20 +03:00
|
|
|
}
|
|
|
|
|
2018-07-30 21:52:03 +03:00
|
|
|
return (
|
2018-07-30 21:31:48 +03:00
|
|
|
<div className="row">
|
2018-09-01 12:08:27 +03:00
|
|
|
<div className="col-md-6">
|
|
|
|
<GraphCard title="Operating systems" stats={processOsStats(visits)} />
|
|
|
|
</div>
|
|
|
|
<div className="col-md-6">
|
|
|
|
<GraphCard title="Browsers" stats={processBrowserStats(visits)} />
|
|
|
|
</div>
|
|
|
|
<div className="col-md-6">
|
|
|
|
<GraphCard title="Countries" stats={processCountriesStats(visits)} isBarChart />
|
|
|
|
</div>
|
|
|
|
<div className="col-md-6">
|
|
|
|
<GraphCard title="Referrers" stats={processReferrersStats(visits)} isBarChart />
|
|
|
|
</div>
|
2018-07-29 20:25:22 +03:00
|
|
|
</div>
|
2018-07-30 21:52:03 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2018-08-16 19:59:00 +03:00
|
|
|
<div className="shlink-container">
|
2018-09-08 10:31:44 +03:00
|
|
|
<VisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} />
|
2018-07-30 21:52:03 +03:00
|
|
|
|
2018-08-09 20:50:22 +03:00
|
|
|
<section className="mt-4">
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-xl-3 col-lg-4 col-md-6 offset-xl-6 offset-lg-4">
|
|
|
|
<DateInput
|
|
|
|
selected={this.state.startDate}
|
|
|
|
placeholderText="Since"
|
2018-08-09 21:13:46 +03:00
|
|
|
isClearable
|
2018-09-05 21:17:46 +03:00
|
|
|
maxDate={this.state.endDate}
|
2018-08-26 00:39:27 +03:00
|
|
|
onChange={(date) => this.setState({ startDate: date }, () => this.loadVisits())}
|
2018-08-09 20:50:22 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-xl-3 col-lg-4 col-md-6">
|
|
|
|
<DateInput
|
2018-10-07 10:22:15 +03:00
|
|
|
className="short-url-visits__date-input"
|
2018-08-09 20:50:22 +03:00
|
|
|
selected={this.state.endDate}
|
|
|
|
placeholderText="Until"
|
2018-08-09 21:13:46 +03:00
|
|
|
isClearable
|
2018-09-05 21:17:46 +03:00
|
|
|
minDate={this.state.startDate}
|
2018-08-26 00:39:27 +03:00
|
|
|
onChange={(date) => this.setState({ endDate: date }, () => this.loadVisits())}
|
2018-08-09 20:50:22 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-07-31 23:04:20 +03:00
|
|
|
</section>
|
|
|
|
|
2018-07-30 22:34:06 +03:00
|
|
|
<section>
|
2018-09-01 12:26:35 +03:00
|
|
|
{renderVisitsContent()}
|
2018-07-30 22:34:06 +03:00
|
|
|
</section>
|
2018-07-29 20:25:22 +03:00
|
|
|
</div>
|
|
|
|
);
|
2018-07-29 19:39:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
const ShortUrlsVisits = connect(
|
2018-09-08 10:31:44 +03:00
|
|
|
pick([ 'shortUrlVisits', 'shortUrlDetail' ]),
|
2018-09-01 12:26:35 +03:00
|
|
|
{ getShortUrlVisits, getShortUrlDetail }
|
2018-08-26 00:39:27 +03:00
|
|
|
)(ShortUrlsVisitsComponent);
|
|
|
|
|
|
|
|
export default ShortUrlsVisits;
|