Download wp-calypso/client/landing/stepper/hooks/use-site-transfer/mutation.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/landing/stepper/hooks/use-site-transfer/mutation.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/landing/stepper/hooks/use-site-transfer/mutation.ts
-
curl -L -o mutation.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/landing/stepper/hooks/use-site-transfer/mutation.ts
1.79 kB
| import { recordTracksEvent } from '@automattic/calypso-analytics'; | |
| import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'; | |
| import wpcom from 'calypso/lib/wp'; | |
| import { TransferStates } from 'calypso/state/automated-transfer/constants'; | |
| import { getSiteTransferStatusQueryKey } from './query'; | |
| interface InitiateAtomicTransferResponse { | |
| status: TransferStates; | |
| atomic_transfer_id: string; | |
| } | |
| const startTransfer = ( | |
| siteId: number, | |
| from?: string | |
| ): Promise< InitiateAtomicTransferResponse > => | |
| wpcom.req.post( { | |
| path: `/sites/${ siteId }/atomic/transfers`, | |
| apiNamespace: 'wpcom/v2', | |
| body: { | |
| context: 'unknown', | |
| transfer_intent: 'migrate', | |
| plugin_slug: 'wpcom-migration', | |
| migration_source_site_domain: from, | |
| }, | |
| } ); | |
| type Options = Pick< UseMutationOptions, 'retry' > & { from?: string }; | |
| /** | |
| * Mutation hook to initiate a site transfer | |
| */ | |
| export const useSiteTransferMutation = ( siteId?: number, options?: Options ) => { | |
| const query = useQueryClient(); | |
| const mutation = () => | |
| siteId | |
| ? startTransfer( siteId, options?.from ) | |
| : Promise.reject( new Error( 'siteId is required' ) ); | |
| const refreshSiteStatus = ( data: InitiateAtomicTransferResponse ) => { | |
| query.setQueryData( getSiteTransferStatusQueryKey( siteId! ), data ); | |
| query.refetchQueries( { queryKey: getSiteTransferStatusQueryKey( siteId! ), exact: true } ); | |
| }; | |
| return useMutation( { | |
| mutationKey: [ 'sites', siteId, 'atomic', 'transfers' ], | |
| mutationFn: mutation, | |
| onSuccess: refreshSiteStatus, | |
| retry: options?.retry ?? 10, | |
| onSettled: ( data, error ) => { | |
| recordTracksEvent( 'calypso_site_transfer_started', { | |
| site_id: siteId, | |
| status: !! error || 'success', | |
| atomic_transfer_id: data?.atomic_transfer_id, | |
| } ); | |
| }, | |
| } ); | |
| }; | |