2020-01-10 14:37:27 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-02-25 11:36:53 +00:00
|
|
|
import { useState, useEffect, useRef } from '@wordpress/element';
|
2020-01-10 14:37:27 +00:00
|
|
|
import { PanelBody, PanelRow } from 'wordpress-components';
|
2020-03-13 12:02:08 +00:00
|
|
|
import { Button } from '@woocommerce/base-components/cart-checkout';
|
2020-03-23 11:22:00 +00:00
|
|
|
import { ValidatedTextInput } from '@woocommerce/base-components/text-input';
|
2020-01-10 14:37:27 +00:00
|
|
|
import Label from '@woocommerce/base-components/label';
|
2020-03-17 11:45:33 +00:00
|
|
|
import { ValidationInputError } from '@woocommerce/base-components/validation';
|
2020-01-10 14:37:27 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2020-03-26 12:39:54 +00:00
|
|
|
import { withInstanceId } from '@woocommerce/base-hocs/with-instance-id';
|
2020-03-17 11:45:33 +00:00
|
|
|
import { useValidationContext } from '@woocommerce/base-context';
|
2020-01-10 14:37:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-02-25 11:36:53 +00:00
|
|
|
import LoadingMask from '../../loading-mask';
|
2020-03-13 12:02:08 +00:00
|
|
|
import './style.scss';
|
2020-01-10 14:37:27 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
const TotalsCouponCodeInput = ( {
|
2020-03-10 14:40:30 +00:00
|
|
|
instanceId,
|
2020-02-25 11:36:53 +00:00
|
|
|
isLoading = false,
|
2020-03-12 09:41:35 +00:00
|
|
|
initialOpen,
|
2020-02-25 11:36:53 +00:00
|
|
|
onSubmit = () => {},
|
|
|
|
} ) => {
|
2020-01-10 14:37:27 +00:00
|
|
|
const [ couponValue, setCouponValue ] = useState( '' );
|
2020-02-25 11:36:53 +00:00
|
|
|
const currentIsLoading = useRef( false );
|
2020-03-23 11:22:00 +00:00
|
|
|
const { getValidationError, getValidationErrorId } = useValidationContext();
|
|
|
|
|
|
|
|
const validationError = getValidationError( 'coupon' );
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( currentIsLoading.current !== isLoading ) {
|
2020-03-23 11:22:00 +00:00
|
|
|
if ( ! isLoading && couponValue && ! validationError ) {
|
2020-02-25 11:36:53 +00:00
|
|
|
setCouponValue( '' );
|
|
|
|
}
|
|
|
|
currentIsLoading.current = isLoading;
|
|
|
|
}
|
2020-03-23 11:22:00 +00:00
|
|
|
}, [ isLoading, couponValue, validationError ] );
|
2020-03-17 11:45:33 +00:00
|
|
|
|
|
|
|
const textInputId = `wc-block-coupon-code__input-${ instanceId }`;
|
2020-02-25 11:36:53 +00:00
|
|
|
|
2020-01-10 14:37:27 +00:00
|
|
|
return (
|
|
|
|
<PanelBody
|
|
|
|
className="wc-block-coupon-code"
|
|
|
|
title={
|
|
|
|
<Label
|
|
|
|
label={ __(
|
|
|
|
'Coupon Code?',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Introduce Coupon Code',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-17 11:45:33 +00:00
|
|
|
htmlFor={ textInputId }
|
2020-01-10 14:37:27 +00:00
|
|
|
/>
|
|
|
|
}
|
2020-03-12 09:41:35 +00:00
|
|
|
initialOpen={ initialOpen }
|
2020-01-10 14:37:27 +00:00
|
|
|
>
|
2020-02-25 11:36:53 +00:00
|
|
|
<LoadingMask
|
|
|
|
screenReaderLabel={ __(
|
|
|
|
'Applying coupon…',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
isLoading={ isLoading }
|
|
|
|
showSpinner={ false }
|
|
|
|
>
|
|
|
|
<PanelRow className="wc-block-coupon-code__row">
|
2020-02-26 12:48:16 +00:00
|
|
|
<form className="wc-block-coupon-code__form">
|
2020-03-23 11:22:00 +00:00
|
|
|
<ValidatedTextInput
|
2020-03-17 11:45:33 +00:00
|
|
|
id={ textInputId }
|
2020-03-23 11:22:00 +00:00
|
|
|
errorId="coupon"
|
|
|
|
className="wc-block-coupon-code__input"
|
2020-02-26 12:48:16 +00:00
|
|
|
label={ __(
|
|
|
|
'Enter code',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ couponValue }
|
2020-03-17 11:45:33 +00:00
|
|
|
ariaDescribedBy={ getValidationErrorId(
|
|
|
|
textInputId
|
|
|
|
) }
|
|
|
|
onChange={ ( newCouponValue ) => {
|
|
|
|
setCouponValue( newCouponValue );
|
|
|
|
} }
|
2020-03-23 11:22:00 +00:00
|
|
|
validateOnMount={ false }
|
|
|
|
showError={ false }
|
2020-02-26 12:48:16 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
className="wc-block-coupon-code__button"
|
2020-03-16 13:15:35 +00:00
|
|
|
disabled={ isLoading || ! couponValue }
|
2020-03-11 20:23:14 +00:00
|
|
|
onClick={ ( e ) => {
|
|
|
|
e.preventDefault();
|
2020-02-26 12:48:16 +00:00
|
|
|
onSubmit( couponValue );
|
|
|
|
} }
|
|
|
|
type="submit"
|
|
|
|
>
|
|
|
|
{ __( 'Apply', 'woo-gutenberg-products-block' ) }
|
|
|
|
</Button>
|
|
|
|
</form>
|
2020-03-23 11:22:00 +00:00
|
|
|
<ValidationInputError
|
|
|
|
propertyName="coupon"
|
|
|
|
elementId={ textInputId }
|
|
|
|
/>
|
2020-02-25 11:36:53 +00:00
|
|
|
</PanelRow>
|
|
|
|
</LoadingMask>
|
2020-01-10 14:37:27 +00:00
|
|
|
</PanelBody>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
TotalsCouponCodeInput.propTypes = {
|
|
|
|
onSubmit: PropTypes.func,
|
2020-02-25 11:36:53 +00:00
|
|
|
isLoading: PropTypes.bool,
|
2020-01-10 14:37:27 +00:00
|
|
|
};
|
|
|
|
|
2020-03-10 14:40:30 +00:00
|
|
|
export default withInstanceId( TotalsCouponCodeInput );
|