({
onMutate,
variables,
}: UseVotePostProps = {})
| 38 | }; |
| 39 | |
| 40 | const useVotePost = ({ |
| 41 | onMutate, |
| 42 | variables, |
| 43 | }: UseVotePostProps = {}): UseVotePost => { |
| 44 | const client = useQueryClient(); |
| 45 | const { user, showLogin } = useContext(AuthContext); |
| 46 | const { logEvent } = useLogContext(); |
| 47 | const postLogEvent = usePostLogEvent(); |
| 48 | const { logOpts } = useActiveFeedContext(); |
| 49 | const defaultOnMutate: NonNullable<UseVoteProps['onMutate']> = ({ |
| 50 | id, |
| 51 | vote, |
| 52 | }: UseVoteMutationProps) => { |
| 53 | const mutationHandler = voteMutationHandlers[vote]; |
| 54 | |
| 55 | if (!mutationHandler) { |
| 56 | return undefined; |
| 57 | } |
| 58 | |
| 59 | const previousVote = client.getQueryData<PostData>(getPostByIdKey(id))?.post |
| 60 | ?.userState?.vote; |
| 61 | updateSinglePostCache(client, id, mutationHandler); |
| 62 | |
| 63 | return () => { |
| 64 | if (typeof previousVote === 'undefined') { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | const rollbackMutationHandler = voteMutationHandlers[previousVote]; |
| 69 | |
| 70 | if (!rollbackMutationHandler) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | updateSinglePostCache(client, id, rollbackMutationHandler); |
| 75 | }; |
| 76 | }; |
| 77 | |
| 78 | const { |
| 79 | upvote: upvotePost, |
| 80 | downvote: downvotePost, |
| 81 | cancelVote: cancelPostVote, |
| 82 | } = useVote({ |
| 83 | onMutate: onMutate || defaultOnMutate, |
| 84 | entity: UserVoteEntity.Post, |
| 85 | variables, |
| 86 | }); |
| 87 | |
| 88 | const toggleUpvote: UseVotePost['toggleUpvote'] = useCallback( |
| 89 | async ({ payload: post, origin, opts }) => { |
| 90 | if (!post) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if (!user) { |
| 95 | showLogin({ trigger: AuthTriggers.Upvote }); |
| 96 | |
| 97 | return; |
no test coverage detected