File size: 1,066 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
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { useCallback } from 'react';
import wp from 'calypso/lib/wp';

export const HOSTING_INTENT_MIGRATE = 'migrate';

type HostingIntent = typeof HOSTING_INTENT_MIGRATE;

interface Variables {
	siteId: number;
	planSlug: string;
	hostingIntent?: HostingIntent;
}

export default function useAddHostingTrialMutation(
	options: UseMutationOptions< unknown, unknown, Variables > = {}
) {
	const mutation = useMutation( {
		mutationFn: async ( { siteId, planSlug, hostingIntent }: Variables ) => {
			const body = hostingIntent ? { hosting_intent: hostingIntent } : undefined;
			return wp.req.post( {
				path: `/sites/${ siteId }/hosting/trial/add/${ planSlug }`,
				apiNamespace: 'wpcom/v2',
				body,
			} );
		},
		...options,
	} );

	const { mutate } = mutation;

	const addHostingTrial = useCallback(
		( siteId: number, planSlug: string, hostingIntent?: HostingIntent ) =>
			mutate( { siteId, planSlug, hostingIntent } ),
		[ mutate ]
	);

	return { addHostingTrial, ...mutation };
}