/** * External dependencies */ import { Fragment, useState, useCallback, useEffect } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import AddressForm from '@woocommerce/base-components/address-form'; import defaultAddressFields from '@woocommerce/base-components/address-form/default-address-fields'; import { FormStep, CheckoutForm, NoShipping, Policies, } from '@woocommerce/base-components/checkout'; import { PlaceOrderButton, ReturnToCartButton, } from '@woocommerce/base-components/cart-checkout'; import { ValidatedTextInput } from '@woocommerce/base-components/text-input'; import ShippingRatesControl from '@woocommerce/base-components/shipping-rates-control'; 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 './style.scss'; const Block = ( { isEditor = false, ...props } ) => ( ); const Checkout = ( { attributes, cartCoupons = [], cartItems = [], cartTotals = {}, shippingRates = [], scrollToTop, } ) => { const { isEditor } = 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 ] ); 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 );