Details page.

This commit is contained in:
Mike Cao 2020-07-31 19:05:14 -07:00
parent 5b45301178
commit ac2612924e
12 changed files with 312 additions and 10 deletions

15
pages/[id].js Normal file
View file

@ -0,0 +1,15 @@
import React from 'react';
import { useRouter } from 'next/router';
import Layout from 'components/Layout';
import WebsiteDetails from '../components/WebsiteDetails';
export default function DetailsPage() {
const router = useRouter();
const { id } = router.query;
return (
<Layout>
<WebsiteDetails websiteId={id} />
</Layout>
);
}

View file

@ -0,0 +1,12 @@
import { getWebsite } from 'lib/db';
import { useAuth } from 'lib/middleware';
export default async (req, res) => {
await useAuth(req, res);
const { id } = req.query;
const website = await getWebsite(id);
return res.status(200).json(website);
};

View file

@ -2,12 +2,14 @@ import moment from 'moment-timezone';
import { getPageviewData } from 'lib/db';
import { useAuth } from 'lib/middleware';
const unitTypes = ['month', 'hour', 'day'];
export default async (req, res) => {
await useAuth(req, res);
const { id, start_at, end_at, unit, tz } = req.query;
if (!moment.tz.zone(tz) || !['month', 'hour', 'day'].includes(unit)) {
if (!moment.tz.zone(tz) || !unitTypes.includes(unit)) {
return res.status(400).end();
}

View file

@ -0,0 +1,21 @@
import { getRankings } from 'lib/db';
import { useAuth } from 'lib/middleware';
const sessionColumns = ['browser', 'os', 'screen'];
const pageviewColumns = ['url', 'referrer'];
export default async (req, res) => {
await useAuth(req, res);
const { id, type, start_at, end_at } = req.query;
if (!sessionColumns.includes(type) && !pageviewColumns.includes(type)) {
return res.status(400).end();
}
const table = sessionColumns.includes(type) ? 'session' : 'pageview';
const rankings = await getRankings(+id, new Date(+start_at), new Date(+end_at), type, table);
return res.status(200).json(rankings);
};