File size: 1,703 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
import {
	useMutation,
	UseMutationOptions,
	UseMutationResult,
	useQueryClient,
} from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
import { useSelector } from 'calypso/state';
import { getActiveAgencyId } from 'calypso/state/a8c-for-agencies/agency/selectors';
import { getFetchDevLicensesQueryKey } from '../purchases/use-fetch-dev-licenses';

export interface APIError {
	status: number;
	code: string;
}

export interface CreateDevSiteParams {
	site_name?: string;
	php_version?: string;
	primary_data_center?: string;
	is_fully_managed_agency_site?: boolean;
}

interface APIResponse {
	site: {
		id: number;
		title: string;
		url: string;
		features: object;
	};
	license: object;
	provision: object;
}

function createWPCOMDevSiteMutation(
	params: CreateDevSiteParams,
	agencyId?: number
): Promise< APIResponse > {
	if ( ! agencyId ) {
		throw new Error( 'Agency ID is required to create a dev site' );
	}

	return wpcom.req.post( {
		apiNamespace: 'wpcom/v2',
		path: `/agency/${ agencyId }/sites/provision-dev-site`,
		body: params,
	} );
}

export default function useCreateWPCOMDevSiteMutation< TContext = unknown >(
	options?: UseMutationOptions< APIResponse, APIError, CreateDevSiteParams, TContext >
): UseMutationResult< APIResponse, APIError, CreateDevSiteParams, TContext > {
	const queryClient = useQueryClient();
	const agencyId = useSelector( getActiveAgencyId );

	return useMutation< APIResponse, APIError, CreateDevSiteParams, TContext >( {
		...options,
		mutationFn: ( args ) => createWPCOMDevSiteMutation( args, agencyId ),
		onSuccess: () => {
			queryClient.invalidateQueries( {
				queryKey: getFetchDevLicensesQueryKey( agencyId ),
			} );
		},
	} );
}