File size: 3,738 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import config from '@automattic/calypso-config';
import {
	InfiniteQueryObserverResult,
	keepPreviousData,
	useInfiniteQuery,
	useQuery,
} from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
import { SearchOptions } from 'calypso/my-sites/promote-post-i2/components/search-bar';
import { PostQueryResult } from './types';

type BlazablePostsQueryOptions = {
	page?: number;
};

interface PostQueryResultError {
	code?: string;
}

export const getSearchOptionsQueryParams = ( searchOptions: SearchOptions ) => {
	let searchQueryParams = '';

	if ( searchOptions.search ) {
		searchQueryParams += `&title=${ searchOptions.search }`;
	}
	if ( searchOptions.filter ) {
		if ( searchOptions.filter.status && searchOptions.filter.status !== 'all' ) {
			searchQueryParams += `&status=${ searchOptions.filter.status }`;
		}
		if ( searchOptions.filter.postType && searchOptions.filter.postType !== 'all' ) {
			searchQueryParams += `&filter_post_type=${ searchOptions.filter.postType }`;
		}
	}
	if ( searchOptions.order ) {
		searchQueryParams += `&order_by=${ searchOptions.order.orderBy }`;
		searchQueryParams += `&order=${ searchOptions.order.order }`;
	}

	return searchQueryParams;
};

async function queryPosts( siteId: number, queryparams: string ) {
	return await wpcom.req.get( {
		path: `/sites/${ siteId }/blaze/posts?${ queryparams }`,
		apiNamespace: config.isEnabled( 'is_running_in_jetpack_site' )
			? 'jetpack/v4/blaze-app'
			: 'wpcom/v2',
	} );
}

export const usePostsQueryStats = ( siteId: number, queryOptions = {} ) => {
	return useQuery( {
		queryKey: [ 'promote-post-posts-stats', siteId ],
		queryFn: async () => {
			const postsResponse = await queryPosts( siteId, 'page=1&posts_per_page=1' );
			return {
				total_items: postsResponse?.total_items,
			};
		},
		...queryOptions,
		enabled: !! siteId,
		retryDelay: 3000,
		refetchOnWindowFocus: false,
		meta: {
			persist: false,
		},
	} );
};

const usePostsQueryPaged = (
	siteId: number,
	searchOptions: SearchOptions,
	queryOptions: BlazablePostsQueryOptions = {}
): InfiniteQueryObserverResult< PostQueryResult, PostQueryResultError > => {
	const searchQueryParams = getSearchOptionsQueryParams( searchOptions );
	return useInfiniteQuery( {
		queryKey: [ 'promote-post-posts', siteId, searchQueryParams ],
		queryFn: async ( { pageParam } ) => {
			// Fetch blazable posts
			const postsResponse = await queryPosts( siteId, `page=${ pageParam }${ searchQueryParams }` );

			const {
				posts,
				page,
				total_items,
				total_pages,
				warnings,
				tsp_eligible = false,
			} = postsResponse;
			const has_more_pages = page < total_pages;

			return {
				posts,
				has_more_pages,
				total_items,
				total_pages,
				page,
				warnings,
				tsp_eligible,
			};
		},
		...queryOptions,
		enabled: !! siteId,
		retryDelay: 3000,
		placeholderData: keepPreviousData,
		refetchOnWindowFocus: false,
		meta: {
			persist: false,
		},
		initialPageParam: 1,
		getNextPageParam: ( lastPage ) => {
			if ( lastPage.has_more_pages ) {
				return lastPage.page + 1;
			}
			return undefined;
		},
		retry: ( failureCount, error ) => {
			// The posts_not_ready happens when the posts are not sync yet
			// We want to show the error ASAP not waiting the retries in this case.
			if ( error.hasOwnProperty( 'code' ) && 'posts_not_ready' === error.code ) {
				return false;
			}

			// We have to define a fallback amount of failures because we
			// override the retry option with a function.
			// We use 3 as the failureCount since its the default value for
			// react-query that we used before.
			// @link https://react-query.tanstack.com/guides/query-retries
			return 3 > failureCount;
		},
	} );
};

export default usePostsQueryPaged;