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

interface MigrationStickerMutationOptions {
	targetBlogId: SiteId;
}

export const useMigrationStickerMutation = () => {
	const addMutation = useMutation( {
		mutationFn: ( { targetBlogId }: MigrationStickerMutationOptions ) =>
			wp.req.post( {
				path: `/sites/${ targetBlogId }/migration-flow`,
				apiNamespace: 'wpcom/v2',
			} ),
	} );

	const deleteMutation = useMutation( {
		mutationFn: ( { targetBlogId }: MigrationStickerMutationOptions ) => {
			return wp.req.post( {
				path: `/sites/${ targetBlogId }/migration-flow`,
				apiNamespace: 'wpcom/v2',
				method: 'DELETE',
			} );
		},
	} );

	const { mutate: addMutate, ...addMutationRest } = addMutation;
	const { mutate: deleteMutate, ...deleteMutationRest } = deleteMutation;

	const addMigrationSticker = useCallback(
		( targetBlogId: SiteId ) => addMutate( { targetBlogId } ),
		[ addMutate ]
	);

	const deleteMigrationSticker = useCallback(
		( targetBlogId: SiteId ) => {
			if ( ! config.isEnabled( 'migration-flow/introductory-offer' ) ) {
				return;
			}

			deleteMutate( { targetBlogId } );
		},
		[ deleteMutate ]
	);

	return {
		addMigrationSticker,
		deleteMigrationSticker,
		addMutationRest,
		deleteMutationRest,
	};
};