Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Site Migration: Send from query param to the Atomic transfer API to be set on the back end #98938

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { useAnalyzeUrlQuery } from 'calypso/data/site-profiler/use-analyze-url-q
import { useQuery } from 'calypso/landing/stepper/hooks/use-query';
import { useSiteSlug } from 'calypso/landing/stepper/hooks/use-site-slug';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import wpcom from 'calypso/lib/wp';
import { GUIDED_ONBOARDING_FLOW_REFERRER } from 'calypso/signup/steps/initial-intent/constants';
import { useMigrationExperiment } from '../../hooks/use-migration-experiment';
import { useSitePreviewMShotImageHandler } from '../site-migration-instructions/site-preview/hooks/use-site-preview-mshot-image-handler';
Expand Down Expand Up @@ -193,31 +192,13 @@ export const Analyzer: FC< Props > = ( {

export type SiteMigrationIdentifyAction = 'continue' | 'skip_platform_identification';

const saveSiteSettings = async ( siteSlug: string, settings: Record< string, unknown > ) => {
return wpcom.req.post(
`/sites/${ siteSlug }/settings`,
{
apiVersion: '1.4',
},
{
...settings,
}
);
};

const SiteMigrationIdentify: Step = function ( { navigation, variantSlug, flow } ) {
const siteSlug = useSiteSlug();
const translate = useTranslate();
const { createScreenshots } = useSitePreviewMShotImageHandler();

const handleSubmit = useCallback(
async ( action: SiteMigrationIdentifyAction, data?: { platform: string; from: string } ) => {
// If we have a site and URL, and we're coming from a WordPress site,
// record the migration source domain.
if ( siteSlug && 'wordpress' === data?.platform && data?.from ) {
await saveSiteSettings( siteSlug, { migration_source_site_domain: data.from } );
}

// If we have a URL of the source, we send requests to the mShots API to create screenshots
// early in the flow to avoid long loading times in the migration instructions step.
// Because mShots API can often take a long time to generate screenshots.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe( 'SiteMigrationIdentify', () => {
restoreIsMigrationExperimentEnabled();
} );

it( 'continues the flow and saves the migration domain when the platform is wordpress', async () => {
it( 'continues the flow when the platform is wordpress', async () => {
useSiteSlug.mockReturnValue( MOCK_WORDPRESS_SITE_SLUG );

const submit = jest.fn();
Expand All @@ -69,15 +69,6 @@ describe( 'SiteMigrationIdentify', () => {
.query( { site_url: 'https://example.com' } )
.reply( 200, API_RESPONSE_WORDPRESS_PLATFORM );

const saveSettingsMock = mockApi()
.post(
`/rest/v1.4/sites/${ MOCK_WORDPRESS_SITE_SLUG }/settings`,
JSON.stringify( { migration_source_site_domain: API_RESPONSE_WORDPRESS_PLATFORM.url } )
)
.reply( 200, {
updated: { migration_source_site_domain: API_RESPONSE_WORDPRESS_PLATFORM.url },
} );

await userEvent.type( getInput(), 'https://example.com' );

await userEvent.click( screen.getByRole( 'button', { name: /Check my site/ } ) );
Expand All @@ -89,7 +80,6 @@ describe( 'SiteMigrationIdentify', () => {
from: API_RESPONSE_WORDPRESS_PLATFORM.url,
} )
);
expect( saveSettingsMock.isDone() ).toBeTruthy();
} );
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const SiteMigrationInstructions: Step = function ( { navigation, flow } ) {
completed: preparationCompleted,
error: preparationError,
migrationKey,
} = usePrepareSiteForMigration( siteId );
} = usePrepareSiteForMigration( siteId, fromUrl );

const migrationKeyStatus = detailedStatus.migrationKey;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ const siteMigration: Flow = {
{
siteId,
siteSlug,
from: fromQueryParam,
},
STEPS.SITE_MIGRATION_HOW_TO_MIGRATE.slug
)
Expand Down Expand Up @@ -347,6 +348,7 @@ const siteMigration: Flow = {
stepName: STEPS.SITE_MIGRATION_UPGRADE_PLAN.slug,
siteSlug: siteSlug,
destination: destination,
from: fromQueryParam ?? undefined,
plan: providedDependencies.plan as string,
cancelDestination: `/setup/${ flowPath }/${
STEPS.SITE_MIGRATION_UPGRADE_PLAN.slug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ describe( 'Site Migration Flow', () => {
destination: `/setup/site-migration/${ STEPS.SITE_MIGRATION_INSTRUCTIONS.slug }?siteSlug=example.wordpress.com&from=https%3A%2F%2Fsite-to-be-migrated.com`,
extraQueryParams: { hosting_intent: HOSTING_INTENT_MIGRATE },
flowName: 'site-migration',
from: 'https://site-to-be-migrated.com',
siteSlug: 'example.wordpress.com',
stepName: STEPS.SITE_MIGRATION_UPGRADE_PLAN.slug,
cancelDestination: `/setup/site-migration/${ STEPS.SITE_MIGRATION_UPGRADE_PLAN.slug }?siteSlug=example.wordpress.com&from=https%3A%2F%2Fsite-to-be-migrated.com`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ const useTransferTimeTracking = (
* Hook to manage the site to prepare a site for migration.
* This hook manages the site transfer, plugin installation and migration key fetching.
*/
export const usePrepareSiteForMigration = ( siteId?: number ) => {
export const usePrepareSiteForMigration = ( siteId?: number, from?: string ) => {
const plugin = { name: 'wpcom-migration/wpcom_migration', slug: 'wpcom-migration' };
const siteTransferState = useSiteTransfer( siteId );
const siteTransferState = useSiteTransfer( siteId, { from } );
const pluginInstallationState = usePluginAutoInstallation( plugin, siteId, {
enabled: Boolean( siteTransferState.completed ),
} );
Expand Down
2 changes: 1 addition & 1 deletion client/landing/stepper/hooks/use-site-transfer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSiteTransferMutation } from './mutation';
import { useSiteTransferStatusQuery } from './query';

type Status = 'idle' | 'pending' | 'success' | 'error';
type Options = Pick< UseQueryOptions, 'retry' >;
type Options = Pick< UseQueryOptions, 'retry' > & { from?: string };

/**
* Hook to initiate a site transfer and monitor its progress
Expand Down
12 changes: 9 additions & 3 deletions client/landing/stepper/hooks/use-site-transfer/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,31 @@ interface InitiateAtomicTransferResponse {
atomic_transfer_id: string;
}

const startTransfer = ( siteId: number ): Promise< InitiateAtomicTransferResponse > =>
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',
migration_source_site_domain: from,
},
} );

type Options = Pick< UseMutationOptions, 'retry' >;
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 ) : Promise.reject( new Error( 'siteId is required' ) );
siteId
? startTransfer( siteId, options?.from )
: Promise.reject( new Error( 'siteId is required' ) );

const refreshSiteStatus = ( data: InitiateAtomicTransferResponse ) => {
query.setQueryData( getSiteTransferStatusQueryKey( siteId! ), data );
Expand Down
1 change: 1 addition & 0 deletions client/landing/stepper/utils/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface GoToCheckoutProps {
stepName: string;
siteSlug: string;
destination: string;
from?: string;
plan?: string;
cancelDestination?: string;
extraProducts?: string[];
Expand Down
Loading