File size: 3,447 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
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { addQueryArgs } from '@wordpress/url';
import { useEffect, useState } from 'react';
import { navigate } from 'calypso/lib/navigate';
import wp from 'calypso/lib/wp';
import { useDispatch, useSelector } from 'calypso/state';
import { requestAdminMenu } from 'calypso/state/admin-menu/actions';
import { getIsRequestingAdminMenu } from 'calypso/state/admin-menu/selectors';
import getRawSite from 'calypso/state/selectors/get-raw-site';
import { receiveSite, requestSite } from 'calypso/state/sites/actions';
import { getSiteAdminUrl } from 'calypso/state/sites/selectors';

const SET_SITE_INTERFACE_MUTATION_KEY = 'set-site-interface-mutation-key';
const PERSISTENT_DATA_DELAY = 1200;

interface MutationResponse {
	interface: 'wp-admin' | 'calypso';
}

interface MutationError {
	code: string;
	message: string;
}

interface UseSiteInterfaceMutationOptions
	extends UseMutationOptions< MutationResponse, MutationError, string > {
	onSuccess?: () => void;
}

function waitMs( ms: number ) {
	return new Promise( ( resolve ) => {
		setTimeout( () => {
			resolve( null );
		}, ms );
	} );
}

export const useSiteInterfaceMutation = (
	siteId: number,
	options: UseSiteInterfaceMutationOptions = {}
) => {
	const dispatch = useDispatch();
	const site = useSelector( ( state ) => getRawSite( state, siteId ) );
	const siteAdminUrl = useSelector( ( state ) => getSiteAdminUrl( state, site?.ID ) );
	const isRequestingMenu = useSelector( getIsRequestingAdminMenu );
	const [ hasSuccessfullyFinished, setHasSuccessfullyFinished ] = useState( false );
	useEffect( () => {
		if ( hasSuccessfullyFinished && ! isRequestingMenu ) {
			setHasSuccessfullyFinished( false );
			options?.onSuccess?.();
		}
	}, [ hasSuccessfullyFinished, isRequestingMenu, options ] );
	const queryKey = [ SET_SITE_INTERFACE_MUTATION_KEY, siteId ];

	const mutation = useMutation< MutationResponse, MutationError, string >( {
		mutationFn: async ( value: string ) => {
			const response = await wp.req.post(
				{
					path: `/sites/${ siteId }/hosting/admin-interface`,
					apiNamespace: 'wpcom/v2',
				},
				{
					interface: value,
				}
			);
			// Wait for persistent data to be updated on the atomic server
			await waitMs( PERSISTENT_DATA_DELAY );
			return response;
		},
		mutationKey: queryKey,
		onSuccess: ( ...params ) => {
			const [ data ] = params;
			if ( ! data?.interface || ! site ) {
				throw new Error( 'Invalid response from hosting/admin-interface' );
			}

			const newOptions = {
				...( site.options || {} ),
				wpcom_admin_interface: data.interface,
			};
			// Apply the new interface option to the site on redux store
			dispatch( receiveSite( { ...site, options: newOptions } ) );

			if ( data.interface === 'wp-admin' && siteAdminUrl ) {
				navigate( addQueryArgs( siteAdminUrl, { 'admin-interface-changed': true } ) );
			} else {
				dispatch( requestAdminMenu( siteId ) );
				setHasSuccessfullyFinished( true );
			}
		},
		onMutate: options?.onMutate,
		onError( _err: MutationError, _newActive: string, prevValue: unknown ) {
			// Request site info on failure
			dispatch( requestSite( siteId ) );
			dispatch( requestAdminMenu( siteId ) );
			options?.onError?.( _err, _newActive, prevValue );
		},
	} );

	const { mutate } = mutation;

	return {
		...mutation,
		setSiteInterface: mutate,
		isLoading: mutation.isPending || isRequestingMenu,
	};
};