diff --git a/components/following-button.tsx b/components/following-button.tsx new file mode 100644 index 0000000000000000000000000000000000000000..8c080205be5b057b5ab5a4cd1eb51d6fbd5f2559 --- /dev/null +++ b/components/following-button.tsx @@ -0,0 +1,43 @@ +"use client" + +import { Prisma } from "@prisma/client"; +import { useRouter } from "next/navigation"; +import { startTransition } from "react"; +import { Icons } from "./icons"; +import { Button } from "./ui/button"; + +type likeType = Prisma.LikeUncheckedCreateInput + +export default function FollowButton(props: { data: likeType }) { + const router = useRouter(); + + async function postFollow(e: any) { + e.preventDefault() + const msgLikeData = props.data; + const likeData = {} as likeType + likeData.userId = msgLikeData.userId + likeData.postId = msgLikeData.postId + + const response = await fetch('http://localhost:3000/api/follows', { + method: 'PUT', + body: JSON.stringify(likeData) + }) + + startTransition(() => { + // Refresh the current route and fetch new data from the server without + // losing client-side browser or React state. + router.refresh(); + }); + return await response.json() + } + + return ( + <div> + <form onSubmit={postFollow}> + <Button type="submit" variant="ghost" size="lg" className="float-right" > + <Icons.heart className="h-3 w-3" /> + </Button> + </form> + </div> + ) +} \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a6b261ad022b8097f26e23533fea0e740a542bfd..2e93abf94adcd847b24059d16238a3d97d140734 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -22,6 +22,8 @@ model User { Post Post[] Comment Comment[] Like Like[] + followers User[] @relation("Followers", references: [id]) + following User[] @relation("Following", references: [id]) } model Post { @@ -57,3 +59,11 @@ model Comment { post Post @relation(fields: [postId], references: [id], onDelete: Cascade) user User @relation(fields: [userId], references: [id], onDelete: Cascade) } + +model Follower { + id Int @id @default(autoincrement()) + follower User @relation("Followers", fields: [followerId], references: [id]) + followerId Int + following User @relation("Following", fields: [followingId], references: [id]) + followingId Int +} \ No newline at end of file