forked from amanintech/amansharma.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useEffect } from 'react' | ||
import useSWR from 'swr' | ||
import format from 'comma-number' | ||
|
||
export function ViewCounter({ slug }) { | ||
const { data } = useSWR(`/api/views/${slug}`, (args) => | ||
fetch(args).then((res) => res.json()) | ||
) | ||
const views = data?.total | ||
|
||
useEffect(() => { | ||
const registerView = () => | ||
fetch(`/api/views/${slug}`, { | ||
method: 'POST' | ||
}) | ||
|
||
registerView() | ||
}, [slug]) | ||
|
||
return <>{views ? format(views) : '––'} views</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as firestore from '@google-cloud/firestore' | ||
import * as db from '@lib/db' | ||
|
||
export default async function handler(req, res) { | ||
if (req.method === 'POST') { | ||
const increment = firestore.FieldValue.increment(1) | ||
|
||
const allRef = db.pageviews.doc('all') | ||
const slugRef = db.pageviews.doc(req.query.slug) | ||
|
||
const [, , views] = await Promise.all([ | ||
allRef.set({ count: increment }, { merge: true }), | ||
slugRef.set({ count: increment }, { merge: true }), | ||
(await slugRef.get()).data().count | ||
]) | ||
|
||
return res.status(200).json({ | ||
total: views | ||
}) | ||
} | ||
|
||
if (req.method === 'GET') { | ||
const snapshot = await db.pageviews.doc(req.query.slug).get() | ||
|
||
const views = snapshot.data()?.count || 0 | ||
|
||
return res.status(200).json({ total: views }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters