File size: 3,906 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { callApi } from '../helpers';
import { useCacheKey, useIsLoggedIn } from '../hooks';
import { postSubscriptionsQueryKeyPrefix } from '../queries/use-post-subscriptions-query';
import { subscriptionsCountQueryKeyPrefix } from '../queries/use-subscriptions-count-query';
import { PendingPostSubscriptionsResult, SubscriptionManagerSubscriptionsCount } from '../types';
type PendingPostConfirmParams = {
id: string;
};
type PendingPostConfirmResponse = {
confirmed: boolean;
};
const usePendingPostConfirmMutation = () => {
const { isLoggedIn } = useIsLoggedIn();
const queryClient = useQueryClient();
const subscriptionsCacheKey = useCacheKey( postSubscriptionsQueryKeyPrefix );
const countCacheKey = useCacheKey( subscriptionsCountQueryKeyPrefix );
return useMutation( {
mutationFn: async ( { id }: PendingPostConfirmParams ) => {
if ( ! id ) {
throw new Error(
// reminder: translate this string when we add it to the UI
'Invalid subscription key'
);
}
const response = await callApi< PendingPostConfirmResponse >( {
apiNamespace: 'wpcom/v2',
path: `/post-comment-subscriptions/${ id }/confirm`,
method: 'POST',
isLoggedIn,
apiVersion: '2',
} );
if ( ! response.confirmed ) {
throw new Error(
// reminder: translate this string when we add it to the UI
'Something went wrong while confirming subscription.'
);
}
return response;
},
onMutate: async ( { id } ) => {
await queryClient.cancelQueries( {
queryKey: [ 'read', 'pending-post-subscriptions', isLoggedIn ],
} );
await queryClient.cancelQueries( { queryKey: subscriptionsCacheKey } );
await queryClient.cancelQueries( { queryKey: countCacheKey } );
const previousPendingPostSubscriptions =
queryClient.getQueryData< PendingPostSubscriptionsResult >( [
'read',
'pending-post-subscriptions',
isLoggedIn,
] );
// remove post comment from pending post subscriptions
if ( previousPendingPostSubscriptions?.pendingPosts ) {
queryClient.setQueryData< PendingPostSubscriptionsResult >(
[ [ 'read', 'pending-post-subscriptions', isLoggedIn ] ],
{
pendingPosts: previousPendingPostSubscriptions.pendingPosts.filter(
( pendingPost ) => pendingPost.id !== id
),
totalCount: previousPendingPostSubscriptions.totalCount - 1,
}
);
}
const previousSubscriptionsCount =
queryClient.getQueryData< SubscriptionManagerSubscriptionsCount >( countCacheKey );
// decrement the post comment count
if ( previousSubscriptionsCount ) {
queryClient.setQueryData< SubscriptionManagerSubscriptionsCount >( countCacheKey, {
...previousSubscriptionsCount,
comments: previousSubscriptionsCount?.comments
? previousSubscriptionsCount?.comments + 1
: 1,
pending: previousSubscriptionsCount?.pending
? previousSubscriptionsCount?.pending - 1
: null,
} );
}
return { previousPendingPostSubscriptions, previousSubscriptionsCount };
},
onError: ( error, variables, context ) => {
if ( context?.previousPendingPostSubscriptions ) {
queryClient.setQueryData< PendingPostSubscriptionsResult >(
[ 'read', 'pending-post-subscriptions', isLoggedIn ],
context.previousPendingPostSubscriptions
);
}
if ( context?.previousSubscriptionsCount ) {
queryClient.setQueryData< SubscriptionManagerSubscriptionsCount >(
countCacheKey,
context.previousSubscriptionsCount
);
}
},
onSettled: () => {
queryClient.invalidateQueries( {
queryKey: [ 'read', 'pending-post-subscriptions', isLoggedIn ],
} );
queryClient.invalidateQueries( { queryKey: subscriptionsCacheKey } );
queryClient.invalidateQueries( { queryKey: countCacheKey } );
},
} );
};
export default usePendingPostConfirmMutation;
|