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