Skip to content

Commit

Permalink
Stepper: Leave checkout bring you to site-setup in onboarding (#97244)
Browse files Browse the repository at this point in the history
* Leave cart bring you to site-setup in onboarding

* Do not touch Start

* Use function already in Start, adds tests

* Renamed test

* Fix tests

* Unused code

* Add skippedCheckout
  • Loading branch information
escapemanuele authored Dec 10, 2024
1 parent f9ebb75 commit 227c0c1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
2 changes: 2 additions & 0 deletions client/landing/stepper/declarative-flow/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { addQueryArgs, getQueryArg, getQueryArgs, removeQueryArgs } from '@wordpress/url';
import { useState } from 'react';
import { SIGNUP_DOMAIN_ORIGIN } from 'calypso/lib/analytics/signup';
import { pathToUrl } from 'calypso/lib/url';
import {
persistSignupDestination,
setSignupCompleteFlowName,
Expand Down Expand Up @@ -219,6 +220,7 @@ const onboarding: Flow = {
addQueryArgs( `/checkout/${ encodeURIComponent( siteSlug ) }`, {
redirect_to: destination,
signup: 1,
checkoutBackUrl: pathToUrl( addQueryArgs( destination, { skippedCheckout: 1 } ) ),
} )
);
} else {
Expand Down
1 change: 1 addition & 0 deletions client/lib/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { default as isHttps } from './is-https';
export { addSchemeIfMissing, setUrlScheme } from './scheme-utils';
export { decodeURIIfValid, decodeURIComponentIfValid } from './decode-utils';
export { default as resolveRelativePath } from './resolve-relative-path';
export { pathToUrl } from './path-to-url';
13 changes: 13 additions & 0 deletions client/lib/url/path-to-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import config from '@automattic/calypso-config';

export function pathToUrl( path: string ) {
if ( config( 'env' ) !== 'production' ) {
const protocol = config( 'protocol' ) ?? 'https';
const port = config( 'port' ) ? ':' + config( 'port' ) : '';
const hostName = config( 'hostname' );

return `${ protocol }://${ hostName }${ port }${ path }`;
}

return `https://${ config( 'hostname' ) }${ path }`;
}
46 changes: 46 additions & 0 deletions client/lib/url/test/path-to-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import config from '@automattic/calypso-config';
import { pathToUrl } from '../path-to-url';

jest.mock( '@automattic/calypso-config', () => jest.fn() );

const mockConfig = config as unknown as jest.Mock;

describe( 'pathToUrl', () => {
beforeEach( () => {
mockConfig.mockReset();
} );

test( 'should return production URL', () => {
mockConfig.mockImplementation( ( key: string ) => {
switch ( key ) {
case 'env':
return 'production';
case 'hostname':
return 'wordpress.com';
default:
return undefined;
}
} );

expect( pathToUrl( '/start' ) ).toBe( 'https://wordpress.com/start' );
} );

test( 'should return development URL', () => {
mockConfig.mockImplementation( ( key: string ) => {
switch ( key ) {
case 'env':
return 'development';
case 'protocol':
return 'http';
case 'hostname':
return 'calypso.localhost';
case 'port':
return '3000';
default:
return undefined;
}
} );

expect( pathToUrl( '/start' ) ).toBe( 'http://calypso.localhost:3000/start' );
} );
} );
17 changes: 2 additions & 15 deletions client/signup/config/flows.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import config from '@automattic/calypso-config';
import { getPlan, TYPE_ECOMMERCE, TYPE_BUSINESS } from '@automattic/calypso-products/';
import {
PREMIUM_THEME,
Expand All @@ -12,22 +11,10 @@ import { isURL } from '@wordpress/url';
import { get, includes, reject } from 'lodash';
import { getPlanCartItem } from 'calypso/lib/cart-values/cart-items';
import { getQueryArgs } from 'calypso/lib/query-args';
import { addQueryArgs } from 'calypso/lib/url';
import { addQueryArgs, pathToUrl } from 'calypso/lib/url';
import { generateFlows } from 'calypso/signup/config/flows-pure';
import stepConfig from './steps';

function constructBackUrlFromPath( path ) {
if ( config( 'env' ) !== 'production' ) {
const protocol = config( 'protocol' ) ?? 'https';
const port = config( 'port' ) ? ':' + config( 'port' ) : '';
const hostName = config( 'hostname' );

return `${ protocol }://${ hostName }${ port }${ path }`;
}

return `https://${ config( 'hostname' ) }${ path }`;
}

function getCheckoutUrl( dependencies, localeSlug, flowName, destination ) {
let checkoutURL = `/checkout/${ dependencies.siteSlug }`;

Expand All @@ -47,7 +34,7 @@ function getCheckoutUrl( dependencies, localeSlug, flowName, destination ) {
// the domain only flow has special rule. Ideally they should also be configurable in flows-pure.
const checkoutBackUrl = isURL( destination )
? destination
: constructBackUrlFromPath( isDomainOnly ? `/start/${ flowName }/domain-only` : destination );
: pathToUrl( isDomainOnly ? `/start/${ flowName }/domain-only` : destination );

return addQueryArgs(
{
Expand Down

0 comments on commit 227c0c1

Please sign in to comment.