Skip to content

Commit 4c94349

Browse files
authored
Merge pull request #146 from kc3hack/RAIT-09/develop
動的ルーティングページ改善
2 parents fe69cb0 + 3b2af87 commit 4c94349

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

frontend/src/app/(main)/post/[postId]/page.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22
import React, { useEffect, useState } from 'react';
33
import { useSession } from 'next-auth/react';
4-
import { useParams } from 'next/navigation';
4+
import { useRouter, useParams } from 'next/navigation';
55
import { PostTypes } from '@/types/postTypes';
66
import Post from '@/components/Post';
77
import fetchOnePost from './actions/fetchOnePost';
@@ -18,19 +18,22 @@ const PostPage = () => {
1818
const [post, setPost] = useState<PostTypes | null>(null);
1919
// セッションの取得
2020
const session = useSession();
21+
const router = useRouter();
2122

2223
// 投稿IDから投稿をFetchする
2324
useEffect(() => {
2425
const getPost = async () => {
26+
if (!postId) return;
2527
if (session.status === 'loading') return;
2628
const data = await fetchOnePost({
2729
postId: postId as string,
2830
iconUrl: session.data?.user?.image ?? '',
2931
});
32+
if (!data) router.push('/post-not-found');
3033
setPost(data);
3134
};
3235
getPost();
33-
}, [postId, session.data?.user?.image, session.status]);
36+
}, [postId, session.data?.user?.image, session.status, router]);
3437

3538
return (
3639
<div>
@@ -44,7 +47,7 @@ const PostPage = () => {
4447
transition={{ duration: 0.5 }}
4548
className='mx-auto max-w-sm pt-5 lg:max-w-lg'
4649
>
47-
{post && <Post post={post} />}
50+
{post && <Post post={post} onDelete={() => router.push('/')} />}
4851
</motion.div>
4952
)}
5053
</AnimatePresence>

frontend/src/app/(main)/profile/[userId]/page.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22
import Image from 'next/image';
33
import React, { useEffect, useState } from 'react';
4-
import { useParams } from 'next/navigation';
4+
import { useRouter, useParams } from 'next/navigation';
55
import Timeline from '@/components/Timeline';
66
import fetchProfile from './actions/fetchProfile';
77
import { ProfileTypes } from '@/types/profileTypes';
@@ -15,15 +15,18 @@ import { ProfileTypes } from '@/types/profileTypes';
1515
const Profile = () => {
1616
const { userId } = useParams() as { userId: string };
1717
const [profile, setProfile] = useState<ProfileTypes | null>(null);
18+
const router = useRouter();
1819

1920
// ユーザIDからプロフィールをFetchする
2021
useEffect(() => {
2122
const getProfile = async () => {
23+
if (!userId) return;
2224
const data = await fetchProfile({ userId: userId as string });
25+
if (!data) router.push('/user-not-found');
2326
setProfile(data);
2427
};
2528
getProfile();
26-
}, [userId]);
29+
}, [userId, router]);
2730

2831
// totalPost に応じた背景色のクラスを決定
2932
const getBackgroundClass = () => {

0 commit comments

Comments
 (0)