/** * 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, getShippingRatesPackageCount, getShippingRatesRateCount, } 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 { useStoreCart, usePaymentMethods } from '@woocommerce/base-hooks'; 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 ) => { return ( ); }; const Checkout = ( { attributes, scrollToTop } ) => { const { isEditor } = useEditorContext(); const { cartItems, cartTotals, cartCoupons } = useStoreCart(); const { hasOrder, hasError: checkoutHasError, isComplete: checkoutIsComplete, isCalculating: checkoutIsCalculating, } = useCheckoutContext(); const { showAllValidationErrors } = useValidationContext(); const { shippingRates, shippingRatesLoading, shippingAddress, setShippingAddress, needsShipping, } = useShippingDataContext(); const { billingData, setBillingData } = useBillingDataContext(); const { paymentMethods } = usePaymentMethods(); 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 && ! checkoutIsCalculating ) { showAllValidationErrors(); scrollToTop( { focusableSelector: 'input:invalid' } ); } }, [ checkoutIsComplete, checkoutHasError, checkoutIsCalculating ] ); 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 && ( 1 ? __( 'Select shipping options below.', 'woo-gutenberg-products-block' ) : '' } > { getShippingRatesPackageCount( shippingRates ) === 0 && isEditor ? ( ) : ( ) } { /*@todo This is not implemented*/ } null } /> ) } 1 ? __( 'Select a payment method below.', 'woo-gutenberg-products-block' ) : '' } >
{ attributes.showReturnToCart && ( ) }
{ attributes.showPolicyLinks && }
); }; export default withScrollToTop( Block );