-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement MVP of repository comments feature
- Loading branch information
1 parent
269b55a
commit 8b2cc6c
Showing
16 changed files
with
207 additions
and
25 deletions.
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
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,24 @@ | ||
"use server"; | ||
import { revalidatePath } from "next/cache"; | ||
import { userAction } from "@/lib/next-safe-action"; | ||
import repositoryService from "@/services/repository.service"; | ||
import * as z from "zod"; | ||
|
||
const schema = z.object({ | ||
repositoryId: z.number(), | ||
content: z.string().min(1, "Comment must be at least 1 character long"), | ||
}); | ||
|
||
export const addComment = userAction(schema, async (data, ctx) => { | ||
try { | ||
await repositoryService.addCommentToRepository( | ||
data.repositoryId, | ||
data.content, | ||
ctx.session.user.id, | ||
); | ||
} catch (error) { | ||
if (error instanceof Error) return { error: error.message }; | ||
} | ||
|
||
revalidatePath(`/repositories/${data.repositoryId}`); | ||
}); |
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,25 @@ | ||
import { AddCommentForm } from "@/components/organisms/_forms/addcomment.form"; | ||
import repositoryService from "@/services/repository.service"; | ||
import React from "react"; | ||
|
||
export default async function RepositoryCommentPage({ | ||
params, | ||
}: { | ||
params: { repositoryId: number }; | ||
}) { | ||
const comments = await repositoryService.getCommentsByRepositoryId( | ||
Number(params.repositoryId), | ||
); | ||
|
||
return ( | ||
<> | ||
<h1>Comments</h1> | ||
{comments.map((comment) => ( | ||
<div key={comment.id}> | ||
<p>{comment.content}</p> | ||
</div> | ||
))} | ||
<AddCommentForm repositoryId={Number(params.repositoryId)} /> | ||
</> | ||
); | ||
} |
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
File renamed without changes.
19 changes: 19 additions & 0 deletions
19
src/components/organisms/RepositoryCard/RepositoryCardComment.tsx
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,19 @@ | ||
import Link from "next/link"; | ||
import { CommentDiscussionIcon } from "@primer/octicons-react"; | ||
import type { Repository } from "@/types/prisma.type"; | ||
|
||
type Props = { | ||
repository: Repository; | ||
}; | ||
|
||
export const RepositoryCardComment = ({ repository }: Props) => { | ||
return ( | ||
<Link | ||
href={`repositories/${repository.id}`} | ||
className="flex items-center space-x-1 hover:text-[#2F81F7]" | ||
> | ||
<CommentDiscussionIcon className="h-4 w-4" /> | ||
<span>0</span> | ||
</Link> | ||
); | ||
}; |
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
2 changes: 1 addition & 1 deletion
2
src/components/organisms/Sidebar/SidebarRemoveStarredRepositories.tsx
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"use client"; | ||
|
||
import { useTransition } from "react"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { useForm } from "react-hook-form"; | ||
import { Form } from "@/components/atoms/form"; | ||
import { SubmitButton } from "@/components/molecules/SubmitButton"; | ||
import { TextAreaForm } from "@/components/molecules/TextAreaForm"; | ||
import { addComment } from "@/actions/addcomment"; | ||
import { formAddCommentSchema } from "./addcomment.schema"; | ||
import type { z } from "zod"; | ||
|
||
type Props = { | ||
repositoryId: number; | ||
}; | ||
|
||
export const AddCommentForm = ({ repositoryId }: Props) => { | ||
const [isPending, startTransition] = useTransition(); | ||
|
||
const form = useForm<z.infer<typeof formAddCommentSchema>>({ | ||
resolver: zodResolver(formAddCommentSchema), | ||
defaultValues: { | ||
content: "", | ||
}, | ||
}); | ||
|
||
function onSubmit(data: z.infer<typeof formAddCommentSchema>) { | ||
startTransition(async () => { | ||
const payload = { | ||
repositoryId, | ||
content: data.content, | ||
}; | ||
|
||
await addComment(payload); | ||
}); | ||
} | ||
|
||
return ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> | ||
<TextAreaForm | ||
control={form.control} | ||
name="content" | ||
placeholder="Leave a comment..." | ||
/> | ||
<SubmitButton isPending={isPending}>Comment</SubmitButton> | ||
</form> | ||
</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,5 @@ | ||
import * as z from "zod"; | ||
|
||
export const formAddCommentSchema = z.object({ | ||
content: z.string().min(1, "Comment must be at least 1 character long"), | ||
}); |
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