({
onMutate,
mutationKey,
}: UseBookmarkPostProps = {})
| 86 | }; |
| 87 | |
| 88 | const useBookmarkPost = ({ |
| 89 | onMutate, |
| 90 | mutationKey, |
| 91 | }: UseBookmarkPostProps = {}): UseBookmarkPost => { |
| 92 | const { requestMethod } = useRequestProtocol(); |
| 93 | const client = useQueryClient(); |
| 94 | const { displayToast } = useToastNotification(); |
| 95 | const { user, showLogin } = useContext(AuthContext); |
| 96 | const { logEvent } = useLogContext(); |
| 97 | const { completeAction } = useActions(); |
| 98 | const { openModal } = useLazyModal(); |
| 99 | const postLogEvent = usePostLogEvent(); |
| 100 | const { logOpts } = useActiveFeedContext(); |
| 101 | |
| 102 | const defaultOnMutate: NonNullable<UseBookmarkPostProps['onMutate']> = ({ |
| 103 | id, |
| 104 | }) => { |
| 105 | if (!id) { |
| 106 | return undefined; |
| 107 | } |
| 108 | |
| 109 | const previousPost = client.getQueryData<PostData>( |
| 110 | getPostByIdKey(id), |
| 111 | )?.post; |
| 112 | |
| 113 | updatePostCache(client, id, (post) => ({ |
| 114 | bookmarked: !post.bookmarked, |
| 115 | analytics: post.analytics |
| 116 | ? { |
| 117 | ...post.analytics, |
| 118 | bookmarks: getOptimisticBookmarkCount( |
| 119 | post.analytics.bookmarks, |
| 120 | post.bookmarked, |
| 121 | ), |
| 122 | } |
| 123 | : post.analytics, |
| 124 | })); |
| 125 | |
| 126 | return () => { |
| 127 | if (!previousPost) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | updatePostCache(client, id, () => ({ |
| 132 | bookmarked: previousPost.bookmarked, |
| 133 | bookmark: previousPost.bookmark, |
| 134 | analytics: previousPost.analytics, |
| 135 | })); |
| 136 | }; |
| 137 | }; |
| 138 | |
| 139 | const { mutateAsync: bookmarkPost } = useMutation({ |
| 140 | mutationKey: mutationKey |
| 141 | ? [...bookmarkMutationKey, ...mutationKey] |
| 142 | : bookmarkMutationKey, |
| 143 | mutationFn: ({ mutation, payload }: UseBookmarkPostMutationProps) => |
| 144 | requestMethod(mutation, { |
| 145 | ...payload, |
no test coverage detected