2020-07-29 04:04:45 +02:00
|
|
|
import React, { useState, useEffect, useMemo } from 'react';
|
2020-07-28 10:17:45 +02:00
|
|
|
import PageviewsChart from './PageviewsChart';
|
|
|
|
import { get } from 'lib/web';
|
2020-07-29 04:04:45 +02:00
|
|
|
import { getDateArray, getTimezone } from 'lib/date';
|
|
|
|
import WebsiteSummary from './WebsiteSummary';
|
2020-07-30 08:25:52 +02:00
|
|
|
import styles from './WebsiteStats.module.css';
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2020-07-30 08:25:52 +02:00
|
|
|
export default function WebsiteStats({ title, websiteId, startDate, endDate, unit }) {
|
2020-07-28 10:17:45 +02:00
|
|
|
const [data, setData] = useState();
|
2020-07-29 04:04:45 +02:00
|
|
|
const [pageviews, uniques] = useMemo(() => {
|
|
|
|
if (data) {
|
|
|
|
return [
|
|
|
|
getDateArray(data.pageviews, startDate, endDate, unit),
|
|
|
|
getDateArray(data.uniques, startDate, endDate, unit),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [[], []];
|
|
|
|
}, [data]);
|
2020-07-28 10:17:45 +02:00
|
|
|
|
|
|
|
async function loadData() {
|
|
|
|
setData(
|
|
|
|
await get(`/api/website/${websiteId}/pageviews`, {
|
|
|
|
start_at: +startDate,
|
|
|
|
end_at: +endDate,
|
|
|
|
unit,
|
|
|
|
tz: getTimezone(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
loadData();
|
2020-07-29 04:04:45 +02:00
|
|
|
}, [websiteId, startDate, endDate, unit]);
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2020-07-29 04:04:45 +02:00
|
|
|
return (
|
2020-07-30 08:25:52 +02:00
|
|
|
<div className={styles.container}>
|
|
|
|
<div className={styles.title}>{title}</div>
|
2020-07-29 09:16:02 +02:00
|
|
|
<WebsiteSummary websiteId={websiteId} startDate={startDate} endDate={endDate} />
|
2020-07-29 06:50:29 +02:00
|
|
|
<PageviewsChart data={{ pageviews, uniques }} unit={unit} />
|
2020-07-29 04:04:45 +02:00
|
|
|
</div>
|
|
|
|
);
|
2020-07-28 10:17:45 +02:00
|
|
|
}
|