Skip to content

Commit e8b0e07

Browse files
authored
Merge pull request #278 from TyrinH/result-page-toast-for-guest
[#228] | Added toast if a guest tries to vote on a snippet
2 parents b551cfd + 80bf5ad commit e8b0e07

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
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: 21 additions & 5 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,6 +33,7 @@ export function Voting({ userId, snippetId, usersVote }: VotingProps) {
3333
disabled={isPending}
3434
onClick={() => {
3535
startTransition(async () => {
36+
if (userId) {
3637
try {
3738
if (usersVote?.type === "UP") {
3839
await deleteVoteAction({ snippetId });
@@ -48,6 +49,13 @@ export function Voting({ userId, snippetId, usersVote }: VotingProps) {
4849
} catch (err) {
4950
catchError(err);
5051
}
52+
} else {
53+
toast({
54+
title: "Warning",
55+
description: "You should sign in first to vote.",
56+
variant: "middle",
57+
});
58+
}
5159
});
5260
}}
5361
>
@@ -63,6 +71,7 @@ export function Voting({ userId, snippetId, usersVote }: VotingProps) {
6371
disabled={isPending}
6472
onClick={() => {
6573
startTransition(async () => {
74+
if (userId) {
6675
try {
6776
if (usersVote?.type === "DOWN") {
6877
await deleteVoteAction({
@@ -76,11 +85,18 @@ export function Voting({ userId, snippetId, usersVote }: VotingProps) {
7685
title: "Success.",
7786
description:
7887
"Thanks for your feedback! We will consider it.",
79-
variant: "default",
80-
});
88+
variant: "default",
89+
});
90+
}
91+
} catch (err) {
92+
catchError(err);
8193
}
82-
} catch (err) {
83-
catchError(err);
94+
} else {
95+
toast({
96+
title: "Warning",
97+
description: "You should sign in first to vote.",
98+
variant: "middle",
99+
});
84100
}
85101
});
86102
}}

0 commit comments

Comments
 (0)