File size: 18,513 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | import config from '@automattic/calypso-config';
import { getDIFMTieredPriceDetails, WPCOM_DIFM_LITE } from '@automattic/calypso-products';
import { RazorpayHookProvider } from '@automattic/calypso-razorpay';
import { StripeHookProvider } from '@automattic/calypso-stripe';
import { Button } from '@automattic/components';
import { BraveTick } from '@automattic/components/src/icons';
import { formatCurrency } from '@automattic/number-formatters';
import { createRequestCartProduct } from '@automattic/shopping-cart';
import { isMobile } from '@automattic/viewport';
import styled from '@emotion/styled';
import { useTranslate } from 'i18n-calypso';
import { useEffect, useMemo, useRef, useState } from 'react';
import InfoPopover from 'calypso/components/info-popover';
import { triggerGuidesForStep } from 'calypso/lib/guides/trigger-guides-for-step';
import { logToLogstash } from 'calypso/lib/logstash';
import { getRazorpayConfiguration, getStripeConfiguration } from 'calypso/lib/store-transactions';
import CalypsoShoppingCartProvider from 'calypso/my-sites/checkout/calypso-shopping-cart-provider';
import PurchaseModal from 'calypso/my-sites/checkout/purchase-modal';
import { useIsEligibleForOneClickCheckout } from 'calypso/my-sites/checkout/purchase-modal/use-is-eligible-for-one-click-checkout';
import { BrowserView } from 'calypso/signup/difm/components/BrowserView';
import {
HOME_PAGE,
BLOG_PAGE,
CONTACT_PAGE,
ABOUT_PAGE,
PHOTO_GALLERY_PAGE,
VIDEO_GALLERY_PAGE,
PORTFOLIO_PAGE,
FAQ_PAGE,
SERVICES_PAGE,
TESTIMONIALS_PAGE,
PRICING_PAGE,
TEAM_PAGE,
SHOP_PAGE,
CUSTOM_PAGE,
EVENTS_PAGE,
CAREERS_PAGE,
DONATE_PAGE,
NEWSLETTER_PAGE,
CASE_STUDIES_PAGE,
} from 'calypso/signup/difm/constants';
import {
BBE_ONBOARDING_PAGE_PICKER_STEP,
BBE_STORE_ONBOARDING_PAGE_PICKER_STEP,
useTranslatedPageDescriptions,
useTranslatedPageTitles,
} from 'calypso/signup/difm/translation-hooks';
import StepWrapper from 'calypso/signup/step-wrapper';
import useLongFetchingDetection from 'calypso/site-profiler/hooks/use-long-fetching-detection';
import { useDispatch, useSelector } from 'calypso/state';
import { getCurrentUserCurrencyCode } from 'calypso/state/currency-code/selectors';
import { buildDIFMCartExtrasObject } from 'calypso/state/difm/assemblers';
import { getProductBySlug } from 'calypso/state/products-list/selectors';
import { getSignupDependencyStore } from 'calypso/state/signup/dependency-store/selectors';
import { saveSignupStep, submitSignupStep } from 'calypso/state/signup/progress/actions';
import { getSiteId, getSitePlan } from 'calypso/state/sites/selectors';
import ShoppingCartForDIFM from './shopping-cart-for-difm';
import useCartForDIFM from './use-cart-for-difm';
import type { PageId } from 'calypso/signup/difm/constants';
import type { BBETranslationContext } from 'calypso/signup/difm/translation-hooks';
import type { Dependencies } from 'calypso/signup/types';
import type { SiteId, SiteSlug } from 'calypso/types';
import './style.scss';
const PageGrid = styled.div`
display: grid;
grid-template-columns: repeat( 2, minmax( 0, 1fr ) );
row-gap: 20px;
column-gap: 15px;
margin: 0 0 30px;
@media ( min-width: 600px ) and ( max-width: 750px ) {
grid-template-columns: 1fr;
column-gap: 15px;
row-gap: 25px;
}
@media ( min-width: 1200px ) {
grid-template-columns: 1fr 1fr 1fr;
row-gap: 40px;
column-gap: 35px;
}
@media ( max-width: 600px ) {
margin: 0 0 200px;
}
`;
const GridCellContainer = styled.div< { isClickDisabled: boolean; isSelected: boolean } >`
cursor: default;
opacity: ${ ( { isSelected, isClickDisabled } ) =>
! isSelected && isClickDisabled ? '0.4' : '1' };
border-radius: 4px;
position: relative;
width: 100%;
position: relative;
display: flex;
flex-direction: column;
align-items: center;
font-weight: 500;
`;
const CellLabelContainer = styled.div`
margin: 14px 0;
text-align: left;
display: flex;
justify-content: center;
align-items: center;
font-size: 12px;
gap: 8px;
width: 100%;
@media ( min-width: 960px ) {
font-size: 14px;
justify-content: left;
}
.info-popover {
margin-inline-start: auto;
}
`;
const PageCellBadge = styled.div`
background: var( --studio-green-5 );
border-radius: 4px;
text-align: center;
font-size: 12px;
padding: 0 6px;
line-height: 20px;
font-weight: 500;
color: var( --studio-green-80 );
height: 20px;
width: auto;
`;
interface PageCellType {
pageId: PageId;
selectedPages: string[];
onClick: ( pageId: string ) => void;
popular?: boolean;
required?: boolean;
context: BBETranslationContext;
}
function PageCell( { pageId, popular, required, selectedPages, context, onClick }: PageCellType ) {
const translate = useTranslate();
const selectedIndex = selectedPages.indexOf( pageId );
const isSelected = Boolean( selectedIndex > -1 );
const title = useTranslatedPageTitles()[ pageId ];
const description = useTranslatedPageDescriptions( pageId, context );
return (
<GridCellContainer isSelected={ isSelected } isClickDisabled={ false }>
<BrowserView
onClick={ () => onClick( pageId ) }
pageId={ pageId }
isClickDisabled={ false }
isSelected={ isSelected }
selectedIndex={ selectedIndex >= 0 ? selectedIndex : -1 }
/>
<CellLabelContainer>
<div>{ title }</div>
{ popular ? <PageCellBadge>{ translate( 'Popular' ) }</PageCellBadge> : null }
{ required ? <PageCellBadge>{ translate( 'Required' ) }</PageCellBadge> : null }
<InfoPopover showOnHover position={ isMobile() ? 'left' : 'top left' }>
{ description }
</InfoPopover>
</CellLabelContainer>
</GridCellContainer>
);
}
function PageSelector( {
selectedPages,
setSelectedPages,
isStoreFlow,
}: {
selectedPages: string[];
setSelectedPages: ( pages: string[] ) => void;
isStoreFlow: boolean;
} ) {
const onPageClick = ( pageId: string ) => {
const isPageSelected = selectedPages.includes( pageId );
// The home page cannot be touched and is always included
if ( ! [ HOME_PAGE, SHOP_PAGE ].includes( pageId ) ) {
if ( isPageSelected ) {
// Unselect selected page
setSelectedPages( selectedPages.filter( ( page ) => page !== pageId ) );
} else {
setSelectedPages( [ ...selectedPages, pageId ] );
}
}
};
const context = isStoreFlow
? BBE_STORE_ONBOARDING_PAGE_PICKER_STEP
: BBE_ONBOARDING_PAGE_PICKER_STEP;
return (
<PageGrid>
<PageCell
context={ context }
required
pageId={ HOME_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
{ isStoreFlow && (
<PageCell
context={ context }
required
pageId={ SHOP_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
) }
<PageCell
context={ context }
popular
pageId={ ABOUT_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
popular
pageId={ CONTACT_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
popular
pageId={ BLOG_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
pageId={ PHOTO_GALLERY_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
{ isStoreFlow ? (
<PageCell
context={ context }
popular
pageId={ FAQ_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
) : (
<PageCell
context={ context }
popular
pageId={ SERVICES_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
) }
<PageCell
context={ context }
pageId={ VIDEO_GALLERY_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
{ ! isStoreFlow && (
<PageCell
context={ context }
popular
pageId={ PRICING_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
) }
<PageCell
context={ context }
pageId={ PORTFOLIO_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
{ isStoreFlow ? (
<PageCell
context={ context }
pageId={ SERVICES_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
) : (
<PageCell
context={ context }
popular
pageId={ FAQ_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
) }
<PageCell
context={ context }
popular={ isStoreFlow }
pageId={ TESTIMONIALS_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
pageId={ TEAM_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
pageId={ CAREERS_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
pageId={ EVENTS_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
pageId={ DONATE_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
pageId={ NEWSLETTER_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
context={ context }
pageId={ CASE_STUDIES_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
<PageCell
popular
context={ context }
pageId={ CUSTOM_PAGE }
selectedPages={ selectedPages }
onClick={ onPageClick }
/>
</PageGrid>
);
}
interface StepProps {
stepSectionName: string | null;
stepName: string;
flowName: string;
goToStep: () => void;
goToNextStep: () => void;
signupDependencies: Dependencies;
}
const StyledButton = styled( Button )`
&.button.is-primary {
min-width: 230px;
padding: 10px 27px 10px 28px;
@media ( max-width: 600px ) {
min-width: auto;
}
}
`;
const Placeholder = styled.span`
animation: pulse-light 2s ease-in-out infinite;
background-color: var( --color-neutral-10 );
color: transparent;
min-height: 16px;
display: inline-block;
min-width: 32px;
`;
const RefundText = styled.div`
display: flex;
align-items: center;
width: 100%;
margin-top: 24px;
@media ( max-width: 600px ) {
font-size: smaller;
}
`;
const StyledBraveTickIcon = styled( BraveTick )`
margin-inline-end: 6px;
path {
fill: var( --color-accent );
}
`;
function OneClickPurchaseModal( {
onClose,
siteSlug,
siteId,
selectedPages,
isStoreFlow,
flowName,
coupon,
}: {
onClose: () => void;
siteSlug: SiteSlug;
siteId: SiteId;
selectedPages: string[];
isStoreFlow: boolean;
flowName: string;
coupon?: string;
} ) {
const translate = useTranslate();
const signupDependencies = useSelector( getSignupDependencyStore );
const product = useMemo( () => {
return createRequestCartProduct( {
product_slug: WPCOM_DIFM_LITE,
extra: buildDIFMCartExtrasObject(
{
...signupDependencies,
selectedPageTitles: selectedPages,
isStoreFlow,
},
siteId,
`page-picker-one-click-modal-flow-${ flowName }`
),
quantity: selectedPages.length,
} );
}, [ flowName, isStoreFlow, selectedPages, signupDependencies, siteId ] );
return (
<CalypsoShoppingCartProvider>
<StripeHookProvider
fetchStripeConfiguration={ getStripeConfiguration }
locale={ translate.localeSlug }
>
<RazorpayHookProvider fetchRazorpayConfiguration={ getRazorpayConfiguration }>
<PurchaseModal
productToAdd={ product }
coupon={ coupon }
onClose={ onClose }
showFeatureList={ false }
siteSlug={ siteSlug }
/>
</RazorpayHookProvider>
</StripeHookProvider>
</CalypsoShoppingCartProvider>
);
}
function DIFMPagePicker( props: StepProps ) {
const {
stepName,
goToNextStep,
signupDependencies: {
siteId: siteIdFromDependencies,
siteSlug,
newOrExistingSiteChoice,
coupon,
},
flowName,
} = props;
const translate = useTranslate();
const dispatch = useDispatch();
const isStoreFlow = 'do-it-for-me-store' === flowName;
const [ isCheckoutPressed, setIsCheckoutPressed ] = useState( false );
const [ showPurchaseModal, setShowPurchaseModal ] = useState( false );
const [ selectedPages, setSelectedPages ] = useState< string[] >(
isStoreFlow
? [ HOME_PAGE, SHOP_PAGE, ABOUT_PAGE, CONTACT_PAGE ]
: [ HOME_PAGE, ABOUT_PAGE, CONTACT_PAGE, PHOTO_GALLERY_PAGE, SERVICES_PAGE ]
);
const siteId = useSelector( ( state ) => getSiteId( state, siteSlug ) ) || siteIdFromDependencies;
const currentPlan = useSelector( ( state ) => ( siteId ? getSitePlan( state, siteId ) : null ) );
const currencyCode = useSelector( getCurrentUserCurrencyCode ) || '';
const isExistingSite = newOrExistingSiteChoice === 'existing-site' || siteSlug;
const { isProductsLoading } = useCartForDIFM( {
selectedPages,
isStoreFlow,
currentPlanSlug: currentPlan?.product_slug,
} );
const difmLiteProduct = useSelector( ( state ) => getProductBySlug( state, WPCOM_DIFM_LITE ) );
let difmTieredPriceDetails = null;
if ( difmLiteProduct ) {
difmTieredPriceDetails = getDIFMTieredPriceDetails( difmLiteProduct, selectedPages.length );
}
useEffect( () => {
dispatch( saveSignupStep( { stepName } ) );
triggerGuidesForStep( flowName, stepName );
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );
const {
isLoading: isLoadingIsEligibleForOneClickCheckout,
result: isEligibleForOneClickCheckout,
} = useIsEligibleForOneClickCheckout();
const submitPickedPages = async () => {
if ( ! isCheckoutPressed ) {
setIsCheckoutPressed( true );
if ( true === isEligibleForOneClickCheckout && isExistingSite ) {
setShowPurchaseModal( true );
return;
}
dispatch(
submitSignupStep(
{ stepName },
{
selectedPageTitles: selectedPages,
newOrExistingSiteChoice: isExistingSite ? 'existing-site' : 'new-site', // Ensure the value of newOrExistingSiteChoice is set if it's not already
}
)
);
goToNextStep();
}
};
const handleModalOnClose = () => {
setShowPurchaseModal( false );
setIsCheckoutPressed( false );
setSelectedPages( ( selectedPages ) => selectedPages.slice() );
};
const isCheckoutButtonBusy =
isProductsLoading ||
( isExistingSite && isLoadingIsEligibleForOneClickCheckout ) ||
isCheckoutPressed;
const isBusyForWhile = useLongFetchingDetection( 'difm-page-picker', isCheckoutButtonBusy, 5000 );
const hasLogged = useRef< boolean >( false );
useEffect( () => {
if ( isBusyForWhile && ! hasLogged.current ) {
hasLogged.current = true;
const message =
'Page Picker Infinite Loading state:' +
` isProductsLoading: ${ isProductsLoading }` +
` isLoadingIsEligibleForOneClickCheckout: ${ isLoadingIsEligibleForOneClickCheckout }`;
logToLogstash( {
feature: 'calypso_client',
message,
severity: config( 'env_id' ) === 'production' ? 'error' : 'debug',
} );
}
}, [ isBusyForWhile, isLoadingIsEligibleForOneClickCheckout, isProductsLoading ] );
const headerText = translate( 'Add pages to your {{wbr}}{{/wbr}}website', {
components: { wbr: <wbr /> },
} );
const subHeaderText = isStoreFlow
? translate(
'Click on the thumbnails to select or deselect pages. Your site build includes up to %(freePageCount)s pages, and you can add more for {{PriceWrapper}}%(extraPagePrice)s{{/PriceWrapper}} each. After checkout, you will have the opportunity to submit your content.' +
'{{br}}{{/br}}{{br}}{{/br}}A cart and checkout are also included with your site.{{br}}{{/br}}You can add products later with the WordPress editor.',
{
components: {
br: <br />,
PriceWrapper: difmTieredPriceDetails?.perExtraPagePrice ? <span /> : <Placeholder />,
},
args: {
freePageCount: difmTieredPriceDetails?.numberOfIncludedPages as number,
extraPagePrice: formatCurrency(
difmTieredPriceDetails?.perExtraPagePrice ?? 0,
currencyCode ?? '',
{
stripZeros: true,
isSmallestUnit: true,
}
),
},
}
)
: translate(
'Click on the thumbnails to select or deselect pages. Your site build includes up to %(freePageCount)s pages, and you can add more for {{PriceWrapper}}%(extraPagePrice)s{{/PriceWrapper}} each. After checkout, you will have the opportunity to submit your content.',
{
components: {
br: <br />,
PriceWrapper: difmTieredPriceDetails?.perExtraPagePrice ? <span /> : <Placeholder />,
},
args: {
freePageCount: difmTieredPriceDetails?.numberOfIncludedPages as number,
extraPagePrice: formatCurrency(
difmTieredPriceDetails?.perExtraPagePrice ?? 0,
currencyCode ?? '',
{
stripZeros: true,
isSmallestUnit: true,
}
),
},
}
);
const renderRefundText = () => {
return (
<RefundText className="refund-text">
<StyledBraveTickIcon />
{ translate( '%(days)d-day money-back guarantee', {
args: { days: 14 },
} ) }
</RefundText>
);
};
return (
<StepWrapper
headerText={ headerText }
fallbackHeaderText={ headerText }
subHeaderText={ subHeaderText }
fallbackSubHeaderText={ subHeaderText }
stepContent={
<>
{ showPurchaseModal && (
<OneClickPurchaseModal
onClose={ handleModalOnClose }
siteSlug={ siteSlug }
siteId={ siteId }
selectedPages={ selectedPages }
isStoreFlow={ isStoreFlow }
flowName={ flowName }
coupon={ coupon }
/>
) }
<PageSelector
isStoreFlow={ isStoreFlow }
selectedPages={ selectedPages }
setSelectedPages={ setSelectedPages }
/>
</>
}
hideSkip
align="left"
isHorizontalLayout
isWideLayout={ false }
headerButton={
<>
<StyledButton
disabled={ isProductsLoading }
busy={ isCheckoutButtonBusy }
primary
onClick={ submitPickedPages }
>
{ translate( 'Go to Checkout' ) }
</StyledButton>
{ renderRefundText() }
</>
}
headerContent={
<ShoppingCartForDIFM
selectedPages={ selectedPages }
isStoreFlow={ isStoreFlow }
currentPlanSlug={ currentPlan?.product_slug }
>
{ renderRefundText() }
</ShoppingCartForDIFM>
}
{ ...props }
/>
);
}
export default function ShoppingCartWrappedPagePicker( props: StepProps ) {
return (
<CalypsoShoppingCartProvider>
<DIFMPagePicker { ...props } />
</CalypsoShoppingCartProvider>
);
}
|