From 9fba6918e553c1193cd2d607756faa7909e2a0c9 Mon Sep 17 00:00:00 2001 From: William Grant Date: Thu, 3 Mar 2022 17:23:18 +1100 Subject: [PATCH] support contentful's native tags --- components/TagBlock.tsx | 6 +++--- pages/tag/[...slug].tsx | 12 ++++++------ services/cms.tsx | 22 +++++++++++----------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/components/TagBlock.tsx b/components/TagBlock.tsx index daffe4e..6ea6aae 100644 --- a/components/TagBlock.tsx +++ b/components/TagBlock.tsx @@ -1,5 +1,5 @@ -import classNames from 'classnames'; import React from 'react'; +import classNames from 'classnames'; interface Props { tag: string; @@ -9,7 +9,7 @@ interface Props { export function TagBlock(props: Props) { const { tag, size = 'small', classes } = props; - const href = `/tag/${tag.toLowerCase()}`; + const href = `/tag/${tag}`; return ( - {tag.toLowerCase()} + {tag} ); } diff --git a/pages/tag/[...slug].tsx b/pages/tag/[...slug].tsx index 37a1d00..749f6b5 100644 --- a/pages/tag/[...slug].tsx +++ b/pages/tag/[...slug].tsx @@ -1,5 +1,6 @@ import { CMS, METADATA } from '@/constants'; import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next'; +import { IPost, ITagList } from '@/types/cms'; import { PageType, setPageType } from '@/state/navigation'; import { ReactElement, useEffect } from 'react'; @@ -9,7 +10,6 @@ import { CmsApi } from '@/services/cms'; import { Contained } from '@/components/Contained'; import CustomHead from '@/components/CustomHead'; import { IPath } from '@/types'; -import { IPost } from '@/types/cms'; import Pagination from '@/components/Pagination'; import { TagBlock } from '@/components/TagBlock'; import classNames from 'classnames'; @@ -38,7 +38,7 @@ export default function Tag(props: Props): ReactElement { useEffect(() => { dispatch(setPageType(PageType.BLOG)); - }, []); + }, [dispatch]); return ( <> @@ -177,16 +177,16 @@ export const getStaticProps: GetStaticProps = async ( export const getStaticPaths: GetStaticPaths = async () => { const cms = new CmsApi(); - const tags = Object.values(await cms.fetchTagList()); + const tags: ITagList = await cms.fetchTagList(); const paths: IPath[] = []; - for (let i = 0; i < tags.length; i++) { - const { entries, total } = await cms.fetchBlogEntriesByTag(tags[i]); + for (let tag of Object.values(tags)) { + const { entries, total } = await cms.fetchBlogEntriesByTag(tag); const pageCount = Math.ceil(total / CMS.BLOG_RESULTS_PER_PAGE); const _paths = []; for (let j = 1; j <= pageCount; j++) { - _paths.push({ params: { slug: [tags[i], String(j)] } }); + _paths.push({ params: { slug: [tag, String(j)] } }); } paths.push(..._paths); diff --git a/services/cms.tsx b/services/cms.tsx index a3827ad..bfb25ad 100644 --- a/services/cms.tsx +++ b/services/cms.tsx @@ -43,15 +43,10 @@ export class CmsApi { } public async fetchTagList(): Promise { - // TODO Migrate to Contentful Tag System - const { entries, total } = await this.fetchBlogEntries(); + const _tags = await this.client.getTags(); const tags: ITagList = {}; - entries.forEach(entry => { - entry.tags.forEach(tag => { - if (!tags[tag]) { - tags[tag] = tag; - } - }); + _tags.items.forEach(tag => { + tags[tag.sys.id] = tag.name; }); return tags; } @@ -79,10 +74,15 @@ export class CmsApi { quantity = CMS.BLOG_RESULTS_PER_PAGE_TAGGED, page = 1, ): Promise { + const taglist = await this.fetchTagList(); + const id = Object.entries(taglist).filter(([_, value]) => { + return tag === value; + })[0][0]; + const _entries = await this.client.getEntries({ content_type: 'post', order: '-fields.date', - 'fields.tags[in]': tag, + 'metadata.tags.sys.id[in]': id, limit: quantity, skip: (page - 1) * quantity, }); @@ -102,11 +102,11 @@ export class CmsApi { quantity = CMS.BLOG_RESULTS_PER_PAGE, page = 1, ): Promise { - const DEV_UPDATE_TAG = 'dev-update'; + const DEV_UPDATE_TAG = 'devUpdate'; const _entries = await this.client.getEntries({ content_type: 'post', // only fetch blog post entry order: '-fields.date', - 'fields.tags[ne]': DEV_UPDATE_TAG, // Exclude blog posts with the "dev-update" tag + 'metadata.tags.sys.id[nin]': DEV_UPDATE_TAG, // Exclude blog posts with the "dev-update" tag limit: quantity, skip: (page - 1) * quantity, });