From 8efad3cd9d32a0dea2e6646e2647f80a15e2eefb Mon Sep 17 00:00:00 2001 From: ko-shiro-hap Date: Wed, 10 May 2023 10:26:42 +0900 Subject: [PATCH 1/7] =?UTF-8?q?add:=20hub=E3=83=9A=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E3=81=AE=E3=83=9A=E3=83=BC=E3=82=B8=E3=83=8D=E3=83=BC=E3=82=B7?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Header.tsx | 2 +- src/components/Pagination.tsx | 100 +++++++++++++++++++ src/pages/hub/[page].tsx | 149 +++++++++++++++++++++++++++++ src/pages/hub/{ => post}/[uid].tsx | 0 4 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 src/components/Pagination.tsx create mode 100644 src/pages/hub/[page].tsx rename src/pages/hub/{ => post}/[uid].tsx (100%) diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 27e4f821..58d1ba81 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -55,7 +55,7 @@ const Header = () => { }, { name: 'Hub', - href: '/hub', + href: '/hub/1', icon: Squares2X2Icon, }, ] as const diff --git a/src/components/Pagination.tsx b/src/components/Pagination.tsx new file mode 100644 index 00000000..dd2ebf7d --- /dev/null +++ b/src/components/Pagination.tsx @@ -0,0 +1,100 @@ +import Link from 'next/link' +import { useRouter } from 'next/router' +import React from 'react' + +type Props = { + totalPages: number + currentPage: number + documentType: string +} + +const Pagination = ({ totalPages, currentPage, documentType }: Props) => { + const router = useRouter() + const isFirstPage = currentPage === 1 + const isLastPage = currentPage === totalPages + + const previousPage = () => { + if (!isFirstPage) { + router.push(`/${documentType}/${currentPage - 1}`) + } + } + + const nextPage = () => { + if (!isLastPage) { + router.push(`/${documentType}/${currentPage + 1}`) + } + } + + return ( + + ) +} + +export { Pagination } diff --git a/src/pages/hub/[page].tsx b/src/pages/hub/[page].tsx new file mode 100644 index 00000000..ff6183fc --- /dev/null +++ b/src/pages/hub/[page].tsx @@ -0,0 +1,149 @@ +import * as prismic from '@prismicio/client' +import { BaseLayout } from 'components/BaseLayout' +import { CustomHead } from 'components/CustomHead' +import { Tags } from 'components/hub/Tags' +import { Pagination } from 'components/Pagination' +import { GetStaticPropsContext, InferGetStaticPropsType } from 'next' +import Link from 'next/link' +import { useRouter } from 'next/router' +import { useEffect, useState } from 'react' +import { getFormattedDate } from 'utils/date' + +import { HubDocument } from '@/.slicemachine/prismicio' +import { createClient } from '@/prismicio' + +type PageProps = InferGetStaticPropsType + +export default function Hub({ pages, totalPages, currentPage }: PageProps) { + const router = useRouter() + const [sortedPages, setSortedPages] = useState[]>([]) + + useEffect(() => { + if (!router.isReady) { + return + } + const tag = router.query.tag + const filteredPages = pages + .filter((page) => { + if (!tag) { + return true + } + return page.tags.includes(tag as string) + }) + .sort((a, b) => { + if (!a.data.publication_date || !b.data.publication_date) { + return 0 + } + if (a.data.publication_date > b.data.publication_date) { + return -1 + } else { + return 1 + } + }) + setSortedPages(filteredPages) + }, [router]) + + return ( + <> + + +
+

+ ANTI-PATTERN HUB +

+

+ アンチパターン情報ポータルサイト +

+
+
+
+ {sortedPages.map((page, key) => { + return ( +
+ + {page.data.hero_image.alt + +

+ + {getFormattedDate(page.data.publication_date)} + + + + +

+ +

+ {page.data.title} +

+ +
+ ) + })} +
+ +
+
+ + ) +} + +export async function getStaticProps({ + previewData, + params, +}: GetStaticPropsContext) { + const client = createClient({ previewData }) + + const page = params?.page ? parseInt(params?.page as string) : 1 + const pageSize = 4 + + const pages = await client.get({ + predicates: [prismic.predicate.at('document.type', 'hub')], + orderings: ['my.hub.publication_date desc'], + page: page, + pageSize: pageSize, + }) + + return { + props: { + pages: pages.results, + totalPages: pages.total_pages, + currentPage: page, + }, + } +} + +export async function getStaticPaths() { + const client = createClient() + + const response = await client.get({ + predicates: [prismic.predicate.at('document.type', 'hub')], + orderings: ['my.hub.publication_date desc'], + pageSize: 1, + }) + + const totalPages = response.total_pages + const paths = [] + + for (let i = 1; i <= totalPages; i++) { + paths.push({ params: { page: i.toString() } }) + } + + return { + paths, + fallback: false, + } +} diff --git a/src/pages/hub/[uid].tsx b/src/pages/hub/post/[uid].tsx similarity index 100% rename from src/pages/hub/[uid].tsx rename to src/pages/hub/post/[uid].tsx From 3d78c861b89d703697265491514518fc1ee07a28 Mon Sep 17 00:00:00 2001 From: ko-shiro-hap Date: Wed, 10 May 2023 10:51:40 +0900 Subject: [PATCH 2/7] =?UTF-8?q?add:=20news=E3=81=AE=E3=83=9A=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E3=83=8D=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Header.tsx | 2 +- src/pages/hub/[page].tsx | 2 +- src/pages/news/[page].tsx | 114 ++++++++++++++++++++++++++++ src/pages/news/{ => post}/[uid].tsx | 0 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/pages/news/[page].tsx rename src/pages/news/{ => post}/[uid].tsx (100%) diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 58d1ba81..d05d99d5 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -50,7 +50,7 @@ const Header = () => { }, { name: locale === 'ja' ? 'ニュース' : 'News', - href: '/news', + href: '/news/1', icon: ShieldCheckIcon, }, { diff --git a/src/pages/hub/[page].tsx b/src/pages/hub/[page].tsx index ff6183fc..821649b5 100644 --- a/src/pages/hub/[page].tsx +++ b/src/pages/hub/[page].tsx @@ -108,7 +108,7 @@ export async function getStaticProps({ const client = createClient({ previewData }) const page = params?.page ? parseInt(params?.page as string) : 1 - const pageSize = 4 + const pageSize = 20 const pages = await client.get({ predicates: [prismic.predicate.at('document.type', 'hub')], diff --git a/src/pages/news/[page].tsx b/src/pages/news/[page].tsx new file mode 100644 index 00000000..4fab73cc --- /dev/null +++ b/src/pages/news/[page].tsx @@ -0,0 +1,114 @@ +import * as prismic from '@prismicio/client' +import { BaseLayout } from 'components/BaseLayout' +import { CustomHead } from 'components/CustomHead' +import { Pagination } from 'components/Pagination' +import type { GetStaticPropsContext, InferGetStaticPropsType } from 'next' +import Link from 'next/link' +import { getFormattedDate } from 'utils/date' + +import { createClient } from '@/prismicio' + +type PageProps = InferGetStaticPropsType + +export default function News({ pages, totalPages, currentPage }: PageProps) { + const sorted_pages = pages.sort((a, b) => { + if (!a.data.publication_date || !b.data.publication_date) { + return 0 + } + if (a.data.publication_date > b.data.publication_date) { + return -1 + } else { + return 1 + } + }) + return ( + <> + + +
+

+ NEWS +

+

お知らせ

+
+
+
+
+ {sorted_pages.map((page, key) => { + return ( + + + {getFormattedDate(page.data.publication_date)} + + + {page.data.title} + + + ) + })} + +
+
+
+
+ + ) +} + +export async function getStaticProps({ + previewData, + params, +}: GetStaticPropsContext) { + const client = createClient({ previewData }) + + const page = params?.page ? parseInt(params?.page as string) : 1 + const pageSize = 20 + + const pages = await client.get({ + predicates: [prismic.predicate.at('document.type', 'news')], + orderings: ['my.news.publication_date desc'], + page: page, + pageSize: pageSize, + }) + + return { + props: { + pages: pages.results, + totalPages: pages.total_pages, + currentPage: page, + }, + } +} + +export async function getStaticPaths() { + const client = createClient() + + const response = await client.get({ + predicates: [prismic.predicate.at('document.type', 'news')], + orderings: ['my.news.publication_date desc'], + pageSize: 1, + }) + + const totalPages = response.total_pages + const paths = [] + + for (let i = 1; i <= totalPages; i++) { + paths.push({ params: { page: i.toString() } }) + } + + return { + paths, + fallback: false, + } +} diff --git a/src/pages/news/[uid].tsx b/src/pages/news/post/[uid].tsx similarity index 100% rename from src/pages/news/[uid].tsx rename to src/pages/news/post/[uid].tsx From 02cd12f9e7b19a452887bf3dd079720a78345751 Mon Sep 17 00:00:00 2001 From: ko-shiro-hap Date: Wed, 10 May 2023 10:59:11 +0900 Subject: [PATCH 3/7] =?UTF-8?q?fix:=20=E4=B8=8D=E8=A6=81=E3=81=AAorderings?= =?UTF-8?q?=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/hub/[page].tsx | 1 - src/pages/news/[page].tsx | 1 - 2 files changed, 2 deletions(-) diff --git a/src/pages/hub/[page].tsx b/src/pages/hub/[page].tsx index 821649b5..b282de68 100644 --- a/src/pages/hub/[page].tsx +++ b/src/pages/hub/[page].tsx @@ -112,7 +112,6 @@ export async function getStaticProps({ const pages = await client.get({ predicates: [prismic.predicate.at('document.type', 'hub')], - orderings: ['my.hub.publication_date desc'], page: page, pageSize: pageSize, }) diff --git a/src/pages/news/[page].tsx b/src/pages/news/[page].tsx index 4fab73cc..7135dbe0 100644 --- a/src/pages/news/[page].tsx +++ b/src/pages/news/[page].tsx @@ -77,7 +77,6 @@ export async function getStaticProps({ const pages = await client.get({ predicates: [prismic.predicate.at('document.type', 'news')], - orderings: ['my.news.publication_date desc'], page: page, pageSize: pageSize, }) From ddfc3790dea2a8cf9ba3112cda9883ba920c99af Mon Sep 17 00:00:00 2001 From: ko-shiro-hap Date: Wed, 10 May 2023 14:53:19 +0900 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20props=E3=81=AB=E5=9E=8B=E3=82=92?= =?UTF-8?q?=E5=AE=9A=E7=BE=A9=E3=81=97=E3=81=A6=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E3=82=92=E8=A7=A3=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/hub/[page].tsx | 2 +- src/pages/news/[page].tsx | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/hub/[page].tsx b/src/pages/hub/[page].tsx index b282de68..9215da30 100644 --- a/src/pages/hub/[page].tsx +++ b/src/pages/hub/[page].tsx @@ -118,7 +118,7 @@ export async function getStaticProps({ return { props: { - pages: pages.results, + pages: pages.results as HubDocument[], totalPages: pages.total_pages, currentPage: page, }, diff --git a/src/pages/news/[page].tsx b/src/pages/news/[page].tsx index 7135dbe0..9089e315 100644 --- a/src/pages/news/[page].tsx +++ b/src/pages/news/[page].tsx @@ -6,6 +6,7 @@ import type { GetStaticPropsContext, InferGetStaticPropsType } from 'next' import Link from 'next/link' import { getFormattedDate } from 'utils/date' +import { NewsDocument } from '@/.slicemachine/prismicio' import { createClient } from '@/prismicio' type PageProps = InferGetStaticPropsType @@ -83,7 +84,7 @@ export async function getStaticProps({ return { props: { - pages: pages.results, + pages: pages.results as NewsDocument[], totalPages: pages.total_pages, currentPage: page, }, From 5d0d15fa222565120aa5d0f57ee53d5f0e12c084 Mon Sep 17 00:00:00 2001 From: ko-shiro-hap Date: Wed, 10 May 2023 15:15:58 +0900 Subject: [PATCH 5/7] =?UTF-8?q?add:=20URL=E3=81=A7=E3=83=9A=E3=83=BC?= =?UTF-8?q?=E3=82=B8=E6=95=B0=E3=82=92=E6=8C=87=E5=AE=9A=E3=81=9B=E3=81=9A?= =?UTF-8?q?=E3=81=AB=E9=A3=9B=E3=82=93=E3=81=A7=E3=81=8D=E3=81=9F=E5=A0=B4?= =?UTF-8?q?=E5=90=88=E3=81=AB=E3=80=811=E3=83=9A=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E7=9B=AE=E3=81=AB=E3=83=AA=E3=83=80=E3=82=A4=E3=83=AC=E3=82=AF?= =?UTF-8?q?=E3=83=88=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/hub/index.tsx | 108 +++------------------------------------ src/pages/news/index.tsx | 75 +++------------------------ 2 files changed, 14 insertions(+), 169 deletions(-) diff --git a/src/pages/hub/index.tsx b/src/pages/hub/index.tsx index 4de973d3..24cd4922 100644 --- a/src/pages/hub/index.tsx +++ b/src/pages/hub/index.tsx @@ -1,105 +1,11 @@ -import { BaseLayout } from 'components/BaseLayout' -import { CustomHead } from 'components/CustomHead' -import { Tags } from 'components/hub/Tags' -import { GetStaticPropsContext, InferGetStaticPropsType } from 'next' -import Link from 'next/link' -import { useRouter } from 'next/router' -import { useEffect, useState } from 'react' -import { getFormattedDate } from 'utils/date' +import { GetServerSideProps } from 'next' -import { HubDocument } from '@/.slicemachine/prismicio' -import { createClient } from '@/prismicio' - -type PageProps = InferGetStaticPropsType - -export default function Hub({ pages }: PageProps) { - const router = useRouter() - const [sortedPages, setSortedPages] = useState[]>([]) - - useEffect(() => { - if (!router.isReady) { - return - } - const tag = router.query.tag - const filteredPages = pages - .filter((page) => { - if (!tag) { - return true - } - return page.tags.includes(tag as string) - }) - .sort((a, b) => { - if (!a.data.publication_date || !b.data.publication_date) { - return 0 - } - if (a.data.publication_date > b.data.publication_date) { - return -1 - } else { - return 1 - } - }) - setSortedPages(filteredPages) - }, [router]) - - return ( - <> - - -
-

- ANTI-PATTERN HUB -

-

- アンチパターン情報ポータルサイト -

-
-
- {sortedPages.map((page, key) => { - return ( -
- - {page.data.hero_image.alt - -

- - {getFormattedDate(page.data.publication_date)} - - - - -

- -

- {page.data.title} -

- -
- ) - })} -
-
- - ) +export const getServerSideProps: GetServerSideProps = async (context) => { + context.res.setHeader('Location', '/hub/1') + context.res.statusCode = 302 + return { props: {} } } -export async function getStaticProps({ previewData }: GetStaticPropsContext) { - const client = createClient({ previewData }) - - const pages = await client.getAllByType('hub') +const RedirectToFirstPage = () => null - return { - props: { - pages, - }, - } -} +export default RedirectToFirstPage diff --git a/src/pages/news/index.tsx b/src/pages/news/index.tsx index 6b683354..cba6ecdc 100644 --- a/src/pages/news/index.tsx +++ b/src/pages/news/index.tsx @@ -1,72 +1,11 @@ -import { BaseLayout } from 'components/BaseLayout' -import { CustomHead } from 'components/CustomHead' -import type { GetStaticPropsContext, InferGetStaticPropsType } from 'next' -import Link from 'next/link' -import { getFormattedDate } from 'utils/date' +import { GetServerSideProps } from 'next' -import { createClient } from '@/prismicio' - -type PageProps = InferGetStaticPropsType - -export default function News({ pages }: PageProps) { - const sorted_pages = pages.sort((a, b) => { - if (!a.data.publication_date || !b.data.publication_date) { - return 0 - } - if (a.data.publication_date > b.data.publication_date) { - return -1 - } else { - return 1 - } - }) - return ( - <> - - -
-

- NEWS -

-

お知らせ

-
-
-
-
- {sorted_pages.map((page, key) => { - return ( - - - {getFormattedDate(page.data.publication_date)} - - - {page.data.title} - - - ) - })} -
-
-
-
- - ) +export const getServerSideProps: GetServerSideProps = async (context) => { + context.res.setHeader('Location', '/news/1') + context.res.statusCode = 302 + return { props: {} } } -export async function getStaticProps({ previewData }: GetStaticPropsContext) { - const client = createClient({ previewData }) +const RedirectToFirstPage = () => null - const pages = await client.getAllByType('news') - - return { - props: { - pages, - }, - } -} +export default RedirectToFirstPage From db355a4ab7bfb4afd0b0bd4d990c64705a8db501 Mon Sep 17 00:00:00 2001 From: ko-shiro-hap Date: Sat, 3 Jun 2023 22:05:07 +0900 Subject: [PATCH 6/7] =?UTF-8?q?fix:=20=E3=83=80=E3=83=BC=E3=82=AF=E3=83=A2?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=A7=E8=A6=8B=E3=81=9F=E7=9B=AE=E3=81=8C?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=81=95=E3=82=8C=E3=81=AA=E3=81=84=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Pagination.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Pagination.tsx b/src/components/Pagination.tsx index dd2ebf7d..432f0023 100644 --- a/src/components/Pagination.tsx +++ b/src/components/Pagination.tsx @@ -32,7 +32,7 @@ const Pagination = ({ totalPages, currentPage, documentType }: Props) => {