/**
* External dependencies
*/
import { Fragment, useState } 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 TextInput 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 from '@woocommerce/base-context/checkout-context';
import {
ExpressCheckoutFormControl,
PaymentMethods,
} from '@woocommerce/base-components/payment-methods';
import { SHIPPING_ENABLED } from '@woocommerce/block-settings';
import { decodeEntities } from '@wordpress/html-entities';
import { useShippingRates } from '@woocommerce/base-hooks';
/**
* Internal dependencies
*/
import './style.scss';
import '../../../payment-methods-demo';
const Block = ( { attributes, isEditor = false, shippingRates = [] } ) => {
const { shippingRatesLoading } = useShippingRates();
const [ selectedShippingRate, setSelectedShippingRate ] = useState( {} );
const [ contactFields, setContactFields ] = useState( {} );
const [ shouldSavePayment, setShouldSavePayment ] = useState( true );
const [ shippingFields, setShippingFields ] = useState( {} );
const [ billingFields, setBillingFields ] = useState( {} );
const [ useShippingAsBilling, setUseShippingAsBilling ] = useState(
attributes.useShippingAsBilling
);
const renderShippingRatesControlOption = ( option ) => ( {
label: decodeEntities( option.name ),
value: option.rate_id,
description: decodeEntities( option.description ),
secondaryLabel: (
),
secondaryDescription: decodeEntities( option.delivery_time ),
} );
const useShippingAddressAsBilling = isEditor
? attributes.useShippingAsBilling
: useShippingAsBilling;
const showBillingFields =
! SHIPPING_ENABLED || ! useShippingAddressAsBilling;
const addressFields = {
...defaultAddressFields,
company: {
...defaultAddressFields.company,
hidden: ! attributes.showCompanyField,
required: attributes.requireCompanyField,
},
address_2: {
...defaultAddressFields.address_2,
hidden: ! attributes.showAddress2Field,
},
};
return (
(
{ __(
'Already have an account? ',
'woo-gutenberg-products-block'
) }
{ __(
'Log in.',
'woo-gutenberg-products-block'
) }
) }
>
setContactFields( {
...contactFields,
email: newValue,
} )
}
required={ true }
/>
setContactFields( {
...contactFields,
keepUpdated: ! contactFields.keepUpdated,
} )
}
/>
{ SHIPPING_ENABLED && (
{ attributes.showPhoneField && (
setShippingFields( {
...shippingFields,
phone: newValue,
} )
}
required={ attributes.requirePhoneField }
/>
) }
setUseShippingAsBilling( isChecked )
}
/>
) }
{ showBillingFields && (
) }
{ SHIPPING_ENABLED &&
( shippingRates.length === 0 && isEditor ? (
) : (
setSelectedShippingRate( {
...selectedShippingRate,
orderNote: ! selectedShippingRate.orderNote,
} )
}
/>
) ) }
{ /*@todo this should be something the payment method controls*/ }
setShouldSavePayment( ! shouldSavePayment )
}
/>
{ attributes.showPolicyLinks && }
);
};
export default Block;