Skip to content

Commit

Permalink
Create pending sticker when loading the step
Browse files Browse the repository at this point in the history
  • Loading branch information
renatho committed Oct 21, 2024
1 parent 432f963 commit 5b7c3ec
Showing 1 changed file with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StepContainer } from '@automattic/onboarding';
import { useTranslate } from 'i18n-calypso';
import { FC, useMemo } from 'react';
import { FC, useMemo, useEffect } from 'react';
import DocumentHead from 'calypso/components/data/document-head';
import FormattedHeader from 'calypso/components/formatted-header';
import { useUpdateMigrationStatus } from 'calypso/data/site-migration/use-update-migration-status';
Expand All @@ -13,10 +13,48 @@ import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { usePresalesChat } from 'calypso/lib/presales-chat';
import useHostingProviderName from 'calypso/site-profiler/hooks/use-hosting-provider-name';
import FlowCard from '../components/flow-card';
import type { StepProps } from '../../types';
import type { NavigationControls, StepProps } from '../../types';

import './style.scss';

interface PendingMigrationStatusProps {
onSubmit?: Pick< NavigationControls, 'submit' >[ 'submit' ];
}

const usePendingMigrationStatus = ( { onSubmit }: PendingMigrationStatusProps ) => {
const site = useSite();
const siteId = site?.ID;

const canInstallPlugins = site?.plan?.features?.active.find(
( feature ) => feature === 'install-plugins'
)
? true
: false;

const { updateMigrationStatus } = useUpdateMigrationStatus();

// Register pending migration status when loading the step.
useEffect( () => {
if ( siteId ) {
updateMigrationStatus( siteId, 'migration-pending' );
}
}, [ siteId, updateMigrationStatus ] );

const onOptionClick = ( how: string ) => {
const destination = canInstallPlugins ? 'migrate' : 'upgrade';
if ( siteId ) {
const parsedHow = how === HOW_TO_MIGRATE_OPTIONS.DO_IT_MYSELF ? 'diy' : how;
updateMigrationStatus( siteId, `migration-pending-${ parsedHow }` );
}

if ( onSubmit ) {
return onSubmit( { how, destination } );
}
};

return { onOptionClick };
};

interface Props extends StepProps {
headerText?: string;
subHeaderText?: string;
Expand All @@ -26,7 +64,6 @@ const SiteMigrationHowToMigrate: FC< Props > = ( props ) => {
const { navigation, headerText } = props;

const translate = useTranslate();
const site = useSite();
const importSiteQueryParam = useQuery().get( 'from' ) || '';
usePresalesChat( 'wpcom' );

Expand Down Expand Up @@ -64,29 +101,14 @@ const SiteMigrationHowToMigrate: FC< Props > = ( props ) => {
urlData
);

const { onOptionClick } = usePendingMigrationStatus( { onSubmit: navigation.submit } );

const hostingProviderSlug = hostingProviderData?.hosting_provider?.slug;
const shouldDisplayHostIdentificationMessage =
hostingProviderSlug &&
hostingProviderSlug !== 'unknown' &&
hostingProviderSlug !== 'automattic';

const canInstallPlugins = site?.plan?.features?.active.find(
( feature ) => feature === 'install-plugins'
)
? true
: false;

const { updateMigrationStatus } = useUpdateMigrationStatus();

const handleClick = ( how: string ) => {
const destination = canInstallPlugins ? 'migrate' : 'upgrade';
if ( site?.ID ) {
const parsedHow = how === HOW_TO_MIGRATE_OPTIONS.DO_IT_MYSELF ? 'diy' : how;
updateMigrationStatus( site.ID, `migration-pending-${ parsedHow }` );
}
return navigation.submit?.( { how, destination } );
};

const stepContent = (
<>
<div className="how-to-migrate__list">
Expand All @@ -95,7 +117,7 @@ const SiteMigrationHowToMigrate: FC< Props > = ( props ) => {
key={ i }
title={ option.label }
text={ option.description }
onClick={ () => handleClick( option.value ) }
onClick={ () => onOptionClick( option.value ) }
/>
) ) }
</div>
Expand Down

0 comments on commit 5b7c3ec

Please sign in to comment.