diff --git a/.env.example b/.env.example index 2326e02..d04effb 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -CONTENTFUL_SPACE_ID = '' -CONTENTFUL_ACCESS_KEY = '' +NEXT_PUBLIC_CONTENTFUL_ACCESS_KEY = '' +NEXT_PUBLIC_CONTENTFUL_SPACE_ID = '' NEXT_PUBLIC_CUSDIS_APPID = '' NEXT_PUBLIC_SITE_URL = '' \ No newline at end of file diff --git a/README.md b/README.md index 07806cb..4bd8a4b 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ First, copy `.env.example` and paste as `.env.local`, fill all field: ```bash -CONTENTFUL_SPACE_ID = '' -CONTENTFUL_ACCESS_KEY = '' +NEXT_PUBLIC_CONTENTFUL_SPACE_ID = '' +NEXT_PUBLIC_CONTENTFUL_ACCESS_KEY = '' NEXT_PUBLIC_CUSDIS_APPID = '' NEXT_PUBLIC_SITE_URL = '' ``` diff --git a/components/blog/Card.js b/components/blog/Card.js index ef5a7ba..eafeb09 100644 --- a/components/blog/Card.js +++ b/components/blog/Card.js @@ -2,51 +2,52 @@ import Link from 'next/link'; import { Badge, Heading, HStack, Text, VStack } from '@chakra-ui/react'; import { format } from 'date-fns'; -const Card = ({ createdAt, slug, summary, tags, title }) => ( - - - - {format(new Date(createdAt), 'EEEE, MMMM do, yyyy')} - - +const Card = ({ createdAt, router, slug, summary, tags, title }) => ( + + + {format(new Date(createdAt), 'EEEE, MMMM do, yyyy')} + + + {title} - - {summary} - - - {tags.map((tag) => ( - - {tag.sys.id} - - ))} - - - + + + {summary} + + + {tags.map((tag) => ( + router.replace(`/blog?tag=${tag.sys.id}`)} + variant="solid" + py={1} + px={2} + bg="gray.700" + fontWeight="500" + textTransform="capitalize" + rounded="md" + shadow="inner" + _hover={{ textDecor: 'underline', cursor: 'pointer' }} + > + {tag.sys.id} + + ))} + + ); export default Card; diff --git a/pages/blog/[slug].js b/pages/blog/[slug].js index 42cb57f..19363b1 100644 --- a/pages/blog/[slug].js +++ b/pages/blog/[slug].js @@ -1,4 +1,5 @@ import Image from 'next/image'; +import { useRouter } from 'next/router'; import { NextSeo } from 'next-seo'; import { createClient } from 'contentful'; import { Badge, Box, Divider, Heading, HStack, Text } from '@chakra-ui/react'; @@ -8,8 +9,8 @@ import MarkdownComponent from '../../components/blog/MarkdownComponent'; import Comments from '../../components/blog/Comments'; const client = createClient({ - space: process.env.CONTENTFUL_SPACE_ID, - accessToken: process.env.CONTENTFUL_ACCESS_KEY, + space: process.env.NEXT_PUBLIC_CONTENTFUL_SPACE_ID, + accessToken: process.env.NEXT_PUBLIC_CONTENTFUL_ACCESS_KEY, }); export const getStaticPaths = async () => { @@ -40,6 +41,7 @@ const DetailBlog = ({ blog }) => { const { description, file } = thumbnail.fields; const { width, height } = file.details.image; const url = `${process.env.NEXT_PUBLIC_SITE_URL}/blog/${slug}`; + const router = useRouter(); return ( <> @@ -101,6 +103,7 @@ const DetailBlog = ({ blog }) => { {tags.map((tag) => ( router.push(`/blog?tag=${tag.sys.id}`)} py={1} px={2} bg="brand.light" @@ -109,6 +112,12 @@ const DetailBlog = ({ blog }) => { textTransform="capitalize" rounded="md" shadow="inner" + transition="all 0.2s ease-in-out" + _hover={{ + bg: 'gray.200', + textDecor: 'underline', + cursor: 'pointer', + }} > {tag.sys.id} diff --git a/pages/blog/index.js b/pages/blog/index.js index 334abfd..6864e0d 100644 --- a/pages/blog/index.js +++ b/pages/blog/index.js @@ -1,14 +1,17 @@ +import { useEffect, useState } from 'react'; +import { useRouter } from 'next/router'; import { NextSeo } from 'next-seo'; import { createClient } from 'contentful'; import { Divider, Heading, Text, VStack } from '@chakra-ui/react'; import Card from '../../components/blog/Card'; +const client = createClient({ + space: process.env.NEXT_PUBLIC_CONTENTFUL_SPACE_ID, + accessToken: process.env.NEXT_PUBLIC_CONTENTFUL_ACCESS_KEY, +}); + export const getStaticProps = async () => { - const client = createClient({ - space: process.env.CONTENTFUL_SPACE_ID, - accessToken: process.env.CONTENTFUL_ACCESS_KEY, - }); const res = await client.getEntries({ content_type: 'blog' }); return { props: { blogs: res.items } }; @@ -18,6 +21,24 @@ const Blog = ({ blogs }) => { const title = 'Blog'; const url = `${process.env.NEXT_PUBLIC_SITE_URL}/blog`; + const router = useRouter(); + const { query } = router; + const [blogLists, setBlogLists] = useState(blogs); + + useEffect(() => { + if (!query) return; + + const fetchBlogByTag = async () => { + const res = await client.getEntries({ + content_type: 'blog', + 'metadata.tags.sys.id[in]': query.tag, + }); + setBlogLists(res.items); + }; + + fetchBlogByTag(); + }, [query]); + return ( <> @@ -30,7 +51,7 @@ const Blog = ({ blogs }) => { - {blogs.map((blog) => { + {blogLists.map((blog) => { const { slug, summary } = blog.fields; const { tags } = blog.metadata; const { createdAt, id } = blog.sys; @@ -43,6 +64,7 @@ const Blog = ({ blogs }) => { title={blog.fields.title} tags={tags} createdAt={createdAt} + router={router} /> ); })}