|
|
import { |
|
|
PREMIUM_THEME, |
|
|
DOT_ORG_THEME, |
|
|
BUNDLED_THEME, |
|
|
MARKETPLACE_THEME, |
|
|
} from '@automattic/design-picker'; |
|
|
import { DOMAIN_FOR_GRAVATAR_FLOW, isDomainForGravatarFlow } from '@automattic/onboarding'; |
|
|
import { isURL } from '@wordpress/url'; |
|
|
import { get, includes, reject } from 'lodash'; |
|
|
import { getQueryArgs } from 'calypso/lib/query-args'; |
|
|
import { addQueryArgs, pathToUrl } from 'calypso/lib/url'; |
|
|
import { generateFlows } from 'calypso/signup/config/flows-pure'; |
|
|
import stepConfig from './steps'; |
|
|
|
|
|
function getCheckoutUrl( dependencies, localeSlug, flowName, destination ) { |
|
|
let checkoutURL = `/checkout/${ dependencies.siteSlug }`; |
|
|
|
|
|
|
|
|
if ( 'no-site' === dependencies.siteSlug && true === dependencies.allowUnauthenticated ) { |
|
|
checkoutURL += `/${ localeSlug }`; |
|
|
} |
|
|
|
|
|
const isDomainOnly = [ 'domain', DOMAIN_FOR_GRAVATAR_FLOW ].includes( flowName ); |
|
|
const isGravatarDomain = isDomainForGravatarFlow( flowName ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const checkoutBackUrl = isURL( destination ) |
|
|
? destination |
|
|
: pathToUrl( isDomainOnly ? `/start/${ flowName }/domain-only` : destination ); |
|
|
|
|
|
|
|
|
const isLaunchSiteFlow = flowName === 'launch-site'; |
|
|
const finalCheckoutBackUrl = isLaunchSiteFlow |
|
|
? addQueryArgs( { skippedCheckout: 1, celebrateLaunch: 'true' }, checkoutBackUrl ) |
|
|
: addQueryArgs( { skippedCheckout: 1 }, checkoutBackUrl ); |
|
|
|
|
|
return addQueryArgs( |
|
|
{ |
|
|
signup: 1, |
|
|
ref: getQueryArgs()?.ref, |
|
|
...( dependencies.coupon && { coupon: dependencies.coupon } ), |
|
|
...( isDomainOnly && { isDomainOnly: 1 } ), |
|
|
...( isGravatarDomain && { isGravatarDomain: 1 } ), |
|
|
checkoutBackUrl: finalCheckoutBackUrl, |
|
|
}, |
|
|
checkoutURL |
|
|
); |
|
|
} |
|
|
|
|
|
function dependenciesContainCartItem( dependencies ) { |
|
|
|
|
|
|
|
|
return dependencies.cartItem || dependencies.domainItem || dependencies.cartItems; |
|
|
} |
|
|
|
|
|
function getRedirectDestination( dependencies ) { |
|
|
try { |
|
|
if ( |
|
|
dependencies.oauth2_redirect && |
|
|
new URL( dependencies.oauth2_redirect ).host === 'public-api.wordpress.com' |
|
|
) { |
|
|
return dependencies.oauth2_redirect; |
|
|
} else if ( dependencies.redirect ) { |
|
|
return dependencies.redirect; |
|
|
} |
|
|
} catch { |
|
|
return '/'; |
|
|
} |
|
|
|
|
|
return '/'; |
|
|
} |
|
|
|
|
|
function getSignupDestination( { domainItem, siteId, siteSlug, refParameter } ) { |
|
|
if ( 'no-site' === siteSlug ) { |
|
|
return '/home'; |
|
|
} |
|
|
let queryParam = { siteSlug, siteId }; |
|
|
if ( domainItem ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
queryParam = { siteId }; |
|
|
} |
|
|
|
|
|
|
|
|
if ( refParameter ) { |
|
|
queryParam.ref = refParameter; |
|
|
} |
|
|
|
|
|
return addQueryArgs( queryParam, '/setup/site-setup' ); |
|
|
} |
|
|
|
|
|
function getLaunchDestination( dependencies ) { |
|
|
return addQueryArgs( { celebrateLaunch: 'true' }, `/home/${ dependencies.siteSlug }` ); |
|
|
} |
|
|
|
|
|
function getDomainSignupFlowDestination( { domainItem, cartItem, siteId, designType, siteSlug } ) { |
|
|
if ( domainItem && cartItem && designType !== 'existing-site' ) { |
|
|
return addQueryArgs( { siteId }, '/start/setup-site' ); |
|
|
} else if ( designType === 'existing-site' ) { |
|
|
return `/checkout/thank-you/${ siteSlug }`; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return '/checkout/thank-you/no-site'; |
|
|
} |
|
|
|
|
|
function getEmailSignupFlowDestination( { siteId, siteSlug } ) { |
|
|
return addQueryArgs( |
|
|
{ siteId }, |
|
|
`/checkout/thank-you/features/email-license/${ siteSlug }/:receiptId` |
|
|
); |
|
|
} |
|
|
|
|
|
function getChecklistThemeDestination( { siteSlug } ) { |
|
|
return `/home/${ siteSlug }`; |
|
|
} |
|
|
|
|
|
function getWithThemeDestination( { |
|
|
siteSlug, |
|
|
themeParameter, |
|
|
styleVariation, |
|
|
themeType, |
|
|
cartItems, |
|
|
} ) { |
|
|
if ( |
|
|
! cartItems && |
|
|
[ DOT_ORG_THEME, PREMIUM_THEME, MARKETPLACE_THEME, BUNDLED_THEME ].includes( themeType ) |
|
|
) { |
|
|
return `/setup/site-setup/design-setup?siteSlug=${ siteSlug }`; |
|
|
} |
|
|
|
|
|
if ( DOT_ORG_THEME === themeType ) { |
|
|
return `/marketplace/theme/${ themeParameter }/install/${ siteSlug }`; |
|
|
} |
|
|
|
|
|
const style = styleVariation ? `&styleVariation=${ styleVariation }` : ''; |
|
|
|
|
|
if ( [ MARKETPLACE_THEME, PREMIUM_THEME, BUNDLED_THEME ].includes( themeType ) ) { |
|
|
return `/marketplace/thank-you/${ siteSlug }?onboarding=&themes=${ themeParameter }${ style }`; |
|
|
} |
|
|
|
|
|
return `/setup/site-setup/design-setup?siteSlug=${ siteSlug }&theme=${ themeParameter }${ style }`; |
|
|
} |
|
|
|
|
|
function getWithPluginDestination( { siteSlug, pluginParameter, pluginBillingPeriod } ) { |
|
|
|
|
|
if ( pluginBillingPeriod ) { |
|
|
return `/marketplace/thank-you/${ siteSlug }?plugins=${ pluginParameter }`; |
|
|
} |
|
|
|
|
|
|
|
|
return `/marketplace/plugin/${ pluginParameter }/install/${ siteSlug }`; |
|
|
} |
|
|
|
|
|
function getEditorDestination( dependencies ) { |
|
|
return `/page/${ dependencies.siteSlug }/home`; |
|
|
} |
|
|
|
|
|
function getDestinationFromIntent( dependencies ) { |
|
|
const { intent, storeType, startingPoint, siteSlug } = dependencies; |
|
|
|
|
|
if ( intent === 'write' && startingPoint !== 'skip-to-my-home' ) { |
|
|
if ( startingPoint !== 'write' ) { |
|
|
window.sessionStorage.setItem( 'wpcom_signup_complete_show_draft_post_modal', '1' ); |
|
|
} |
|
|
|
|
|
return `/post/${ siteSlug }`; |
|
|
} |
|
|
|
|
|
if ( intent === 'sell' && storeType === 'power' ) { |
|
|
return addQueryArgs( |
|
|
{ |
|
|
back_to: `/start/setup-site/store-features?siteSlug=${ siteSlug }`, |
|
|
siteSlug: siteSlug, |
|
|
}, |
|
|
`/start/woocommerce-install` |
|
|
); |
|
|
} |
|
|
|
|
|
return getChecklistThemeDestination( dependencies ); |
|
|
} |
|
|
|
|
|
function getDIFMSignupDestination( { siteId } ) { |
|
|
return addQueryArgs( { siteId }, '/start/site-content-collection' ); |
|
|
} |
|
|
|
|
|
function getDIFMSiteContentCollectionDestination( { siteSlug } ) { |
|
|
return `/home/${ siteSlug }`; |
|
|
} |
|
|
|
|
|
function getHostingFlowDestination( { stepperHostingFlow } ) { |
|
|
return `/setup/${ stepperHostingFlow }`; |
|
|
} |
|
|
|
|
|
const flows = generateFlows( { |
|
|
getRedirectDestination, |
|
|
getSignupDestination, |
|
|
getLaunchDestination, |
|
|
getDomainSignupFlowDestination, |
|
|
getEmailSignupFlowDestination, |
|
|
getWithThemeDestination, |
|
|
getWithPluginDestination, |
|
|
getEditorDestination, |
|
|
getDestinationFromIntent, |
|
|
getDIFMSignupDestination, |
|
|
getDIFMSiteContentCollectionDestination, |
|
|
getHostingFlowDestination, |
|
|
} ); |
|
|
|
|
|
function removeUserStepFromFlow( flow ) { |
|
|
if ( ! flow ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
return { |
|
|
...flow, |
|
|
steps: flow.steps.filter( ( stepName ) => ! stepConfig[ stepName ].providesToken ), |
|
|
}; |
|
|
} |
|
|
|
|
|
function filterDestination( destination, dependencies, flowName, localeSlug ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ! dependencies.siteSlug ) { |
|
|
return destination; |
|
|
} |
|
|
|
|
|
if ( dependenciesContainCartItem( dependencies ) ) { |
|
|
return getCheckoutUrl( dependencies, localeSlug, flowName, destination ); |
|
|
} |
|
|
|
|
|
return destination; |
|
|
} |
|
|
|
|
|
function getDefaultFlowName() { |
|
|
return 'onboarding'; |
|
|
} |
|
|
|
|
|
const Flows = { |
|
|
filterDestination, |
|
|
|
|
|
defaultFlowName: getDefaultFlowName(), |
|
|
excludedSteps: [], |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getFlow( flowName, isUserLoggedIn ) { |
|
|
let flow = Flows.getFlows()[ flowName ]; |
|
|
|
|
|
|
|
|
if ( ! flow ) { |
|
|
return flow; |
|
|
} |
|
|
|
|
|
if ( isUserLoggedIn ) { |
|
|
const isUserStepOnly = flow.steps.length === 1 && stepConfig[ flow.steps[ 0 ] ].providesToken; |
|
|
|
|
|
|
|
|
if ( ! isUserStepOnly ) { |
|
|
flow = removeUserStepFromFlow( flow ); |
|
|
} |
|
|
} |
|
|
|
|
|
return Flows.filterExcludedSteps( flow ); |
|
|
}, |
|
|
|
|
|
getNextStepNameInFlow( flowName, currentStepName = '' ) { |
|
|
const flow = Flows.getFlows()[ flowName ]; |
|
|
|
|
|
if ( ! flow ) { |
|
|
return false; |
|
|
} |
|
|
const flowSteps = flow.steps; |
|
|
const currentStepIndex = flowSteps.indexOf( currentStepName ); |
|
|
const nextIndex = currentStepIndex + 1; |
|
|
const nextStepName = get( flowSteps, nextIndex ); |
|
|
|
|
|
return nextStepName; |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
excludeStep( step ) { |
|
|
step && Flows.excludedSteps.indexOf( step ) === -1 && Flows.excludedSteps.push( step ); |
|
|
}, |
|
|
|
|
|
excludeSteps( steps ) { |
|
|
steps.forEach( ( step ) => Flows.excludeStep( step ) ); |
|
|
}, |
|
|
|
|
|
filterExcludedSteps( flow ) { |
|
|
if ( ! flow ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
return { |
|
|
...flow, |
|
|
steps: reject( flow.steps, ( stepName ) => includes( Flows.excludedSteps, stepName ) ), |
|
|
}; |
|
|
}, |
|
|
|
|
|
resetExcludedSteps() { |
|
|
Flows.excludedSteps = []; |
|
|
}, |
|
|
|
|
|
resetExcludedStep( stepName ) { |
|
|
const index = Flows.excludedSteps.indexOf( stepName ); |
|
|
|
|
|
if ( index > -1 ) { |
|
|
Flows.excludedSteps.splice( index, 1 ); |
|
|
} |
|
|
}, |
|
|
|
|
|
getFlows() { |
|
|
return flows; |
|
|
}, |
|
|
}; |
|
|
|
|
|
export default Flows; |
|
|
|