/** * 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, NoShipping, 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, useValidationContext, useCheckoutContext, useShippingDataContext, useBillingDataContext, } from '@woocommerce/base-context'; import { ExpressCheckoutFormControl, PaymentMethods, } from '@woocommerce/base-components/payment-methods'; import { SHIPPING_ENABLED } from '@woocommerce/block-settings'; 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/index.js'; import CheckoutOrderError from './checkout-order-error/index.js'; import './style.scss'; const Block = ( { isEditor = false, ...props } ) => ( ); const Checkout = ( { attributes, cartCoupons = [], cartItems = [], cartTotals = {}, shippingRates = [], scrollToTop, } ) => { const { isEditor, hasOrder } = useCheckoutContext(); const { shippingRatesLoading, shippingAddress, setShippingAddress, } = useShippingDataContext(); const { billingData, setBillingData } = useBillingDataContext(); const { hasValidationErrors, showAllValidationErrors, } = useValidationContext(); const [ contactFields, setContactFields ] = useState( {} ); const [ shippingAsBilling, setShippingAsBilling ] = useState( true ); const validateSubmit = () => { if ( hasValidationErrors() ) { showAllValidationErrors(); scrollToTop( { focusableSelector: 'input:invalid' } ); return false; } return true; }; const renderShippingRatesControlOption = ( option ) => ( { label: decodeEntities( option.name ), value: option.rate_id, description: decodeEntities( option.description ), secondaryLabel: ( ), secondaryDescription: decodeEntities( option.delivery_time ), } ); const showBillingFields = ! SHIPPING_ENABLED || ! 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, setBillingData ] ); if ( ! isEditor && ! hasOrder ) { return ; } return ( <>
( { __( 'Already have an account? ', 'woo-gutenberg-products-block' ) } { __( 'Log in.', 'woo-gutenberg-products-block' ) } ) } > setBillingData( { email: newValue } ) } required={ true } /> setContactFields( { ...contactFields, keepUpdated: ! contactFields.keepUpdated, } ) } /> { SHIPPING_ENABLED && ( { attributes.showPhoneField && ( setBillingData( { phone: newValue, } ) } required={ attributes.requirePhoneField } /> ) } setShippingAsBilling( isChecked ) } required={ attributes.requirePhoneField } /> ) } { showBillingFields && ( ) } { SHIPPING_ENABLED && ( { shippingRates.length === 0 && isEditor ? ( ) : ( ) } { /*@todo This is not implemented*/ } null } /> ) }
{ attributes.showReturnToCart && ( ) }
{ attributes.showPolicyLinks && }
); }; export default withScrollToTop( Block );