File size: 1,385 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
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';

type TransferWithSoftwareStatusResponse = {
	blog_id: number;
	transfer_id: number;
	transfer_status: string;
};

const getTransferWithSoftwareStatus: (
	siteId?: number,
	transferId?: number
) => Promise< TransferWithSoftwareStatusResponse > = async ( siteId, transferId ) => {
	if ( ! siteId || ! transferId ) {
		return {
			blog_id: 0,
			transfer_id: 0,
			transfer_status: 'pending',
		};
	}
	return wpcom.req.get(
		`/sites/${ siteId }/atomic/transfer-with-software/${ transferId }?http_envelope=1`,
		{
			apiNamespace: 'wpcom/v2',
		}
	);
};

export const useTransferWithSoftwareStatus = (
	siteId?: number,
	transferId?: number,
	options?: {
		retry?: UseQueryOptions[ 'retry' ];
	}
) => {
	return useQuery( {
		queryKey: [ 'software-transfer-status', siteId, transferId ],
		queryFn: () => getTransferWithSoftwareStatus( siteId, transferId ),
		select: ( data: TransferWithSoftwareStatusResponse ) => ( {
			transfer_status: data.transfer_status,
		} ),
		refetchOnWindowFocus: false,
		refetchOnReconnect: false,
		refetchInterval: ( { state } ) => {
			if ( state.data?.transfer_status === 'completed' ) {
				return false;
			}
			return 5000;
		},
		retry: options?.retry ?? false,
		enabled: !! siteId && !! transferId, // Only run when both values exist.
	} );
};