({ post }: UsePollVoteProps)
| 19 | * Extracted from PollGrid/PollList to reduce duplication. |
| 20 | */ |
| 21 | export const usePollVote = ({ post }: UsePollVoteProps): UsePollVoteReturn => { |
| 22 | const { user } = useAuthContext(); |
| 23 | const { onVote, isCastingVote } = usePoll({ post }); |
| 24 | const [shouldAnimateResults, setShouldAnimateResults] = useState(false); |
| 25 | const onSendViewPost = useViewPost(); |
| 26 | |
| 27 | const handleVote = useCallback( |
| 28 | (optionId: string, text: string) => { |
| 29 | if (!isCastingVote) { |
| 30 | onVote(optionId, text); |
| 31 | setShouldAnimateResults(true); |
| 32 | |
| 33 | if (!post?.id || !user?.id) { |
| 34 | return; |
| 35 | } |
| 36 | onSendViewPost(post.id); |
| 37 | } |
| 38 | }, |
| 39 | [isCastingVote, onVote, post?.id, user?.id, onSendViewPost], |
| 40 | ); |
| 41 | |
| 42 | return { |
| 43 | handleVote, |
| 44 | shouldAnimateResults, |
| 45 | isCastingVote, |
| 46 | }; |
| 47 | }; |
no test coverage detected