/** * External dependencies */ import { Fragment, useState, useCallback, useEffect } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import defaultAddressFields from '@woocommerce/base-components/cart-checkout/address-form/default-address-fields'; import { AddressForm, FormStep, CheckoutForm, PlaceOrderButton, Policies, ReturnToCartButton, ShippingRatesControl, } from '@woocommerce/base-components/cart-checkout'; import { ValidatedTextInput } from '@woocommerce/base-components/text-input'; import CheckboxControl from '@woocommerce/base-components/checkbox-control'; import { getCurrencyFromPriceResponse } from '@woocommerce/base-utils'; import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount'; import { CheckoutProvider, useCheckoutContext, useEditorContext, useShippingDataContext, useBillingDataContext, useValidationContext, } from '@woocommerce/base-context'; import { ExpressCheckoutFormControl, PaymentMethods, } from '@woocommerce/base-components/payment-methods'; import { decodeEntities } from '@wordpress/html-entities'; import { Sidebar, SidebarLayout, Main, } from '@woocommerce/base-components/sidebar-layout'; import { getSetting } from '@woocommerce/settings'; import withScrollToTop from '@woocommerce/base-hocs/with-scroll-to-top'; /** * Internal dependencies */ import CheckoutSidebar from './sidebar'; import CheckoutOrderError from './checkout-order-error'; import NoShippingPlaceholder from './no-shipping-placeholder'; import './style.scss'; const Block = ( props ) => ( ); const Checkout = ( { attributes, cartCoupons = [], cartItems = [], cartTotals = {}, scrollToTop, shippingRates = [], } ) => { const { isEditor } = useEditorContext(); const { hasOrder, hasError: checkoutHasError, isComplete: checkoutIsComplete, } = useCheckoutContext(); const { showAllValidationErrors } = useValidationContext(); const { shippingRatesLoading, shippingAddress, setShippingAddress, needsShipping, } = useShippingDataContext(); const { billingData, setBillingData } = useBillingDataContext(); const [ shippingAsBilling, setShippingAsBilling ] = useState( needsShipping ); const renderShippingRatesControlOption = ( option ) => ( { label: decodeEntities( option.name ), value: option.rate_id, description: decodeEntities( option.description ), secondaryLabel: ( ), secondaryDescription: decodeEntities( option.delivery_time ), } ); const showBillingFields = ! needsShipping || ! shippingAsBilling; const addressFields = { ...defaultAddressFields, company: { ...defaultAddressFields.company, hidden: ! attributes.showCompanyField, required: attributes.requireCompanyField, }, address_2: { ...defaultAddressFields.address_2, hidden: ! attributes.showAddress2Field, }, }; const setShippingFields = useCallback( ( address ) => { if ( shippingAsBilling ) { setShippingAddress( address ); setBillingData( address ); } else { setShippingAddress( address ); } }, [ setShippingAddress, setBillingData, shippingAsBilling ] ); useEffect( () => { if ( shippingAsBilling ) { setBillingData( { ...shippingAddress, shippingAsBilling } ); } else { setBillingData( { shippingAsBilling } ); } }, [ shippingAsBilling, setBillingData ] ); useEffect( () => { if ( checkoutIsComplete && checkoutHasError ) { showAllValidationErrors(); scrollToTop( { focusableSelector: 'input:invalid' } ); } }, [ checkoutIsComplete, checkoutHasError ] ); if ( ! isEditor && ! hasOrder ) { return ; } return ( <> ( { __( 'Already have an account? ', 'woo-gutenberg-products-block' ) } { __( 'Log in.', 'woo-gutenberg-products-block' ) } ) } > setBillingData( { email: newValue } ) } required={ true } /> { needsShipping && ( { attributes.showPhoneField && ( setBillingData( { phone: newValue, } ) } required={ attributes.requirePhoneField } /> ) } setShippingAsBilling( isChecked ) } /> ) } { showBillingFields && ( ) } { needsShipping && ( { shippingRates.length === 0 && isEditor ? ( ) : ( ) } { /*@todo This is not implemented*/ } null } /> ) } { attributes.showReturnToCart && ( ) } { attributes.showPolicyLinks && } > ); }; export default withScrollToTop( Block );