-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b85602
commit ea6a255
Showing
11 changed files
with
277 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ Next.jsのApp Routerを使用したブログアプリです。 | |
- ページネーション機能 | ||
- いいね機能 | ||
- タグ機能 | ||
- フォロー機能 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Metadata } from "next"; | ||
import type { User } from "@/app/types/User"; | ||
import { fetchFollowers } from "@/app/lib/data"; | ||
import { DefaultLayout } from "@/app/components/templates/DefaultLayout/DefaultLayout"; | ||
import { UserItem } from "@/app/components/organisms/UserItem"; | ||
|
||
export function generateMetadata( | ||
{ params }: { params: { name: string } } | ||
): Metadata { | ||
const name = decodeURIComponent(params.name); | ||
|
||
return { | ||
title: `${name}`, | ||
}; | ||
} | ||
|
||
export default async function Page({ | ||
params | ||
}: { | ||
params: { name: string } | ||
}) { | ||
const users: User[] = await fetchFollowers(params.name); | ||
|
||
const userItems = users.length > 0 ? ( | ||
users.map(user => ( | ||
<UserItem | ||
key={user.id} | ||
user={user} | ||
/> | ||
)) | ||
) : ( | ||
<p className="font-bold text-xs text-center p-4">フォロワーはいません</p> | ||
); | ||
|
||
return ( | ||
<DefaultLayout className="p-4"> | ||
<section className="bg-white"> | ||
<h1 className="font-bold px-4 py-4">フォロワー</h1> | ||
{userItems} | ||
</section> | ||
</DefaultLayout> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Metadata } from "next"; | ||
import type { User } from "@/app/types/User"; | ||
import { fetchFollowingUsers } from "@/app/lib/data"; | ||
import { DefaultLayout } from "@/app/components/templates/DefaultLayout/DefaultLayout"; | ||
import { UserItem } from "@/app/components/organisms/UserItem"; | ||
|
||
export function generateMetadata( | ||
{ params }: { params: { name: string } } | ||
): Metadata { | ||
const name = decodeURIComponent(params.name); | ||
|
||
return { | ||
title: `${name}`, | ||
}; | ||
} | ||
|
||
export default async function Page({ | ||
params | ||
}: { | ||
params: { name: string } | ||
}) { | ||
const users: User[] = await fetchFollowingUsers(params.name); | ||
|
||
const userItems = users.length > 0 ? ( | ||
users.map(user => ( | ||
<UserItem | ||
key={user.id} | ||
user={user} | ||
/> | ||
)) | ||
) : ( | ||
<p className="font-bold text-xs text-center p-4">フォローしているユーザーはいません</p> | ||
); | ||
|
||
return ( | ||
<DefaultLayout className="p-4"> | ||
<section className="bg-white"> | ||
<h1 className="font-bold px-4 py-4">フォローしているユーザー</h1> | ||
{userItems} | ||
</section> | ||
</DefaultLayout> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"use client"; | ||
|
||
import type { User } from "@/app/types/User"; | ||
import { useSession } from "next-auth/react"; | ||
import { createFollow, deleteFollow } from "@/app/lib/actions"; | ||
import { SubmitButton } from "@/app/components/atoms/SubmitButton"; | ||
|
||
export const FollowButton = ({ user }: { user: User }) => { | ||
const session = useSession(); | ||
|
||
const Following = user.followers.find((follower) => follower.id === session.data?.user?.id); | ||
|
||
const handleSubmit = () => { | ||
if (!session.data?.user) { | ||
alert("ログインすると「フォロー」をすることができます!"); | ||
return; | ||
} | ||
|
||
if (Following) { | ||
deleteFollow(session?.data?.user?.id as string, user.id); | ||
} else { | ||
createFollow(session?.data?.user?.id as string, user.id) | ||
} | ||
}; | ||
|
||
return ( | ||
<form action={handleSubmit}> | ||
<SubmitButton | ||
className={`${Following ? "bg-gray-100 text-black hover:bg-gray-200" : "bg-black text-white hover:opacity-70"} border font-bold mt-6 px-4 py-1 rounded-lg w-full`} | ||
> | ||
{Following ? "フォロー中" : "フォロー"} | ||
</SubmitButton> | ||
</form> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./FollowButton"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { User } from "@/app/types/User"; | ||
import Link from "next/link"; | ||
import { SessionProvider } from "next-auth/react"; | ||
import { auth } from "@/auth"; | ||
import { UserIcon } from "@/app/components/atoms/UserIcon"; | ||
import { FollowButton } from "@/app/components/atoms/FollowButton"; | ||
|
||
export const UserItem = async ({ user }: { user: User }) => { | ||
const session = await auth(); | ||
|
||
return ( | ||
<div className="border-t p-4 bg-white"> | ||
<section className="flex gap-4"> | ||
<UserIcon | ||
user={user} | ||
width={48} | ||
height={48} | ||
/> | ||
|
||
<div> | ||
<Link href={`/${user.name}`} className="w-full hover:underline"> | ||
<span className="font-bold text-sm">{user.name}</span> | ||
</Link> | ||
<ul className="flex gap-2"> | ||
{user.tags.map(tag => ( | ||
<li key={tag.id}> | ||
<Link | ||
href={`/tags/${tag.name}`} | ||
className="bg-gray-100 px-1.5 text-sm text-black/60 rounded hover:bg-gray-200" | ||
> | ||
{tag.name} | ||
</Link> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
</section> | ||
{session?.user?.id !== user.id && | ||
<SessionProvider> | ||
<FollowButton user={user} /> | ||
</SessionProvider> | ||
} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./UserItem"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,6 @@ export interface User { | |
posts: Post[]; | ||
likes: Like[]; | ||
tags: Tag[]; | ||
followings: User[]; | ||
followers: User[]; | ||
} |