2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-12-16 22:13:41 +00:00
|
|
|
import { Fragment, useState } from '@wordpress/element';
|
2019-12-06 13:18:55 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import FormStep from '@woocommerce/base-components/checkout/form-step';
|
|
|
|
import CheckoutForm from '@woocommerce/base-components/checkout/form';
|
2019-12-16 22:13:41 +00:00
|
|
|
import NoShipping from '@woocommerce/base-components/checkout/no-shipping';
|
|
|
|
import TextInput from '@woocommerce/base-components/text-input';
|
2020-01-17 16:58:08 +00:00
|
|
|
import { ShippingCountryInput } from '@woocommerce/base-components/country-input';
|
2020-02-17 11:12:15 +00:00
|
|
|
import { ShippingStateInput } from '@woocommerce/base-components/state-input';
|
2020-02-26 15:50:53 +00:00
|
|
|
import ShippingRatesControl, {
|
|
|
|
Packages,
|
|
|
|
} from '@woocommerce/base-components/shipping-rates-control';
|
2019-12-16 22:13:41 +00:00
|
|
|
import InputRow from '@woocommerce/base-components/input-row';
|
2020-01-06 22:28:09 +00:00
|
|
|
import { CheckboxControl } from '@wordpress/components';
|
2020-02-14 03:43:13 +00:00
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/base-utils';
|
|
|
|
import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount';
|
2020-01-06 22:28:09 +00:00
|
|
|
import CheckoutProvider from '@woocommerce/base-context/checkout-context';
|
|
|
|
import {
|
|
|
|
ExpressCheckoutFormControl,
|
|
|
|
PaymentMethods,
|
|
|
|
} from '@woocommerce/base-components/payment-methods';
|
2020-02-21 16:40:25 +00:00
|
|
|
import { SHIPPING_ENABLED } from '@woocommerce/block-settings';
|
2020-02-18 10:50:08 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2020-01-06 22:28:09 +00:00
|
|
|
import '../../../payment-methods-demo';
|
2019-12-03 14:12:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component displaying an attribute filter.
|
|
|
|
*/
|
2020-02-26 15:50:53 +00:00
|
|
|
const Block = ( { shippingRates = [], isEditor = false } ) => {
|
|
|
|
const [ selectedShippingRate, setSelectedShippingRate ] = useState( {} );
|
2019-12-16 22:13:41 +00:00
|
|
|
const [ contactFields, setContactFields ] = useState( {} );
|
|
|
|
const [ shouldSavePayment, setShouldSavePayment ] = useState( true );
|
|
|
|
const [ shippingFields, setShippingFields ] = useState( {} );
|
2020-02-26 15:50:53 +00:00
|
|
|
|
|
|
|
const renderShippingRatesControlOption = ( option ) => ( {
|
|
|
|
label: decodeEntities( option.name ),
|
|
|
|
value: option.rate_id,
|
|
|
|
description: decodeEntities( option.description ),
|
|
|
|
secondaryLabel: (
|
|
|
|
<FormattedMonetaryAmount
|
|
|
|
currency={ getCurrencyFromPriceResponse( option ) }
|
|
|
|
value={ option.price }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
secondaryDescription: decodeEntities( option.delivery_time ),
|
|
|
|
} );
|
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
return (
|
2020-01-06 22:28:09 +00:00
|
|
|
<CheckoutProvider>
|
|
|
|
<ExpressCheckoutFormControl />
|
|
|
|
<CheckoutForm>
|
2019-12-16 22:13:41 +00:00
|
|
|
<FormStep
|
2020-01-06 22:28:09 +00:00
|
|
|
id="billing-fields"
|
|
|
|
className="wc-block-checkout__billing-fields"
|
2019-12-16 22:13:41 +00:00
|
|
|
title={ __(
|
2020-01-06 22:28:09 +00:00
|
|
|
'Contact information',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
2020-01-06 22:28:09 +00:00
|
|
|
"We'll use this email to send you details and updates about your order.",
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-01-06 22:28:09 +00:00
|
|
|
stepNumber={ 1 }
|
|
|
|
stepHeadingContent={ () => (
|
|
|
|
<Fragment>
|
|
|
|
{ __(
|
|
|
|
'Already have an account? ',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
<a href="/wp-login.php">
|
|
|
|
{ __(
|
|
|
|
'Log in.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</a>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
2019-12-16 22:13:41 +00:00
|
|
|
>
|
2020-01-06 22:28:09 +00:00
|
|
|
<TextInput
|
|
|
|
type="email"
|
|
|
|
label={ __(
|
|
|
|
'Email address',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ contactFields.email }
|
2020-02-19 15:10:26 +00:00
|
|
|
autoComplete="email"
|
2020-01-06 22:28:09 +00:00
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setContactFields( {
|
|
|
|
...contactFields,
|
|
|
|
email: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__keep-updated"
|
|
|
|
label={ __(
|
|
|
|
'Keep me up to date on news and exclusive offers',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ contactFields.keepUpdated }
|
|
|
|
onChange={ () =>
|
|
|
|
setContactFields( {
|
|
|
|
...contactFields,
|
|
|
|
keepUpdated: ! contactFields.keepUpdated,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
2019-12-16 22:13:41 +00:00
|
|
|
</FormStep>
|
2020-02-21 16:40:25 +00:00
|
|
|
{ SHIPPING_ENABLED && (
|
|
|
|
<FormStep
|
|
|
|
id="shipping-fields"
|
|
|
|
className="wc-block-checkout__shipping-fields"
|
|
|
|
title={ __(
|
|
|
|
'Shipping address',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
description={ __(
|
|
|
|
'Enter the physical address where you want us to deliver your order.',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
stepNumber={ 2 }
|
|
|
|
>
|
|
|
|
<InputRow>
|
|
|
|
<TextInput
|
|
|
|
label={ __(
|
|
|
|
'First name',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ shippingFields.firstName }
|
|
|
|
autoComplete="given-name"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
|
|
|
firstName: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
label={ __(
|
|
|
|
'Surname',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ shippingFields.lastName }
|
|
|
|
autoComplete="family-name"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
|
|
|
lastName: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</InputRow>
|
|
|
|
<TextInput
|
2020-02-14 03:43:13 +00:00
|
|
|
label={ __(
|
2020-02-21 16:40:25 +00:00
|
|
|
'Street address',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
value={ shippingFields.streetAddress }
|
|
|
|
autoComplete="address-line1"
|
2020-02-14 03:43:13 +00:00
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
2020-02-21 16:40:25 +00:00
|
|
|
streetAddress: newValue,
|
2020-02-14 03:43:13 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
label={ __(
|
2020-02-21 16:40:25 +00:00
|
|
|
'Apartment, suite, etc.',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
value={ shippingFields.apartment }
|
|
|
|
autoComplete="address-line2"
|
2020-02-14 03:43:13 +00:00
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
2020-02-21 16:40:25 +00:00
|
|
|
apartment: newValue,
|
2020-02-14 03:43:13 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
2020-02-21 16:40:25 +00:00
|
|
|
<InputRow>
|
|
|
|
<ShippingCountryInput
|
|
|
|
label={ __(
|
|
|
|
'Country / Region',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ shippingFields.country }
|
|
|
|
autoComplete="country"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
|
|
|
country: newValue,
|
|
|
|
state: '',
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
label={ __(
|
|
|
|
'City',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ shippingFields.city }
|
|
|
|
autoComplete="address-level2"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
|
|
|
city: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</InputRow>
|
|
|
|
<InputRow>
|
|
|
|
<ShippingStateInput
|
|
|
|
country={ shippingFields.country }
|
|
|
|
label={ __(
|
|
|
|
'State / County',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ shippingFields.state }
|
|
|
|
autoComplete="address-level1"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
|
|
|
state: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
label={ __(
|
|
|
|
'Postal code',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ shippingFields.postcode }
|
|
|
|
autoComplete="postal-code"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
|
|
|
postcode: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</InputRow>
|
|
|
|
<TextInput
|
|
|
|
type="tel"
|
2020-02-14 03:43:13 +00:00
|
|
|
label={ __(
|
2020-02-21 16:40:25 +00:00
|
|
|
'Phone',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
value={ shippingFields.phone }
|
|
|
|
autoComplete="tel"
|
2020-02-14 03:43:13 +00:00
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
2020-02-21 16:40:25 +00:00
|
|
|
phone: newValue,
|
2020-02-14 03:43:13 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
2020-02-21 16:40:25 +00:00
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__use-address-for-billing"
|
2020-02-14 03:43:13 +00:00
|
|
|
label={ __(
|
2020-02-21 16:40:25 +00:00
|
|
|
'Use same address for billing',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
checked={ shippingFields.useSameForBilling }
|
|
|
|
onChange={ () =>
|
2020-02-14 03:43:13 +00:00
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
2020-02-21 16:40:25 +00:00
|
|
|
useSameForBilling: ! shippingFields.useSameForBilling,
|
2020-02-14 03:43:13 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormStep>
|
2019-12-06 13:18:55 +00:00
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
{ SHIPPING_ENABLED &&
|
2020-02-26 15:50:53 +00:00
|
|
|
( shippingRates.length === 0 && isEditor ? (
|
2020-02-21 16:40:25 +00:00
|
|
|
<NoShipping />
|
|
|
|
) : (
|
|
|
|
<FormStep
|
|
|
|
id="shipping-option"
|
|
|
|
className="wc-block-checkout__shipping-option"
|
|
|
|
title={ __(
|
|
|
|
'Shipping options',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
|
|
|
'Select your shipping method below.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
stepNumber={ 3 }
|
|
|
|
>
|
2020-02-26 15:50:53 +00:00
|
|
|
{ shippingRates.length > 0 ? (
|
|
|
|
<Packages
|
|
|
|
renderOption={
|
|
|
|
renderShippingRatesControlOption
|
|
|
|
}
|
|
|
|
shippingRates={ shippingRates }
|
|
|
|
selected={ [
|
|
|
|
shippingRates[ 0 ].shipping_rates[ 0 ]
|
|
|
|
.rate_id,
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<ShippingRatesControl
|
|
|
|
address={
|
|
|
|
shippingFields.country
|
|
|
|
? {
|
|
|
|
address_1:
|
|
|
|
shippingFields.streetAddress,
|
|
|
|
address_2:
|
|
|
|
shippingFields.apartment,
|
|
|
|
city: shippingFields.city,
|
|
|
|
state: shippingFields.state,
|
|
|
|
postcode:
|
|
|
|
shippingFields.postcode,
|
|
|
|
country:
|
|
|
|
shippingFields.country,
|
|
|
|
}
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
noResultsMessage={ __(
|
|
|
|
'There are no shipping options available. Please ensure that your address has been entered correctly, or contact us if you need any help.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
selected={ selectedShippingRate.methods }
|
|
|
|
renderOption={
|
|
|
|
renderShippingRatesControlOption
|
|
|
|
}
|
|
|
|
onChange={ ( newMethods ) =>
|
|
|
|
setSelectedShippingRate( {
|
|
|
|
...selectedShippingRate,
|
|
|
|
methods: newMethods,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__add-note"
|
|
|
|
label="Add order notes?"
|
2020-02-26 15:50:53 +00:00
|
|
|
checked={ selectedShippingRate.orderNote }
|
2020-02-21 16:40:25 +00:00
|
|
|
onChange={ () =>
|
2020-02-26 15:50:53 +00:00
|
|
|
setSelectedShippingRate( {
|
|
|
|
...selectedShippingRate,
|
|
|
|
orderNote: ! selectedShippingRate.orderNote,
|
2020-02-21 16:40:25 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormStep>
|
|
|
|
) ) }
|
2020-01-06 22:28:09 +00:00
|
|
|
<FormStep
|
|
|
|
id="payment-method"
|
|
|
|
className="wc-block-checkout__payment-method"
|
|
|
|
title={ __(
|
|
|
|
'Payment method',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
|
|
|
'Select a payment method below.',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-01-06 22:28:09 +00:00
|
|
|
stepNumber={ 4 }
|
|
|
|
>
|
|
|
|
<PaymentMethods />
|
|
|
|
{ /*@todo this should be something the payment method controls*/ }
|
|
|
|
<CheckboxControl
|
2020-01-10 09:42:53 +00:00
|
|
|
className="wc-block-checkout__save-card-info"
|
2020-01-06 22:28:09 +00:00
|
|
|
label={ __(
|
|
|
|
'Save payment information to my account for future purchases.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ shouldSavePayment }
|
|
|
|
onChange={ () =>
|
|
|
|
setShouldSavePayment( ! shouldSavePayment )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormStep>
|
|
|
|
</CheckoutForm>
|
|
|
|
</CheckoutProvider>
|
2019-12-03 14:12:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|