Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 44x 44x 2x 2x 44x 44x 44x 44x 44x 2x 2x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 1x 43x 2x 202x 202x 202x 13x 8x 5x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 202x 3x 199x 796x 792x 2x 792x 199x 195x 199x 199x 2x 199x 199x 199x 199x 2x 195x 195x 195x 195x 195x 195x 195x 195x 195x 28x 2x 199x 199x 2x 199x 199x 199x 199x 2x 199x 199x 199x 2x 107x 107x 107x 107x 107x 23x 84x 46x 38x 84x 44x 44x 73x 73x 73x 73x 73x 73x 61x 73x 44x 3x 3x 3x 3x 3x 3x 3x 44x 44x 56x 56x 56x 56x 56x 56x 56x 56x 44x 44x 1x 1x 44x 5x 5x 44x 9x 9x 9x 9x 44x 29x 44x 68x 44x 2x 2x 2x 2x 2x 1x 1x 1x 1x 44x 2x 2x 1x 1x 44x 1x 44x 44x 3x 3x 3x 3x 3x 3x 2x 2x | import { Address, Cart, CartChangedError, CheckoutParams, CheckoutSelectors, Consignment, EmbeddedCheckoutMessenger, EmbeddedCheckoutMessengerOptions, FlashMessage, Promotion, RequestOptions, StepTracker } from '@bigcommerce/checkout-sdk'; import classNames from 'classnames'; import { find, findIndex } from 'lodash'; import React, { lazy, Component, ReactNode } from 'react'; import { StaticBillingAddress } from '../billing'; import { EmptyCartMessage } from '../cart'; import { isCustomError, CustomError, ErrorLogger, ErrorModal } from '../common/error'; import { retry } from '../common/utility'; import { CustomerInfo, CustomerSignOutEvent, CustomerViewType } from '../customer'; import { isEmbedded, EmbeddedCheckoutStylesheet } from '../embeddedCheckout'; import { withLanguage, TranslatedString, WithLanguageProps } from '../locale'; import { PromotionBannerList } from '../promotion'; import { hasSelectedShippingOptions, isUsingMultiShipping, StaticConsignment } from '../shipping'; import { ShippingOptionExpiredError } from '../shipping/shippingOption'; import { LazyContainer, LoadingNotification, LoadingOverlay } from '../ui/loading'; import { MobileView } from '../ui/responsive'; import mapToCheckoutProps from './mapToCheckoutProps'; import navigateToOrderConfirmation from './navigateToOrderConfirmation'; import withCheckout from './withCheckout'; import CheckoutStep from './CheckoutStep'; import CheckoutStepStatus from './CheckoutStepStatus'; import CheckoutStepType from './CheckoutStepType'; import CheckoutSupport from './CheckoutSupport'; const Billing = lazy(() => retry(() => import( /* webpackChunkName: "billing" */ '../billing/Billing' ))); const CartSummary = lazy(() => retry(() => import( /* webpackChunkName: "cart-summary" */ '../cart/CartSummary' ))); const CartSummaryDrawer = lazy(() => retry(() => import( /* webpackChunkName: "cart-summary-drawer" */ '../cart/CartSummaryDrawer' ))); const Customer = lazy(() => retry(() => import( /* webpackChunkName: "customer" */ '../customer/Customer' ))); const Payment = lazy(() => retry(() => import( /* webpackChunkName: "payment" */ '../payment/Payment' ))); const Shipping = lazy(() => retry(() => import( /* webpackChunkName: "shipping" */ '../shipping/Shipping' ))); export interface CheckoutProps { checkoutId: string; containerId: string; embeddedStylesheet: EmbeddedCheckoutStylesheet; embeddedSupport: CheckoutSupport; errorLogger: ErrorLogger; createEmbeddedMessenger(options: EmbeddedCheckoutMessengerOptions): EmbeddedCheckoutMessenger; createStepTracker(): StepTracker; } export interface CheckoutState { activeStepType?: CheckoutStepType; isBillingSameAsShipping: boolean; customerViewType?: CustomerViewType; defaultStepType?: CheckoutStepType; error?: Error; flashMessages?: FlashMessage[]; isMultiShippingMode: boolean; isCartEmpty: boolean; isRedirecting: boolean; hasSelectedShippingOptions: boolean; } export interface WithCheckoutProps { billingAddress?: Address; cart?: Cart; consignments?: Consignment[]; error?: Error; hasCartChanged: boolean; flashMessages?: FlashMessage[]; isGuestEnabled: boolean; isLoadingCheckout: boolean; isPending: boolean; loginUrl: string; createAccountUrl: string; canCreateAccountInCheckout: boolean; promotions?: Promotion[]; steps: CheckoutStepStatus[]; clearError(error?: Error): void; loadCheckout(id: string, options?: RequestOptions<CheckoutParams>): Promise<CheckoutSelectors>; subscribeToConsignments(subscriber: (state: CheckoutSelectors) => void): () => void; } class Checkout extends Component<CheckoutProps & WithCheckoutProps & WithLanguageProps, CheckoutState> { stepTracker: StepTracker | undefined; state: CheckoutState = { isBillingSameAsShipping: true, isCartEmpty: false, isRedirecting: false, isMultiShippingMode: false, hasSelectedShippingOptions: false, }; private embeddedMessenger?: EmbeddedCheckoutMessenger; private unsubscribeFromConsignments?: () => void; componentWillUnmount(): void { if (this.unsubscribeFromConsignments) { this.unsubscribeFromConsignments(); this.unsubscribeFromConsignments = undefined; } } async componentDidMount(): Promise<void> { const { checkoutId, containerId, createStepTracker, createEmbeddedMessenger, embeddedStylesheet, loadCheckout, subscribeToConsignments, } = this.props; try { const { data } = await loadCheckout(checkoutId, { params: { include: [ 'cart.lineItems.physicalItems.categoryNames', 'cart.lineItems.digitalItems.categoryNames', ] as any, // FIXME: Currently the enum is not exported so it can't be used here. }, }); const { links: { siteLink = '' } = {} } = data.getConfig() || {}; const errorFlashMessages = data.getFlashMessages('error') || []; if (errorFlashMessages.length) { const { language } = this.props; this.setState({ error: new CustomError({ title: errorFlashMessages[0].title || language.translate('common.error_heading'), message: errorFlashMessages[0].message, data: {}, name: 'default', }), }); } const messenger = createEmbeddedMessenger({ parentOrigin: siteLink }); this.unsubscribeFromConsignments = subscribeToConsignments(this.handleConsignmentsUpdated); this.embeddedMessenger = messenger; messenger.receiveStyles(styles => embeddedStylesheet.append(styles)); messenger.postFrameLoaded({ contentId: containerId }); messenger.postLoaded(); this.stepTracker = createStepTracker(); this.stepTracker.trackCheckoutStarted(); const consignments = data.getConsignments(); const cart = data.getCart(); const hasMultiShippingEnabled = data.getConfig()?.checkoutSettings?.hasMultiShippingEnabled; const isMultiShippingMode = !!cart && !!consignments && hasMultiShippingEnabled && isUsingMultiShipping(consignments, cart.lineItems); if (isMultiShippingMode) { this.setState({ isMultiShippingMode }, this.handleReady); } else { this.handleReady(); } } catch (error) { this.handleUnhandledError(error); } } render(): ReactNode { const { error } = this.state; let errorModal = null; if (error) { if (isCustomError(error)) { errorModal = <ErrorModal error={ error } onClose={ this.handleCloseErrorModal } title={ error.title } />; } else { errorModal = <ErrorModal error={ error } onClose={ this.handleCloseErrorModal } />; } } return <> <div className={ classNames({ 'is-embedded': isEmbedded() }) }> <div className="layout optimizedCheckout-contentPrimary"> { this.renderContent() } </div> { errorModal } </div> </>; } private renderContent(): ReactNode { const { isPending, loginUrl, promotions = [], steps, } = this.props; const { activeStepType, defaultStepType, isCartEmpty, isRedirecting, } = this.state; if (isCartEmpty) { return ( <EmptyCartMessage loginUrl={ loginUrl } waitInterval={ 3000 } /> ); } return ( <LoadingOverlay hideContentWhenLoading isLoading={ isRedirecting } > <div className="layout-main"> <LoadingNotification isLoading={ isPending } /> <PromotionBannerList promotions={ promotions } /> <ol className="checkout-steps"> { steps .filter(step => step.isRequired) .map(step => this.renderStep({ ...step, isActive: activeStepType ? activeStepType === step.type : defaultStepType === step.type, })) } </ol> </div> { this.renderCartSummary() } </LoadingOverlay> ); } private renderStep(step: CheckoutStepStatus): ReactNode { switch (step.type) { case CheckoutStepType.Customer: return this.renderCustomerStep(step); case CheckoutStepType.Shipping: return this.renderShippingStep(step); case CheckoutStepType.Billing: return this.renderBillingStep(step); case CheckoutStepType.Payment: return this.renderPaymentStep(step); default: return null; } } private renderCustomerStep(step: CheckoutStepStatus): ReactNode { const { isGuestEnabled } = this.props; const { customerViewType = isGuestEnabled ? CustomerViewType.Guest : CustomerViewType.Login, } = this.state; return ( <CheckoutStep { ...step } heading={ <TranslatedString id="customer.customer_heading" /> } key={ step.type } onEdit={ this.handleEditStep } onExpanded={ this.handleExpanded } summary={ <CustomerInfo onSignOut={ this.handleSignOut } onSignOutError={ this.handleError } /> } > <LazyContainer> <Customer checkEmbeddedSupport={ this.checkEmbeddedSupport } isEmbedded={ isEmbedded() } onAccountCreated={ this.navigateToNextIncompleteStep } onChangeViewType={ this.setCustomerViewType } onContinueAsGuest={ this.navigateToNextIncompleteStep } onContinueAsGuestError={ this.handleError } onReady={ this.handleReady } onSignIn={ this.navigateToNextIncompleteStep } onSignInError={ this.handleError } onUnhandledError={ this.handleUnhandledError } viewType={ customerViewType } /> </LazyContainer> </CheckoutStep> ); } private renderShippingStep(step: CheckoutStepStatus): ReactNode { const { hasCartChanged, cart, consignments = [], } = this.props; const { isBillingSameAsShipping, isMultiShippingMode, } = this.state; Iif (!cart) { return; } return ( <CheckoutStep { ...step } heading={ <TranslatedString id="shipping.shipping_heading" /> } key={ step.type } onEdit={ this.handleEditStep } onExpanded={ this.handleExpanded } summary={ consignments.map(consignment => <div className="staticConsignmentContainer" key={ consignment.id }> <StaticConsignment cart={ cart } compactView={ consignments.length < 2 } consignment={ consignment } /> </div>) } > <LazyContainer> <Shipping cartHasChanged={ hasCartChanged } isBillingSameAsShipping={ isBillingSameAsShipping } isMultiShippingMode={ isMultiShippingMode } navigateNextStep={ this.handleShippingNextStep } onCreateAccount={ this.handleShippingCreateAccount } onReady={ this.handleReady } onSignIn={ this.handleShippingSignIn } onToggleMultiShipping={ this.handleToggleMultiShipping } onUnhandledError={ this.handleUnhandledError } /> </LazyContainer> </CheckoutStep> ); } private renderBillingStep(step: CheckoutStepStatus): ReactNode { const { billingAddress } = this.props; return ( <CheckoutStep { ...step } heading={ <TranslatedString id="billing.billing_heading" /> } key={ step.type } onEdit={ this.handleEditStep } onExpanded={ this.handleExpanded } summary={ billingAddress && <StaticBillingAddress address={ billingAddress } /> } > <LazyContainer> <Billing navigateNextStep={ this.navigateToNextIncompleteStep } onReady={ this.handleReady } onUnhandledError={ this.handleUnhandledError } /> </LazyContainer> </CheckoutStep> ); } private renderPaymentStep(step: CheckoutStepStatus): ReactNode { const { consignments, cart, } = this.props; return ( <CheckoutStep { ...step } heading={ <TranslatedString id="payment.payment_heading" /> } key={ step.type } onEdit={ this.handleEditStep } onExpanded={ this.handleExpanded } > <LazyContainer> <Payment checkEmbeddedSupport={ this.checkEmbeddedSupport } isEmbedded={ isEmbedded() } isUsingMultiShipping={ cart && consignments ? isUsingMultiShipping(consignments, cart.lineItems) : false } onCartChangedError={ this.handleCartChangedError } onFinalize={ this.navigateToOrderConfirmation } onReady={ this.handleReady } onSubmit={ this.navigateToOrderConfirmation } onSubmitError={ this.handleError } onUnhandledError={ this.handleUnhandledError } /> </LazyContainer> </CheckoutStep> ); } private renderCartSummary(): ReactNode { return ( <MobileView> { matched => { Iif (matched) { return <LazyContainer> <CartSummaryDrawer /> </LazyContainer>; } return <aside className="layout-cart"> <LazyContainer> <CartSummary /> </LazyContainer> </aside>; } } </MobileView> ); } private navigateToStep(type: CheckoutStepType, options?: { isDefault?: boolean }): void { const { clearError, error, steps } = this.props; const { activeStepType } = this.state; const step = find(steps, { type }); Iif (!step) { return; } if (activeStepType === step.type) { return; } if (options && options.isDefault) { this.setState({ defaultStepType: step.type }); } else { this.setState({ activeStepType: step.type }); } Iif (error) { clearError(error); } } private handleToggleMultiShipping: () => void = () => { const { isMultiShippingMode } = this.state; this.setState({ isMultiShippingMode: !isMultiShippingMode }); }; private navigateToNextIncompleteStep: (options?: { isDefault?: boolean }) => void = options => { const { steps } = this.props; const activeStepIndex = findIndex(steps, { isActive: true }); const activeStep = activeStepIndex >= 0 && steps[activeStepIndex]; Iif (!activeStep) { return; } const previousStep = steps[Math.max(activeStepIndex - 1, 0)]; if (previousStep && this.stepTracker) { this.stepTracker.trackStepCompleted(previousStep.type); } this.navigateToStep(activeStep.type, options); }; private navigateToOrderConfirmation: () => void = () => { const { steps } = this.props; Eif (this.stepTracker) { this.stepTracker.trackStepCompleted(steps[steps.length - 1].type); } Eif (this.embeddedMessenger) { this.embeddedMessenger.postComplete(); } this.setState({ isRedirecting: true }, () => { navigateToOrderConfirmation(); }); }; private checkEmbeddedSupport: (methodIds: string[]) => boolean = methodIds => { const { embeddedSupport } = this.props; return embeddedSupport.isSupported(...methodIds); }; private handleCartChangedError: (error: CartChangedError) => void = () => { this.navigateToStep(CheckoutStepType.Shipping); }; private handleConsignmentsUpdated: (state: CheckoutSelectors) => void = ({ data }) => { const { hasSelectedShippingOptions: prevHasSelectedShippingOptions, activeStepType, } = this.state; const { steps } = this.props; const newHasSelectedShippingOptions = hasSelectedShippingOptions(data.getConsignments() || []); Iif (prevHasSelectedShippingOptions && !newHasSelectedShippingOptions && findIndex(steps, { type: CheckoutStepType.Shipping }) < findIndex(steps, { type: activeStepType }) ) { this.navigateToStep(CheckoutStepType.Shipping); this.setState({ error: new ShippingOptionExpiredError() }); } this.setState({ hasSelectedShippingOptions: newHasSelectedShippingOptions }); }; private handleCloseErrorModal: () => void = () => { this.setState({ error: undefined }); }; private handleExpanded: (type: CheckoutStepType) => void = type => { Eif (this.stepTracker) { this.stepTracker.trackStepViewed(type); } }; private handleUnhandledError: (error: Error) => void = error => { this.handleError(error); // For errors that are not caught and handled by child components, we // handle them here by displaying a generic error modal to the shopper. this.setState({ error }); }; private handleError: (error: Error) => void = error => { const { errorLogger } = this.props; errorLogger.log(error); Eif (this.embeddedMessenger) { this.embeddedMessenger.postError(error); } }; private handleEditStep: (type: CheckoutStepType) => void = type => { this.navigateToStep(type); }; private handleReady: () => void = () => { this.navigateToNextIncompleteStep({ isDefault: true }); }; private handleSignOut: (event: CustomerSignOutEvent) => void = ({ isCartEmpty }) => { const { loginUrl, isGuestEnabled } = this.props; Iif (this.embeddedMessenger) { this.embeddedMessenger.postSignedOut(); } Eif (isGuestEnabled) { this.setCustomerViewType(CustomerViewType.Guest); } if (isCartEmpty) { this.setState({ isCartEmpty: true }); Eif (!isEmbedded()) { return window.top.location.assign(loginUrl); } } this.navigateToStep(CheckoutStepType.Customer); }; private handleShippingNextStep: (isBillingSameAsShipping: boolean) => void = isBillingSameAsShipping => { this.setState({ isBillingSameAsShipping }); if (isBillingSameAsShipping) { this.navigateToNextIncompleteStep(); } else { this.navigateToStep(CheckoutStepType.Billing); } }; private handleShippingSignIn: () => void = () => { this.setCustomerViewType(CustomerViewType.Login); }; private handleShippingCreateAccount: () => void = () => { this.setCustomerViewType(CustomerViewType.CreateAccount); }; private setCustomerViewType: (viewType: CustomerViewType) => void = customerViewType => { const { canCreateAccountInCheckout, createAccountUrl, } = this.props; Iif (customerViewType === CustomerViewType.CreateAccount && (!canCreateAccountInCheckout || isEmbedded()) ) { window.top.location.replace(createAccountUrl); return; } this.navigateToStep(CheckoutStepType.Customer); this.setState({ customerViewType }); }; } export default withLanguage(withCheckout(mapToCheckoutProps)(Checkout)); |