File size: 1,907 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 |
import { useMutation, UseMutationResult } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
export type TransferWithSoftwareResponse = {
blog_id: number;
transfer_id: number;
transfer_status: string;
};
type ApiSettings = Record< string, unknown >;
type TransferOptions = {
siteId: number;
apiSettings?: ApiSettings;
plugin_slug?: string;
theme_slug?: string;
};
const requestTransferWithSoftware: (
transferOptions: TransferOptions
) => Promise< TransferWithSoftwareResponse > = async ( {
siteId,
apiSettings,
plugin_slug,
theme_slug,
} ) => {
const response = await wpcom.req.post( {
path: `/sites/${ siteId }/atomic/transfer-with-software?http_envelope=1`,
apiNamespace: 'wpcom/v2',
body: {
plugin_slug: plugin_slug,
theme_slug: theme_slug,
settings: { ...apiSettings },
},
} );
if ( ! response ) {
throw new Error( 'Transfer with software failed' );
}
return response;
};
export const useRequestTransferWithSoftware = (
transferOptions: TransferOptions,
queryOptions?: {
retry?: number;
onSuccess?: ( data: TransferWithSoftwareResponse ) => void;
onError?: ( error: Error ) => void;
}
): UseMutationResult< TransferWithSoftwareResponse, Error, void > => {
return useMutation( {
mutationKey: [
'transfer-with-software',
transferOptions.siteId,
transferOptions.apiSettings,
transferOptions.plugin_slug,
transferOptions.theme_slug,
],
mutationFn: async () => {
if ( ! transferOptions.siteId ) {
throw new Error( 'Site ID is required' );
}
return requestTransferWithSoftware( {
siteId: transferOptions.siteId,
apiSettings: transferOptions.apiSettings,
plugin_slug: transferOptions.plugin_slug,
theme_slug: transferOptions.theme_slug,
} );
},
retry: queryOptions?.retry ?? 3, // Default retry 3 times
onSuccess: queryOptions?.onSuccess,
onError: queryOptions?.onError,
} );
};
|