2019-01-07 23:00:28 +03:00
|
|
|
import { isEmpty, mapObjIndexed, values } from 'ramda';
|
2018-08-26 00:39:27 +03:00
|
|
|
import React from 'react';
|
2020-04-04 00:00:57 +03:00
|
|
|
import { Button, Card, Collapse } from 'reactstrap';
|
2018-08-26 00:39:27 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2020-02-08 11:07:55 +03:00
|
|
|
import qs from 'qs';
|
2020-04-04 00:00:57 +03:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faChevronDown as chevronDown } from '@fortawesome/free-solid-svg-icons';
|
2020-01-14 21:59:25 +03:00
|
|
|
import DateRangeRow from '../utils/DateRangeRow';
|
2020-03-08 13:16:57 +03:00
|
|
|
import Message from '../utils/Message';
|
2020-03-28 19:33:23 +03:00
|
|
|
import { formatDate } from '../utils/helpers/date';
|
2018-10-29 01:04:52 +03:00
|
|
|
import SortableBarGraph from './SortableBarGraph';
|
2018-12-18 16:32:02 +03:00
|
|
|
import { shortUrlVisitsType } from './reducers/shortUrlVisits';
|
2019-03-04 20:14:45 +03:00
|
|
|
import VisitsHeader from './VisitsHeader';
|
2018-10-19 20:04:22 +03:00
|
|
|
import GraphCard from './GraphCard';
|
2018-12-18 16:32:02 +03:00
|
|
|
import { shortUrlDetailType } from './reducers/shortUrlDetail';
|
2020-04-04 00:00:57 +03:00
|
|
|
import VisitsTable from './VisitsTable';
|
2018-07-29 19:39:00 +03:00
|
|
|
|
2019-03-10 15:05:20 +03:00
|
|
|
const ShortUrlVisits = (
|
|
|
|
{ processStatsFromVisits },
|
|
|
|
OpenMapModalBtn
|
|
|
|
) => class ShortUrlVisits extends React.PureComponent {
|
2018-08-27 17:53:09 +03:00
|
|
|
static propTypes = {
|
2018-09-08 14:28:40 +03:00
|
|
|
match: PropTypes.shape({
|
|
|
|
params: PropTypes.object,
|
|
|
|
}),
|
2020-02-08 11:07:55 +03:00
|
|
|
location: PropTypes.shape({
|
|
|
|
search: PropTypes.string,
|
|
|
|
}),
|
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,
|
2019-03-08 21:40:43 +03:00
|
|
|
cancelGetShortUrlVisits: PropTypes.func,
|
2020-04-04 13:09:17 +03:00
|
|
|
matchMedia: PropTypes.func,
|
2018-08-27 17:53:09 +03:00
|
|
|
};
|
|
|
|
|
2020-04-04 00:00:57 +03:00
|
|
|
state = {
|
|
|
|
startDate: undefined,
|
|
|
|
endDate: undefined,
|
|
|
|
showTable: false,
|
2020-04-04 13:09:17 +03:00
|
|
|
tableIsSticky: false,
|
|
|
|
isMobileDevice: false,
|
2020-04-04 00:00:57 +03:00
|
|
|
};
|
|
|
|
|
2020-02-08 11:38:19 +03:00
|
|
|
loadVisits = (loadDetail = false) => {
|
|
|
|
const { match: { params }, location: { search }, getShortUrlVisits, getShortUrlDetail } = this.props;
|
2019-03-04 20:14:45 +03:00
|
|
|
const { shortCode } = params;
|
2020-02-08 12:07:34 +03:00
|
|
|
const { startDate, endDate } = mapObjIndexed(formatDate(), this.state);
|
|
|
|
const { domain } = qs.parse(search, { ignoreQueryPrefix: true });
|
2019-03-04 20:14:45 +03:00
|
|
|
|
2020-02-08 11:07:55 +03:00
|
|
|
// While the "page" is loaded, use the timestamp + filtering dates as memoization IDs for stats calculations
|
2019-03-04 20:19:50 +03:00
|
|
|
this.memoizationId = `${this.timeWhenMounted}_${shortCode}_${startDate}_${endDate}`;
|
2020-02-08 11:07:55 +03:00
|
|
|
getShortUrlVisits(shortCode, { startDate, endDate, domain });
|
2020-02-08 11:38:19 +03:00
|
|
|
|
|
|
|
if (loadDetail) {
|
|
|
|
getShortUrlDetail(shortCode, domain);
|
|
|
|
}
|
2018-07-31 23:04:20 +03:00
|
|
|
};
|
2018-07-30 21:31:48 +03:00
|
|
|
|
2020-04-04 13:09:17 +03:00
|
|
|
setIsMobileDevice = () => {
|
|
|
|
const { matchMedia = window.matchMedia } = this.props;
|
|
|
|
|
|
|
|
this.setState({ isMobileDevice: matchMedia('(max-width: 991px)').matches });
|
|
|
|
};
|
|
|
|
|
2018-07-29 20:25:22 +03:00
|
|
|
componentDidMount() {
|
2019-03-04 20:19:50 +03:00
|
|
|
this.timeWhenMounted = new Date().getTime();
|
2020-02-08 11:38:19 +03:00
|
|
|
this.loadVisits(true);
|
2020-04-04 13:09:17 +03:00
|
|
|
this.setIsMobileDevice();
|
|
|
|
window.addEventListener('resize', this.setIsMobileDevice);
|
2018-07-29 20:25:22 +03:00
|
|
|
}
|
|
|
|
|
2019-03-08 21:40:43 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.props.cancelGetShortUrlVisits();
|
2020-04-04 13:09:17 +03:00
|
|
|
window.removeEventListener('resize', this.setIsMobileDevice);
|
2019-03-08 21:40:43 +03:00
|
|
|
}
|
|
|
|
|
2018-07-29 20:25:22 +03:00
|
|
|
render() {
|
2018-12-18 16:32:02 +03:00
|
|
|
const { shortUrlVisits, shortUrlDetail } = this.props;
|
2020-04-04 00:00:57 +03:00
|
|
|
const { visits, loading, loadingLarge, error } = shortUrlVisits;
|
2018-09-01 12:08:27 +03:00
|
|
|
|
2018-09-01 12:26:35 +03:00
|
|
|
const renderVisitsContent = () => {
|
2018-07-30 21:52:03 +03:00
|
|
|
if (loading) {
|
2019-03-04 21:21:46 +03:00
|
|
|
const message = loadingLarge ? 'This is going to take a while... :S' : 'Loading...';
|
|
|
|
|
2020-03-08 13:16:57 +03:00
|
|
|
return <Message loading>{message}</Message>;
|
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)) {
|
2020-03-08 13:16:57 +03:00
|
|
|
return <Message>There are no visits matching current filter :(</Message>;
|
2018-07-31 23:04:20 +03:00
|
|
|
}
|
|
|
|
|
2019-03-04 20:14:45 +03:00
|
|
|
const { os, browsers, referrers, countries, cities, citiesForMap } = processStatsFromVisits(
|
|
|
|
{ id: this.memoizationId, visits }
|
|
|
|
);
|
2019-03-05 16:09:08 +03:00
|
|
|
const mapLocations = values(citiesForMap);
|
2019-03-04 20:14:45 +03:00
|
|
|
|
2018-07-30 21:52:03 +03:00
|
|
|
return (
|
2018-07-30 21:31:48 +03:00
|
|
|
<div className="row">
|
2019-01-07 13:53:14 +03:00
|
|
|
<div className="col-xl-4 col-lg-6">
|
2019-03-04 20:14:45 +03:00
|
|
|
<GraphCard title="Operating systems" stats={os} />
|
2018-09-01 12:08:27 +03:00
|
|
|
</div>
|
2019-01-07 13:53:14 +03:00
|
|
|
<div className="col-xl-4 col-lg-6">
|
2019-03-04 20:14:45 +03:00
|
|
|
<GraphCard title="Browsers" stats={browsers} />
|
2018-09-01 12:08:27 +03:00
|
|
|
</div>
|
2019-01-07 13:53:14 +03:00
|
|
|
<div className="col-xl-4">
|
|
|
|
<SortableBarGraph
|
2019-03-04 20:14:45 +03:00
|
|
|
stats={referrers}
|
2019-03-10 11:54:40 +03:00
|
|
|
withPagination={false}
|
2019-01-07 13:53:14 +03:00
|
|
|
title="Referrers"
|
|
|
|
sortingItems={{
|
|
|
|
name: 'Referrer name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-lg-6">
|
2018-10-29 01:04:52 +03:00
|
|
|
<SortableBarGraph
|
2019-03-04 20:14:45 +03:00
|
|
|
stats={countries}
|
2018-10-29 01:04:52 +03:00
|
|
|
title="Countries"
|
|
|
|
sortingItems={{
|
|
|
|
name: 'Country name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
/>
|
2018-09-01 12:08:27 +03:00
|
|
|
</div>
|
2019-01-07 13:53:14 +03:00
|
|
|
<div className="col-lg-6">
|
2018-10-29 01:04:52 +03:00
|
|
|
<SortableBarGraph
|
2019-03-04 20:14:45 +03:00
|
|
|
stats={cities}
|
2019-01-07 13:53:14 +03:00
|
|
|
title="Cities"
|
2019-03-10 14:09:54 +03:00
|
|
|
extraHeaderContent={(activeCities) =>
|
|
|
|
mapLocations.length > 0 &&
|
|
|
|
<OpenMapModalBtn modalTitle="Cities" locations={mapLocations} activeCities={activeCities} />
|
2019-03-10 12:56:36 +03:00
|
|
|
}
|
2018-10-29 01:04:52 +03:00
|
|
|
sortingItems={{
|
2019-01-07 13:53:14 +03:00
|
|
|
name: 'City name',
|
2018-10-29 01:04:52 +03:00
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
/>
|
2018-09-01 12:08:27 +03:00
|
|
|
</div>
|
2018-07-29 20:25:22 +03:00
|
|
|
</div>
|
2018-07-30 21:52:03 +03:00
|
|
|
);
|
|
|
|
};
|
2020-01-14 21:59:25 +03:00
|
|
|
const setDate = (dateField) => (date) => this.setState({ [dateField]: date }, this.loadVisits);
|
2018-07-30 21:52:03 +03:00
|
|
|
|
|
|
|
return (
|
2020-03-05 13:11:26 +03:00
|
|
|
<React.Fragment>
|
2020-01-15 20:31:28 +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">
|
2020-04-04 00:00:57 +03:00
|
|
|
<div className="row flex-md-row-reverse">
|
|
|
|
<div className="col-lg-8 col-xl-6">
|
|
|
|
<DateRangeRow
|
|
|
|
startDate={this.state.startDate}
|
|
|
|
endDate={this.state.endDate}
|
|
|
|
onStartDateChange={setDate('startDate')}
|
|
|
|
onEndDateChange={setDate('endDate')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-lg-4 col-xl-6 mt-4 mt-lg-0">
|
|
|
|
{visits.length > 0 && (
|
2020-04-04 13:09:17 +03:00
|
|
|
<Button
|
|
|
|
outline
|
|
|
|
block={this.state.isMobileDevice}
|
|
|
|
onClick={() => this.setState(({ showTable }) => ({ showTable: !showTable }))}
|
|
|
|
>
|
2020-04-04 00:00:57 +03:00
|
|
|
Show table <FontAwesomeIcon icon={chevronDown} rotation={this.state.showTable ? 180 : undefined} />
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-07-31 23:04:20 +03:00
|
|
|
</section>
|
|
|
|
|
2020-04-04 00:00:57 +03:00
|
|
|
{!loading && visits.length > 0 && (
|
2020-04-04 13:09:17 +03:00
|
|
|
<Collapse
|
|
|
|
isOpen={this.state.showTable}
|
|
|
|
|
|
|
|
// Enable stickiness only when there's no CSS animation, to avoid weird rendering effects
|
|
|
|
onEntered={() => this.setState({ tableIsSticky: true })}
|
|
|
|
onExiting={() => this.setState({ tableIsSticky: false })}
|
|
|
|
>
|
|
|
|
<VisitsTable visits={visits} isSticky={this.state.tableIsSticky} />
|
2020-04-04 00:00:57 +03:00
|
|
|
</Collapse>
|
|
|
|
)}
|
|
|
|
|
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>
|
2020-03-05 13:11:26 +03:00
|
|
|
</React.Fragment>
|
2018-07-29 20:25:22 +03:00
|
|
|
);
|
2018-07-29 19:39:00 +03:00
|
|
|
}
|
2018-12-18 16:32:02 +03:00
|
|
|
};
|
2018-08-26 00:39:27 +03:00
|
|
|
|
2018-12-18 16:32:02 +03:00
|
|
|
export default ShortUrlVisits;
|