From 2d8645b1a2c00de74fb9f34aa33ecf29e52ac4ed Mon Sep 17 00:00:00 2001 From: tosaken1116 Date: Sat, 13 Apr 2024 23:06:03 +0900 Subject: [PATCH] fix: async component in client components --- src/app/page.tsx | 8 +++-- src/components/page/TopPage/index.tsx | 7 +++-- src/components/ui/Pagination/index.tsx | 2 ++ .../Work/components/WorkCards/index.tsx | 27 ++++++++++++++-- .../WorkCards/presentations/container.tsx | 31 ++++++------------- src/domains/Work/utils/converter/work.ts | 4 +-- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index 87bd68f..d6c9552 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,11 @@ -import type { FC } from 'react'; +import type { ReactNode } from 'react'; import { Screen } from '@/components/page/TopPage'; -const TopPage: FC = () => ; +const TopPage = ({ + searchParams, +}: { + searchParams: { page?: number }; +}): ReactNode => ; export default TopPage; diff --git a/src/components/page/TopPage/index.tsx b/src/components/page/TopPage/index.tsx index 86c52f8..8fa5be9 100644 --- a/src/components/page/TopPage/index.tsx +++ b/src/components/page/TopPage/index.tsx @@ -5,13 +5,16 @@ import { WithFooter } from '@/components/Layout/WithFooter'; import { SearchWorkPalette } from '@/domains/Work/components/SearchWorkPalette'; import { WorkCards } from '@/domains/Work/components/WorkCards'; -export const Screen: FC = () => ( +type Props = { + page: number; +}; +export const Screen: FC = ({ page }) => (
- +
); diff --git a/src/components/ui/Pagination/index.tsx b/src/components/ui/Pagination/index.tsx index 3d25711..f05508d 100644 --- a/src/components/ui/Pagination/index.tsx +++ b/src/components/ui/Pagination/index.tsx @@ -28,6 +28,8 @@ export const Pagination: FC = ({ currentPage, displayRange = 1, }: Props) => { + console.log('currentPage', currentPage); + console.log('totalPage', totalPage); // Format current page to be between 1 and totalPage ( 1 <= formatCurrentPage <= totalPage ) const formatCurrentPage = restrictRange(currentPage, 1, totalPage); diff --git a/src/domains/Work/components/WorkCards/index.tsx b/src/domains/Work/components/WorkCards/index.tsx index 4bd4d65..8c66fd5 100644 --- a/src/domains/Work/components/WorkCards/index.tsx +++ b/src/domains/Work/components/WorkCards/index.tsx @@ -1,16 +1,39 @@ import type { FC } from 'react'; import { Suspense } from 'react'; +import { worksRepository } from '../../repository'; + import { WorkCardsContainer } from './presentations'; import { WorkCardsErrorPresentation } from './presentations/error'; import { WorkCardsLoadingPresentation } from './presentations/loading'; import { ErrorBoundary } from '@/libs/ErrorBoundary'; -export const WorkCards: FC = () => ( +type Props = { + page: number; +}; +export const WorkCards: FC = ({ page }) => ( }> }> - + ); + +const WorkCardsFetchContainer: FC<{ page: number }> = async ({ page }) => { + const { works, totalCount } = await worksRepository.getWorks({ + query: { + page, + }, + }); + console.log(totalCount); + return ( + + ); +}; diff --git a/src/domains/Work/components/WorkCards/presentations/container.tsx b/src/domains/Work/components/WorkCards/presentations/container.tsx index 80a15f0..9d691e7 100644 --- a/src/domains/Work/components/WorkCards/presentations/container.tsx +++ b/src/domains/Work/components/WorkCards/presentations/container.tsx @@ -4,36 +4,25 @@ import type { FC } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; -import { worksRepository } from '../../../repository'; - import { WorkCardsPresentation } from './main'; -const WorkCardsFetchContainer: FC<{ +import type { Works } from '@/domains/Work/types'; + +type Props = { currentPage: number; - handleNextPage: (page: number) => void; -}> = async ({ currentPage, handleNextPage }) => { - const { works, totalCount } = await worksRepository.getWorks({ - query: { - page: currentPage, - }, - }); - return ( - - ); + totalPage: number; + works: Works; }; - -export const WorkCardsContainer: FC = () => { +export const WorkCardsContainer: FC = ({ works, totalPage }) => { const params = useSearchParams(); const currentPage = parseInt(params.get('page') || '1'); const router = useRouter(); + return ( - router.push(`/?page=${page}`)} /> ); diff --git a/src/domains/Work/utils/converter/work.ts b/src/domains/Work/utils/converter/work.ts index a2cdf3d..fe4066c 100644 --- a/src/domains/Work/utils/converter/work.ts +++ b/src/domains/Work/utils/converter/work.ts @@ -13,7 +13,7 @@ export const convertWork = (work: modelWork): Work => ({ title: work.title, creator: userConverter(work.user), isPublic: work.visibility === 'public', - tags: work.tags.map((tag) => tagConverter(tag)), + tags: work.tags?.map((tag) => tagConverter(tag)), thumbnailUrl: work.thumbnail.url, createdAt: work.created_at, }); @@ -38,7 +38,7 @@ export const convertCreateWork = (work: DefaultWork): PostWork => ({ title: work.title, description: work.description, visibility: work.isPublic ? 'public' : 'private', - tags_id: work.tags.map((tag) => tag.id), + tags_id: work.tags?.map((tag) => tag.id), thumbnail_asset_id: work.thumbnail.id, urls: urlsConverterToBaseURLInfo(work.urls), assets_id: work.assets.map((asset) => asset.id),