mirror of
https://github.com/r-freeman/portfolio.git
synced 2024-11-22 19:05:41 +00:00
Refactored views component and removed image
This commit is contained in:
parent
2f098f1429
commit
a7784789fd
Binary file not shown.
Before Width: | Height: | Size: 291 KiB |
@ -1,33 +1,27 @@
|
||||
import {ElementType, useEffect} from 'react'
|
||||
import useSWR from 'swr'
|
||||
import useSWRImmutable from 'swr/immutable'
|
||||
import fetcher from '@/lib/fetcher'
|
||||
|
||||
|
||||
function numberFormat(value: number) {
|
||||
return new Intl.NumberFormat('en', {
|
||||
notation: 'compact'
|
||||
}).format(value)
|
||||
}
|
||||
|
||||
type ViewsType = {
|
||||
views: string
|
||||
}
|
||||
const updateViews = (slug: string) => fetcher(`/api/views/${slug}`, {method: 'POST'})
|
||||
|
||||
export function Views({as: Component = 'span', slug}: { as?: ElementType, slug: string }) {
|
||||
const {data} = useSWR<ViewsType>(`/api/views/${slug}`, fetcher)
|
||||
const {data} = useSWRImmutable(`/api/views/${slug}`, fetcher)
|
||||
const views = Number(data?.views)
|
||||
|
||||
useEffect(() => {
|
||||
const registerView = () =>
|
||||
fetch(`/api/views/${slug}`, {
|
||||
method: 'POST'
|
||||
})
|
||||
|
||||
registerView().then(r => r)
|
||||
updateViews(slug).then(r => r)
|
||||
}, [slug])
|
||||
|
||||
return (
|
||||
<Component>
|
||||
{views > 0 ? ` · ${numberFormat(views)} views` : ''}
|
||||
{` · ${views > 0 ? numberFormat(views) : '---'} views`}
|
||||
</Component>
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user