@@ -14,7 +14,7 @@ import { toast } from "@/components/ui/use-toast";
14
14
import { catchError , cn } from "@/lib/utils" ;
15
15
16
16
interface VotingProps {
17
- userId : User [ "id" ] ;
17
+ userId : User [ "id" ] | undefined ;
18
18
snippetId : Snippet [ "id" ] ;
19
19
usersVote ?: SnippetVote ;
20
20
}
@@ -33,26 +33,34 @@ export function Voting({ userId, snippetId, usersVote }: VotingProps) {
33
33
disabled = { isPending }
34
34
onClick = { ( ) => {
35
35
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 ) ;
53
57
}
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
+ } ) ;
56
64
}
57
65
} ) ;
58
66
} }
@@ -69,26 +77,34 @@ export function Voting({ userId, snippetId, usersVote }: VotingProps) {
69
77
disabled = { isPending }
70
78
onClick = { ( ) => {
71
79
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 :
86
95
"Thanks for your feedback! We will consider it." ,
87
- variant : "default" ,
88
- } ) ;
96
+ variant : "default" ,
97
+ } ) ;
98
+ }
99
+ } catch ( err ) {
100
+ catchError ( err ) ;
89
101
}
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
+ } ) ;
92
108
}
93
109
} ) ;
94
110
} }
0 commit comments