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

Removing calls to front end isSubdomain where possible #99121

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
48 changes: 30 additions & 18 deletions client/components/domains/connect-domain-step/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import DomainTransferRecommendation from 'calypso/components/domains/domain-tran
import TwoColumnsLayout from 'calypso/components/domains/layout/two-columns-layout';
import FormattedHeader from 'calypso/components/formatted-header';
import BodySectionCssClass from 'calypso/layout/body-section-css-class';
import { isSubdomain } from 'calypso/lib/domains';
import wpcom from 'calypso/lib/wp';
import DomainHeader from 'calypso/my-sites/domains/domain-management/components/domain-header';
import {
Expand Down Expand Up @@ -47,21 +46,26 @@ function ConnectDomainStep( {
currentRoute,
} ) {
const { __ } = useI18n();
const stepsDefinition = isSubdomain( domain )
? connectASubdomainStepsDefinition
: connectADomainStepsDefinition;
const firstStep = isSubdomain( domain )
? stepSlug.SUBDOMAIN_SUGGESTED_START
: stepSlug.SUGGESTED_START;
const [ pageSlug, setPageSlug ] = useState( firstStep );

const [ verificationStatus, setVerificationStatus ] = useState( {} );
const [ verificationInProgress, setVerificationInProgress ] = useState( false );
const [ domainSetupInfo, setDomainSetupInfo ] = useState( defaultDomainSetupInfo );
const [ domainSetupInfoError, setDomainSetupInfoError ] = useState( {} );
const [ loadingDomainSetupInfo, setLoadingDomainSetupInfo ] = useState( false );

const baseClassName = 'connect-domain-step';
if ( stepsDefinition[ pageSlug ] === undefined ) {

// Get the steps definition based on domain type once API data is loaded
const stepsDefinition = domainSetupInfo?.data?.is_subdomain
? connectASubdomainStepsDefinition
: connectADomainStepsDefinition;

// Get the first step based on domain type once API data is loaded
const firstStep = domainSetupInfo?.data?.is_subdomain
? stepSlug.SUBDOMAIN_SUGGESTED_START
: stepSlug.SUGGESTED_START;
const [ pageSlug, setPageSlug ] = useState( firstStep );
if ( pageSlug && stepsDefinition[ pageSlug ] === undefined ) {
// eslint-disable-next-line no-console
console.error(
'Tried to set invalid pageSlug in ConnectDomainStep',
Expand All @@ -70,12 +74,14 @@ function ConnectDomainStep( {
domain
);
}
const currentStep = stepsDefinition[ pageSlug ] || stepsDefinition[ firstStep ];
const isStepStart = stepType.START === currentStep.step;

const currentStep =
( pageSlug && stepsDefinition[ pageSlug ] ) || ( firstStep && stepsDefinition[ firstStep ] );
const isStepStart = currentStep && stepType.START === currentStep.step;
const mode = currentStep.mode;
const step = currentStep.step;
const prevPageSlug = currentStep.prev;
const isTwoColumnLayout = ! currentStep.singleColumnLayout;
const isTwoColumnLayout = currentStep ? ! currentStep.singleColumnLayout : true;

const statusRef = useRef( {} );

Expand All @@ -97,13 +103,13 @@ function ConnectDomainStep( {
);

const resolveMappingSetupStep = useCallback(
( connectionMode, supportsDomainConnect, domainName ) => {
( connectionMode, supportsDomainConnect, domainName, isSubdomain ) => {
if ( initialStep ) {
return initialStep;
}
// If connectionMode is present we'll send you to the last step of the relevant flow
if ( connectionMode ) {
if ( isSubdomain( domainName ) ) {
if ( isSubdomain ) {
return connectionMode === modeType.ADVANCED
? stepSlug.SUBDOMAIN_ADVANCED_UPDATE
: stepSlug.SUBDOMAIN_SUGGESTED_UPDATE;
Expand All @@ -126,15 +132,20 @@ function ConnectDomainStep( {

const verifyConnection = useCallback(
( setStepAfterVerify = true ) => {
if ( ! domainSetupInfo?.data ) {
return;
}

setVerificationStatus( {} );
setVerificationInProgress( true );

const isSubdomain = domainSetupInfo.data.is_subdomain;
let connectedSlug =
modeType.SUGGESTED === mode ? stepSlug.SUGGESTED_CONNECTED : stepSlug.ADVANCED_CONNECTED;
let verifyingSlug =
modeType.SUGGESTED === mode ? stepSlug.SUGGESTED_VERIFYING : stepSlug.ADVANCED_VERIFYING;

if ( isSubdomain( domain ) ) {
if ( isSubdomain ) {
connectedSlug =
modeType.SUGGESTED === mode
? stepSlug.SUBDOMAIN_SUGGESTED_CONNECTED
Expand Down Expand Up @@ -166,7 +177,7 @@ function ConnectDomainStep( {
} )
.finally( () => setVerificationInProgress( false ) );
},
[ domain, mode ]
[ domain, mode, domainSetupInfo ]
);

useEffect( () => {
Expand All @@ -188,7 +199,8 @@ function ConnectDomainStep( {
const resolvedPageSlug = resolveMappingSetupStep(
data?.connection_mode,
!! data?.domain_connect_apply_wpcom_hosting,
domain
domain,
data?.is_subdomain
);
setPageSlug( resolvedPageSlug );
recordMappingSetupTracksEvent(
Expand Down Expand Up @@ -365,7 +377,7 @@ function ConnectDomainStep( {
supportsDomainConnect={ !! domainSetupInfo?.data?.domain_connect_apply_wpcom_hosting }
currentMode={ mode }
currentStep={ step }
isSubdomain={ isSubdomain( domain ) }
isSubdomain={ !! domainSetupInfo?.data?.is_subdomain }
setPage={ setPageSlug }
/>
</>
Expand Down