mirror of
https://github.com/r-freeman/portfolio.git
synced 2024-11-22 18:15:41 +00:00
Added total article views to dashboard
This commit is contained in:
parent
0e6c2b9a33
commit
a7d518460a
18
pages/api/views/index.ts
Normal file
18
pages/api/views/index.ts
Normal 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})
|
||||||
|
}
|
||||||
|
}
|
@ -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>
|
||||||
</>
|
</>
|
||||||
|
Loading…
Reference in New Issue
Block a user