Skip to content

Commit 0101c8c

Browse files
committed
Added toast to warn guest if they try to vote on a snippet to log in first
1 parent 4135058 commit 0101c8c

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

src/app/result/page.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,11 @@ export default async function ResultsChart({
8181
</Button>
8282
</div>
8383
<div className="my-4">
84-
{user && (
85-
<Voting
86-
snippetId={searchParams.snippetId}
87-
userId={user.id}
88-
usersVote={usersVote ?? undefined}
89-
/>
90-
)}
84+
<Voting
85+
snippetId={searchParams.snippetId}
86+
userId={user?.id ?? undefined}
87+
usersVote={usersVote ?? undefined}
88+
/>
9189
</div>
9290
<div className="flex items-center justify-center space-x-2">
9391
<Badge variant="outline">

src/app/result/voting.tsx

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { toast } from "@/components/ui/use-toast";
1414
import { catchError, cn } from "@/lib/utils";
1515

1616
interface VotingProps {
17-
userId: User["id"];
17+
userId: User["id"] | undefined;
1818
snippetId: Snippet["id"];
1919
usersVote?: SnippetVote;
2020
}
@@ -33,26 +33,34 @@ export function Voting({ userId, snippetId, usersVote }: VotingProps) {
3333
disabled={isPending}
3434
onClick={() => {
3535
startTransition(async () => {
36-
try {
37-
if (usersVote?.type === "UP") {
38-
await deleteVoteAction({
39-
userId,
40-
snippetId,
41-
});
42-
} else {
43-
await upvoteSnippetAction({
44-
userId,
45-
snippetId,
46-
});
47-
toast({
48-
title: "Success.",
49-
description:
50-
"Thanks for your feedback! We will consider it.",
51-
variant: "default",
52-
});
36+
if (userId) {
37+
try {
38+
if (usersVote?.type === "UP") {
39+
await deleteVoteAction({
40+
userId,
41+
snippetId,
42+
});
43+
} else {
44+
await upvoteSnippetAction({
45+
userId,
46+
snippetId,
47+
});
48+
toast({
49+
title: "Success.",
50+
description:
51+
"Thanks for your feedback! We will consider it.",
52+
variant: "default",
53+
});
54+
}
55+
} catch (err) {
56+
catchError(err);
5357
}
54-
} catch (err) {
55-
catchError(err);
58+
} else {
59+
toast({
60+
title: "Warning",
61+
description: "You should sign in first to vote.",
62+
variant: "middle",
63+
});
5664
}
5765
});
5866
}}
@@ -69,26 +77,34 @@ export function Voting({ userId, snippetId, usersVote }: VotingProps) {
6977
disabled={isPending}
7078
onClick={() => {
7179
startTransition(async () => {
72-
try {
73-
if (usersVote?.type === "DOWN") {
74-
await deleteVoteAction({
75-
userId,
76-
snippetId,
77-
});
78-
} else {
79-
await downvoteSnippetAction({
80-
userId,
81-
snippetId,
82-
});
83-
toast({
84-
title: "Success.",
85-
description:
80+
if (userId) {
81+
try {
82+
if (usersVote?.type === "DOWN") {
83+
await deleteVoteAction({
84+
userId,
85+
snippetId,
86+
});
87+
} else {
88+
await downvoteSnippetAction({
89+
userId,
90+
snippetId,
91+
});
92+
toast({
93+
title: "Success.",
94+
description:
8695
"Thanks for your feedback! We will consider it.",
87-
variant: "default",
88-
});
96+
variant: "default",
97+
});
98+
}
99+
} catch (err) {
100+
catchError(err);
89101
}
90-
} catch (err) {
91-
catchError(err);
102+
} else {
103+
toast({
104+
title: "Warning",
105+
description: "You should sign in first to vote.",
106+
variant: "middle",
107+
});
92108
}
93109
});
94110
}}

0 commit comments

Comments
 (0)