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-01-10 14:37:27 +00:00
|
|
|
import TextInput from '@woocommerce/base-components/text-input';
|
|
|
|
import Label from '@woocommerce/base-components/label';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-03-10 14:40:30 +00:00
|
|
|
import { withInstanceId } from 'wordpress-compose';
|
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 );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( currentIsLoading.current !== isLoading ) {
|
|
|
|
if ( ! isLoading && couponValue ) {
|
|
|
|
setCouponValue( '' );
|
|
|
|
}
|
|
|
|
currentIsLoading.current = isLoading;
|
|
|
|
}
|
|
|
|
}, [ isLoading, couponValue ] );
|
|
|
|
|
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-10 14:40:30 +00:00
|
|
|
htmlFor={ `wc-block-coupon-code__input-${ instanceId }` }
|
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">
|
|
|
|
<TextInput
|
2020-03-10 14:40:30 +00:00
|
|
|
id={ `wc-block-coupon-code__input-${ instanceId }` }
|
2020-02-26 12:48:16 +00:00
|
|
|
className="wc-block-coupon-code__input"
|
|
|
|
label={ __(
|
|
|
|
'Enter code',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ couponValue }
|
|
|
|
onChange={ ( newCouponValue ) =>
|
|
|
|
setCouponValue( newCouponValue )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<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-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 );
|