From 99d70b1d984f4170d1094c838c0af96cfb437ebd Mon Sep 17 00:00:00 2001 From: r-freeman Date: Sat, 28 Jan 2023 21:59:01 +0000 Subject: [PATCH] Added total articles card to dashboard --- lib/dashboard.ts | 14 ++++++++++++++ lib/views.ts | 13 +++++++++++++ pages/api/views/index.ts | 18 ------------------ pages/dashboard.tsx | 13 ------------- 4 files changed, 27 insertions(+), 31 deletions(-) create mode 100644 lib/views.ts delete mode 100644 pages/api/views/index.ts diff --git a/lib/dashboard.ts b/lib/dashboard.ts index 3a0ce3f..15d941e 100644 --- a/lib/dashboard.ts +++ b/lib/dashboard.ts @@ -1,4 +1,6 @@ import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github' +import {getAllArticles} from '@/lib/getAllArticles' +import {getViews} from '@/lib/views' export async function dashboard() { const [totalRepos, totalFollowers] = await Promise.all([ @@ -7,6 +9,8 @@ export async function dashboard() { ]) const totalStars = await getTotalStars(totalRepos) + const articles = await getAllArticles() + const {views} = await getViews() return [ { @@ -23,6 +27,16 @@ export async function dashboard() { title: "GitHub Stars", total: totalStars, href: "https://github.com/r-freeman/" + }, + { + title: "Total Articles", + total: articles.length, + href: "/writing" + }, + { + title: "Total Article Views", + total: views, + href: "/writing" } ] } \ No newline at end of file diff --git a/lib/views.ts b/lib/views.ts new file mode 100644 index 0000000..1dead0a --- /dev/null +++ b/lib/views.ts @@ -0,0 +1,13 @@ +import {prisma} from '@/lib/prisma' + +export async function getViews() { + const totalViews: { _sum: { count: any } } = await prisma.views.aggregate({ + _sum: { + count: true + } + }) + + return { + views: totalViews._sum.count.toString() + } +} \ No newline at end of file diff --git a/pages/api/views/index.ts b/pages/api/views/index.ts deleted file mode 100644 index bb5443a..0000000 --- a/pages/api/views/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -import type {NextApiRequest, NextApiResponse} from 'next' -import {prisma} from '@/lib/prisma' - -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - try { - const totalViews: { _sum: { count: any } } = await prisma.views.aggregate({ - _sum: { - count: true - } - }) - - return res.status(200).json({ - views: totalViews._sum.count.toString() - }) - } catch (e: any) { - return res.status(500).json({message: e.message}) - } -} \ No newline at end of file diff --git a/pages/dashboard.tsx b/pages/dashboard.tsx index ee9d796..cf9f5ab 100644 --- a/pages/dashboard.tsx +++ b/pages/dashboard.tsx @@ -1,11 +1,9 @@ import Head from 'next/head' import {GetStaticProps} from 'next' -import useSWR from 'swr' import {SimpleLayout} from '@/components/SimpleLayout' import {Card} from '@/components/Card' import {numberFormat} from '@/lib/numberFormat' import {dashboard} from '@/lib/dashboard' -import fetcher from '@/lib/fetcher' type CardProps = { title: string @@ -14,9 +12,6 @@ type CardProps = { } export default function Dashboard({cards}: { cards: CardProps[] }) { - const {data} = useSWR('/api/views/', fetcher) - const totalArticleViews = Number(data?.views) - return ( <> @@ -53,14 +48,6 @@ export default function Dashboard({cards}: { cards: CardProps[] }) { ))} - -

- Total Article Views -

- - {totalArticleViews > 0 ? numberFormat(totalArticleViews) : '—'} - -