File size: 2,277 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
import { useQuery } from '@tanstack/react-query';
import { callApi } from '../helpers';
import { useIsLoggedIn, useIsQueryEnabled } from '../hooks';
import type { PendingPostSubscription, PendingPostSubscriptionsResult } from '../types';

type SubscriptionManagerPendingPostSubscriptions = {
	comment_subscriptions: PendingPostSubscription[];
	total_comment_subscriptions_count: number;
};

type SubscriptionManagerPendingPostSubscriptionsQueryProps = {
	filter?: ( item?: PendingPostSubscription ) => boolean;
	sort?: ( a?: PendingPostSubscription, b?: PendingPostSubscription ) => number;
};

const callPendingBlogSubscriptionsEndpoint = async (
	isLoggedIn: boolean
): Promise< PendingPostSubscriptionsResult > => {
	const pendingPosts = [];
	const perPage = 1000; // TODO: This is a temporary workaround to get all pending subscriptions. We should remove this once we decide how to handle pagination.

	const incoming = await callApi< SubscriptionManagerPendingPostSubscriptions >( {
		path: `/post-comment-subscriptions?status=pending&per_page=${ perPage }`,
		apiNamespace: 'wpcom/v2',
		apiVersion: '2',
		isLoggedIn,
	} );

	if ( incoming && incoming.comment_subscriptions ) {
		pendingPosts.push(
			...incoming.comment_subscriptions.map( ( pendingSubscription ) => ( {
				...pendingSubscription,
				date_subscribed: new Date( pendingSubscription.date_subscribed ),
			} ) )
		);
	}

	return {
		pendingPosts,
		totalCount: incoming?.total_comment_subscriptions_count ?? 0,
	};
};

const defaultFilter = () => true;
const defaultSort = () => 0;

const usePendingPostSubscriptionsQuery = ( {
	filter = defaultFilter,
	sort = defaultSort,
}: SubscriptionManagerPendingPostSubscriptionsQueryProps = {} ) => {
	const { isLoggedIn } = useIsLoggedIn();
	const enabled = useIsQueryEnabled();

	const { data, ...rest } = useQuery< PendingPostSubscriptionsResult >( {
		queryKey: [ 'read', 'pending-post-subscriptions', isLoggedIn ],
		queryFn: async () => {
			return await callPendingBlogSubscriptionsEndpoint( isLoggedIn );
		},
		enabled,
		refetchOnWindowFocus: false,
	} );

	return {
		data: {
			pendingPosts: data?.pendingPosts?.filter( filter ).sort( sort ),
			totalCount: data?.totalCount,
		},
		...rest,
	};
};

export default usePendingPostSubscriptionsQuery;