Added total article views to dashboard

This commit is contained in:
r-freeman 2023-01-28 15:29:17 +00:00
parent 0e6c2b9a33
commit a7d518460a
2 changed files with 32 additions and 1 deletions

18
pages/api/views/index.ts Normal file
View File

@ -0,0 +1,18 @@
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})
}
}

View File

@ -1,9 +1,11 @@
import Head from 'next/head' import Head from 'next/head'
import {GetStaticProps} from 'next'
import useSWR from 'swr'
import {SimpleLayout} from '@/components/SimpleLayout' import {SimpleLayout} from '@/components/SimpleLayout'
import {Card} from '@/components/Card' import {Card} from '@/components/Card'
import {numberFormat} from '@/lib/numberFormat' import {numberFormat} from '@/lib/numberFormat'
import fetcher from '@/lib/fetcher'
import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github' import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github'
import {GetStaticProps} from 'next'
type DashboardProps = { type DashboardProps = {
totalRepos: number totalRepos: number
@ -12,6 +14,9 @@ type DashboardProps = {
} }
export default function Dashboard({totalRepos, totalFollowers, totalStars}: DashboardProps) { export default function Dashboard({totalRepos, totalFollowers, totalStars}: DashboardProps) {
const {data} = useSWR('/api/views/', fetcher)
const totalArticleViews = Number(data?.views)
return ( return (
<> <>
<Head> <Head>
@ -62,6 +67,14 @@ export default function Dashboard({totalRepos, totalFollowers, totalStars}: Dash
{numberFormat(totalStars)} {numberFormat(totalStars)}
</Card.Description> </Card.Description>
</Card> </Card>
<Card as="li">
<h2 className="text-base font-semibold transition group-hover:text-indigo-500 text-zinc-800 dark:text-zinc-400">
<Card.Link href="https://github.com/r-freeman">Total Article Views</Card.Link>
</h2>
<Card.Description className="text-zinc-800 dark:text-zinc-100 font-semibold text-5xl">
{totalArticleViews > 0 ? numberFormat(totalArticleViews) : '—'}
</Card.Description>
</Card>
</ul> </ul>
</SimpleLayout> </SimpleLayout>
</> </>