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';
|
|
|
|
import Button from '@woocommerce/base-components/button';
|
|
|
|
import TextInput from '@woocommerce/base-components/text-input';
|
|
|
|
import Label from '@woocommerce/base-components/label';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import withComponentId from '@woocommerce/base-hocs/with-component-id';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
2020-02-25 11:36:53 +00:00
|
|
|
import LoadingMask from '../../loading-mask';
|
2020-01-10 14:37:27 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
const TotalsCouponCodeInput = ( {
|
|
|
|
componentId,
|
|
|
|
isLoading = false,
|
|
|
|
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'
|
|
|
|
) }
|
|
|
|
htmlFor={ `wc-block-coupon-code__input-${ componentId }` }
|
|
|
|
/>
|
|
|
|
}
|
2020-02-19 16:33:10 +00:00
|
|
|
initialOpen={ true }
|
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
|
|
|
|
id={ `wc-block-coupon-code__input-${ componentId }` }
|
|
|
|
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"
|
|
|
|
disabled={ isLoading }
|
|
|
|
onClick={ () => {
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
export default withComponentId( TotalsCouponCodeInput );
|