Skip to content

Commit 6b516d1

Browse files
committed
feat: Add login check before creating an answer
1 parent d4ff0fb commit 6b516d1

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

components/forms/Answer.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useTheme } from "@/context/ThemeProvider";
1111
import { Button } from "../ui/button";
1212
import { createAnswer } from "@/lib/actions/answer.action";
1313
import { usePathname } from "next/navigation";
14+
import { toast } from "../hooks/use-toast";
1415

1516
interface Props {
1617
question: string;
@@ -30,6 +31,12 @@ const Answer = ({ question, questionId, author }: Props) => {
3031
});
3132

3233
const handleCreateAnswer = async (values: z.infer<typeof answerSchema>) => {
34+
if (!author)
35+
return toast({
36+
title: "You need to be logged in to answer",
37+
description: "Please login to answer",
38+
variant: "destructive",
39+
});
3340
setIsSubmiting(true);
3441
try {
3542
await createAnswer({

components/ui/toaster.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ export function Toaster() {
1515

1616
return (
1717
<ToastProvider>
18-
{toasts.map(function ({ id, title, description, action, ...props }) {
18+
{toasts.map(function ({
19+
id,
20+
title,
21+
description,
22+
variant,
23+
action,
24+
...props
25+
}) {
1926
return (
2027
<Toast
2128
key={id}
2229
{...props}
23-
className="text-dark100_light900 border-light-700 bg-light-900 dark:border-dark-400 dark:bg-dark-300"
30+
className={`text-dark100_light900 border-light-700 bg-light-900 dark:border-dark-400 dark:bg-dark-300 ${variant === "destructive" ? "border-red-500 bg-red-500 text-white dark:bg-red-400" : ""}`}
2431
>
2532
<div className="grid gap-1">
2633
{title && <ToastTitle>{title}</ToastTitle>}

0 commit comments

Comments
 (0)