File size: 11,680 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
import { isEnabled } from '@automattic/calypso-config';
import { DomainSuggestion, OnboardActions, OnboardSelect } from '@automattic/data-stores';
import { ONBOARDING_FLOW, clearStepPersistedState } from '@automattic/onboarding';
import { MinimalRequestCartProduct } from '@automattic/shopping-cart';
import { useDispatch, useSelect } from '@wordpress/data';
import { addQueryArgs, getQueryArg, getQueryArgs, removeQueryArgs } from '@wordpress/url';
import { useState, useEffect } from 'react';
import { isSimplifiedOnboarding } from 'calypso/landing/stepper/hooks/use-simplified-onboarding';
import { SIGNUP_DOMAIN_ORIGIN } from 'calypso/lib/analytics/signup';
import { pathToUrl } from 'calypso/lib/url';
import {
persistSignupDestination,
setSignupCompleteFlowName,
setSignupCompleteSlug,
clearSignupCompleteSlug,
clearSignupCompleteFlowName,
clearSignupDestinationCookie,
} from 'calypso/signup/storageUtils';
import { useDispatch as useReduxDispatch } from 'calypso/state';
import { setSelectedSiteId } from 'calypso/state/ui/actions';
import { STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT } from '../../../constants';
import { useFlowLocale } from '../../../hooks/use-flow-locale';
import { useQuery } from '../../../hooks/use-query';
import { ONBOARD_STORE } from '../../../stores';
import { stepsWithRequiredLogin } from '../../../utils/steps-with-required-login';
import { recordStepNavigation } from '../../internals/analytics/record-step-navigation';
import { usePurchasePlanNotification } from '../../internals/hooks/use-purchase-plan-notification';
import { STEPS } from '../../internals/steps';
import { ProcessingResult } from '../../internals/steps-repository/processing-step/constants';
import type { FlowV2, ProvidedDependencies, SubmitHandler } from '../../internals/types';
const clearUseMyDomainsQueryParams = ( currentStepSlug: string | undefined ) => {
const isDomainsStep = currentStepSlug === 'domains';
const isPlansStepWithQuery =
currentStepSlug === 'plans' && getQueryArg( window.location.href, 'step' );
if ( isDomainsStep || isPlansStepWithQuery ) {
const { pathname, search } = window.location;
const newURL = removeQueryArgs( pathname + search, 'step', 'initialQuery', 'lastQuery' );
window.history.replaceState( {}, document.title, newURL );
}
};
const withLocale = ( url: string, locale: string ) => {
return locale && locale !== 'en' ? `${ url }/${ locale }` : url;
};
function initialize() {
const steps = [
STEPS.UNIFIED_DOMAINS,
STEPS.USE_MY_DOMAIN,
STEPS.UNIFIED_PLANS,
STEPS.SITE_CREATION_STEP,
STEPS.PROCESSING,
STEPS.POST_CHECKOUT_ONBOARDING,
];
return [ ...stepsWithRequiredLogin( steps ), STEPS.PLAYGROUND ];
}
const onboarding: FlowV2< typeof initialize > = {
name: ONBOARDING_FLOW,
isSignupFlow: true,
__experimentalUseBuiltinAuth: true,
initialize,
useStepNavigation( currentStepSlug, navigate ) {
const flowName = this.name;
const {
setDomain,
setDomainCartItem,
setDomainCartItems,
setPlanCartItem,
setProductCartItems,
setSiteUrl,
setSignupDomainOrigin,
} = useDispatch( ONBOARD_STORE ) as OnboardActions;
const locale = useFlowLocale();
const { signupDomainOrigin } = useSelect(
( select ) => ( {
signupDomainOrigin: ( select( ONBOARD_STORE ) as OnboardSelect ).getSignupDomainOrigin(),
} ),
[]
);
const coupon = useQuery().get( 'coupon' );
const [ useMyDomainTracksEventProps, setUseMyDomainTracksEventProps ] = useState( {} );
const { setShouldShowNotification } = usePurchasePlanNotification();
/**
* Returns [destination, backDestination] for the post-checkout destination.
*/
const getPostCheckoutDestination = async (
providedDependencies: ProvidedDependencies
): Promise< [ string, string | null ] > => {
if ( ! providedDependencies.hasExternalTheme && providedDependencies.hasPluginByGoal ) {
return [ `/home/${ providedDependencies.siteSlug }`, null ];
}
const playgroundId = getQueryArg( window.location.href, 'playground' );
if ( playgroundId && providedDependencies.siteSlug ) {
return [
addQueryArgs( withLocale( '/setup/site-setup/importerPlayground', locale ), {
siteSlug: providedDependencies.siteSlug,
siteId: providedDependencies.siteId,
playground: playgroundId,
} ),
null,
];
}
/**
* If the dashboard/v2/onboarding feature flag is enabled, we'll redirect the user to the new hosting Dashboard.
* We aren't using the dashboard/v2 FF because it's enabled by default on wpcalypso.json which would break e2e tests.
* Since we're aiming to remove steps after the isMvpOnboarding experiment ends,
* we'll redirect the user to the new Dashboard here.
*/
if ( isEnabled( 'dashboard/v2/onboarding' ) ) {
return [
addQueryArgs( `/v2/sites/${ providedDependencies.siteSlug }`, { ref: flowName } ),
addQueryArgs( withLocale( `/setup/${ flowName }/plans`, locale ), {
siteSlug: providedDependencies.siteSlug,
} ),
];
}
if ( await isSimplifiedOnboarding() ) {
return [
addQueryArgs( `/home/${ providedDependencies.siteSlug }`, { ref: flowName } ),
addQueryArgs( withLocale( `/setup/${ flowName }/plans`, locale ), {
siteSlug: providedDependencies.siteSlug,
} ),
];
}
const destination = addQueryArgs( withLocale( '/setup/site-setup', locale ), {
siteSlug: providedDependencies.siteSlug,
} );
return [ destination, addQueryArgs( destination, { skippedCheckout: 1 } ) ];
};
clearUseMyDomainsQueryParams( currentStepSlug );
const submit: SubmitHandler< typeof initialize > = async ( submittedStep ) => {
const { slug, providedDependencies } = submittedStep;
switch ( slug ) {
case 'domains':
setSiteUrl( providedDependencies.siteUrl as string );
setDomain( providedDependencies.suggestion as DomainSuggestion );
setDomainCartItem( providedDependencies.domainItem as MinimalRequestCartProduct );
setDomainCartItems( providedDependencies.domainCart as MinimalRequestCartProduct[] );
setSignupDomainOrigin( providedDependencies.signupDomainOrigin as string );
if ( providedDependencies.navigateToUseMyDomain ) {
const currentQueryArgs = getQueryArgs( window.location.href );
currentQueryArgs.step = 'domain-input';
let useMyDomainURL = addQueryArgs( '/use-my-domain', currentQueryArgs );
const lastQueryParam = ( providedDependencies?.domainForm as { lastQuery?: string } )
?.lastQuery;
if ( lastQueryParam !== undefined ) {
currentQueryArgs.initialQuery = lastQueryParam;
useMyDomainURL = addQueryArgs( useMyDomainURL, currentQueryArgs );
}
setUseMyDomainTracksEventProps( {
site_url: providedDependencies.siteUrl,
signup_domain_origin: signupDomainOrigin,
domain_item: providedDependencies.domainItem,
} );
return navigate( useMyDomainURL as typeof currentStepSlug );
}
return navigate( 'plans' );
case 'use-my-domain':
setSignupDomainOrigin( SIGNUP_DOMAIN_ORIGIN.USE_YOUR_DOMAIN );
if ( providedDependencies?.mode && providedDependencies?.domain ) {
setUseMyDomainTracksEventProps( {
...useMyDomainTracksEventProps,
signup_domain_origin: SIGNUP_DOMAIN_ORIGIN.USE_YOUR_DOMAIN,
site_url: providedDependencies.domain,
} );
const destination = addQueryArgs( '/use-my-domain', {
...getQueryArgs( window.location.href ),
step: providedDependencies.mode,
initialQuery: providedDependencies.domain,
} );
return navigate( destination as typeof currentStepSlug );
}
// We trigger the event here, because we skip it in the domains step if
// the user chose use-my-domain
recordStepNavigation( {
event: STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT,
flow: this.name,
intent: '',
step: 'domains',
providedDependencies: useMyDomainTracksEventProps,
} );
return navigate( 'plans' );
case 'plans': {
const cartItems = providedDependencies.cartItems;
const [ pickedPlan, ...products ] = cartItems ?? [];
setPlanCartItem( pickedPlan );
if ( ! pickedPlan ) {
// Since we're removing the paid domain, it means that the user chose to continue
// with a free domain. Because signupDomainOrigin should reflect the last domain
// selection status before they land on the checkout page, this value can be
// 'free' or 'choose-later'
if ( signupDomainOrigin === 'choose-later' ) {
setSignupDomainOrigin( signupDomainOrigin );
} else {
setSignupDomainOrigin( SIGNUP_DOMAIN_ORIGIN.FREE );
}
}
// Make sure to put the rest of products into the cart, e.g. the storage add-ons.
setProductCartItems( products.filter( ( product ) => product !== null ) );
setSignupCompleteFlowName( flowName );
return navigate( 'create-site', undefined, false );
}
case 'create-site':
return navigate( 'processing', undefined, true );
case 'post-checkout-onboarding':
setShouldShowNotification( providedDependencies?.siteId as number );
return navigate( 'processing' );
case 'processing': {
const [ destination, backDestination ] =
await getPostCheckoutDestination( providedDependencies );
if ( providedDependencies.processingResult === ProcessingResult.SUCCESS ) {
persistSignupDestination( destination );
setSignupCompleteFlowName( flowName );
setSignupCompleteSlug( providedDependencies.siteSlug );
if ( providedDependencies.goToCheckout ) {
const siteSlug = providedDependencies.siteSlug as string;
/**
* If the user comes from the Playground onboarding flow,
* redirect the user back to Playground to start the import.
*/
const playgroundId = getQueryArg( window.location.href, 'playground' );
const redirectTo: string = playgroundId
? addQueryArgs( withLocale( '/setup/site-setup/importerPlayground', locale ), {
siteSlug,
siteId: providedDependencies.siteId,
playground: playgroundId,
} )
: addQueryArgs(
withLocale( '/setup/onboarding/post-checkout-onboarding', locale ),
{
siteSlug,
}
);
// replace the location to delete processing step from history.
window.location.replace(
addQueryArgs( `/checkout/${ encodeURIComponent( siteSlug ) }`, {
redirect_to: redirectTo,
signup: 1,
checkoutBackUrl: pathToUrl( backDestination ?? '' ),
coupon,
} )
);
} else {
// replace the location to delete processing step from history.
window.location.replace( destination );
}
} else {
// TODO: Handle errors
// navigate( 'error' );
}
return;
}
case 'playground':
return navigate( 'domains' );
default:
return;
}
};
return { submit };
},
useSideEffect( currentStepSlug ) {
const reduxDispatch = useReduxDispatch();
const { resetOnboardStore } = useDispatch( ONBOARD_STORE );
/**
* Clears every state we're persisting during the flow
* when entering it. This is to ensure that the user
* starts on a clean slate.
*/
useEffect( () => {
if ( ! currentStepSlug ) {
resetOnboardStore();
reduxDispatch( setSelectedSiteId( null ) );
clearStepPersistedState( this.name );
clearSignupDestinationCookie();
clearSignupCompleteFlowName();
clearSignupCompleteSlug();
}
}, [ currentStepSlug, reduxDispatch, resetOnboardStore ] );
},
};
export default onboarding;
|