Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🫚 Add comment value #169

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use server";

import { revalidatePath } from "next/cache";
import {
deleteCommentValue,
insertCommentValue,
} from "@/drizzle/queries/commentValues";
import { commentValues } from "@/drizzle/schema";
import { type Session } from "next-auth";

import { ApplicationParamsSchema } from "@/lib/paramsValidation";

interface CommentValueActionPayload extends ApplicationParamsSchema {
commentId: string;
session: Session | null;
isChecked: boolean;
value: (typeof commentValues.$inferInsert)["value"];
}

export async function commentValueAction({
applicationId,
session,
commentId,
waveId,
isChecked,
value,
}: CommentValueActionPayload) {
if (!session?.user?.id) {
throw new Error("Unauthorized");
}

if (isChecked) {
await deleteCommentValue({
commentId,
userId: session.user.id,
value,
});
} else {
await insertCommentValue({
commentId,
userId: session.user.id,
value,
});
}

revalidatePath(`/waves/${waveId}/applications/${applicationId}`);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { getCommentValue } from "@/drizzle/queries/commentValues";

import { auth } from "@/lib/auth";
import { ApplicationParamsSchema } from "@/lib/paramsValidation";
import { Button } from "@/components/ui/button";

import { commentValueAction } from "./commentValueAction";

interface CommentValueFormProps extends ApplicationParamsSchema {
commentId: string;
}

export async function CommentValueForm({
applicationId,
waveId,
commentId,
}: CommentValueFormProps) {
const session = await auth();
const applicationValue = await getCommentValue({
commentId,
userId: session?.user?.id,
});

const isUpvoted = applicationValue === "positive";
const isSpam = applicationValue === "spam";

return (
<form className="ml-auto flex gap-4">
<Button
variant="outline"
formAction={async () => {
"use server";
await commentValueAction({
commentId,
session,
applicationId,
waveId,
isChecked: isSpam,
value: "spam",
});
}}
>
{isSpam ? "Marked as SPAM" : "Report SPAM"}
</Button>
<Button
variant="outline"
disabled={!session?.user?.id}
formAction={async () => {
"use server";
await commentValueAction({
commentId,
session,
applicationId,
waveId,
isChecked: isUpvoted,
value: "positive",
});
}}
>
{isUpvoted ? "Upvoted" : "Upvote"}
</Button>
</form>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { UserAvatar } from "@/components/ui/userAvatar";

import { AddCommentForm } from "./addCommentForm/addCommentForm";
import { CommentValueForm } from "./commentValue/commentValueForm";
import { parseMarkdown } from "./parseMarkdown";

const SECTIONS = {
Expand All @@ -18,9 +19,10 @@ const SECTIONS = {

interface CommentsProps {
comments: ApplicationWithComments["comments"];
waveId: number;
}

export async function Comments({ comments }: CommentsProps) {
export async function Comments({ comments, waveId }: CommentsProps) {
const reviews = comments.filter((comment) => comment.reviews?.isReview);

return (
Expand All @@ -37,10 +39,10 @@ export async function Comments({ comments }: CommentsProps) {
/>
</TabsList>
<TabsContent value={SECTIONS.discussion}>
<CommentsList comments={comments} />
<CommentsList comments={comments} waveId={waveId} />
</TabsContent>
<TabsContent value={SECTIONS.reviews}>
<CommentsList comments={reviews} />
<CommentsList comments={reviews} waveId={waveId} />
</TabsContent>
</Tabs>

Expand All @@ -65,20 +67,21 @@ function SectionButton({ section, elementsAmount }: SectionButtonProps) {
);
}

function CommentsList({ comments }: CommentsProps) {
function CommentsList({ comments, waveId }: CommentsProps) {
return comments.map((comment) => (
<Fragment key={comment.id}>
<Comment comment={comment} />
<Comment comment={comment} waveId={waveId} />
<Separator className="my-6 last:hidden" />
</Fragment>
));
}

interface CommentProps {
comment: ApplicationWithComments["comments"][number];
waveId: number;
}

export async function Comment({ comment }: CommentProps) {
export async function Comment({ comment, waveId }: CommentProps) {
const commentHtml = await parseMarkdown(comment.content);

const isReview = comment.reviews?.isReview;
Expand Down Expand Up @@ -106,6 +109,11 @@ export async function Comment({ comment }: CommentProps) {
<span className="text-foreground/60">
{formatTime(comment.createdAt)}
</span>
<CommentValueForm
applicationId={comment.applicationId}
waveId={waveId}
commentId={comment.id}
/>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default async function Application({ params }: { params: unknown }) {

<Separator className="my-16" />

<Comments comments={application.comments} />
<Comments comments={application.comments} waveId={waveId} />
</div>
);
}
18 changes: 18 additions & 0 deletions src/drizzle/migrations/0004_talented_lady_ursula.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS "commentValue" (
"commentId" uuid NOT NULL,
"userId" text NOT NULL,
"value" "contentValue" NOT NULL,
CONSTRAINT "commentValue_commentId_userId_pk" PRIMARY KEY("commentId","userId")
);

DO $$ BEGIN
ALTER TABLE "commentValue" ADD CONSTRAINT "commentValue_commentId_comment_id_fk" FOREIGN KEY ("commentId") REFERENCES "comment"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
ALTER TABLE "commentValue" ADD CONSTRAINT "commentValue_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Loading
Loading