File size: 3,515 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
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { callApi } from '../helpers';
import { useCacheKey, useIsLoggedIn } from '../hooks';
import { subscriptionsCountQueryKeyPrefix } from '../queries/use-subscriptions-count-query';
import { PendingPostSubscriptionsResult, SubscriptionManagerSubscriptionsCount } from '../types';

type PendingPostDeleteParams = {
	id: number | string;
};

type PendingPostDeleteResponse = {
	deleted: boolean;
};

const usePendingPostDeleteMutation = () => {
	const { isLoggedIn } = useIsLoggedIn();
	const queryClient = useQueryClient();
	const countCacheKey = useCacheKey( subscriptionsCountQueryKeyPrefix );

	return useMutation( {
		mutationFn: async ( params: PendingPostDeleteParams ) => {
			if ( ! params.id ) {
				throw new Error(
					// reminder: translate this string when we add it to the UI
					'ID is missing.'
				);
			}

			const response = await callApi< PendingPostDeleteResponse >( {
				apiNamespace: 'wpcom/v2',
				path: `/post-comment-subscriptions/${ params.id }/delete`,
				method: 'POST',
				isLoggedIn,
				apiVersion: '2',
			} );
			if ( ! response.deleted ) {
				throw new Error(
					// reminder: translate this string when we add it to the UI
					'Something went wrong while deleting pending subscription.'
				);
			}

			return response;
		},
		onMutate: async ( params ) => {
			await queryClient.cancelQueries( {
				queryKey: [ 'read', 'pending-post-subscriptions', isLoggedIn ],
			} );
			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(
							( pendingPostSubscription ) => pendingPostSubscription.id !== params.id
						),
						totalCount: previousPendingPostSubscriptions.totalCount - 1,
					}
				);
			}

			const previousSubscriptionsCount =
				queryClient.getQueryData< SubscriptionManagerSubscriptionsCount >( countCacheKey );

			// decrement the post comment count
			if ( previousSubscriptionsCount ) {
				queryClient.setQueryData< SubscriptionManagerSubscriptionsCount >( countCacheKey, {
					...previousSubscriptionsCount,
					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: countCacheKey } );
		},
	} );
};

export default usePendingPostDeleteMutation;