2020-02-25 11:36:53 +00:00
|
|
|
/** @typedef { import('@woocommerce/type-defs/hooks').StoreCartCoupon } StoreCartCoupon */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-03 10:26:02 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2020-02-25 11:36:53 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2020-03-17 11:45:33 +00:00
|
|
|
import { useValidationContext } from '@woocommerce/base-context';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useStoreCart } from './use-store-cart';
|
2020-03-17 11:45:33 +00:00
|
|
|
import { useStoreNotices } from '../use-store-notices';
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a custom hook for loading the Store API /cart/coupons endpoint and an
|
|
|
|
* action for adding a coupon _to_ the cart.
|
|
|
|
* See also: https://github.com/woocommerce/woocommerce-gutenberg-products-block/tree/master/src/RestApi/StoreApi
|
|
|
|
*
|
|
|
|
* @return {StoreCartCoupon} An object exposing data and actions from/for the
|
|
|
|
* store api /cart/coupons endpoint.
|
|
|
|
*/
|
|
|
|
export const useStoreCartCoupons = () => {
|
2020-03-10 15:49:26 +00:00
|
|
|
const { cartCoupons, cartErrors, cartIsLoading } = useStoreCart();
|
2020-03-16 20:57:12 +00:00
|
|
|
const { addErrorNotice, addSnackbarNotice } = useStoreNotices();
|
2020-03-17 11:45:33 +00:00
|
|
|
const { setValidationErrors } = useValidationContext();
|
2020-02-25 11:36:53 +00:00
|
|
|
|
2020-03-03 10:26:02 +00:00
|
|
|
const results = useSelect(
|
|
|
|
( select, { dispatch } ) => {
|
|
|
|
const store = select( storeKey );
|
|
|
|
const isApplyingCoupon = store.isApplyingCoupon();
|
|
|
|
const isRemovingCoupon = store.isRemovingCoupon();
|
2020-03-17 11:45:33 +00:00
|
|
|
const {
|
|
|
|
applyCoupon,
|
|
|
|
removeCoupon,
|
|
|
|
receiveApplyingCoupon,
|
|
|
|
} = dispatch( storeKey );
|
2020-03-03 10:26:02 +00:00
|
|
|
|
|
|
|
const applyCouponWithNotices = ( couponCode ) => {
|
|
|
|
applyCoupon( couponCode )
|
|
|
|
.then( ( result ) => {
|
|
|
|
if ( result === true ) {
|
2020-03-16 20:57:12 +00:00
|
|
|
addSnackbarNotice(
|
2020-03-03 10:26:02 +00:00
|
|
|
sprintf(
|
|
|
|
// translators: %s coupon code.
|
|
|
|
__(
|
|
|
|
'Coupon code "%s" has been applied to your cart',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
couponCode
|
|
|
|
),
|
|
|
|
{
|
|
|
|
id: 'coupon-form',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
2020-03-17 11:45:33 +00:00
|
|
|
setValidationErrors( { coupon: error.message } );
|
|
|
|
// Finished handling the coupon.
|
|
|
|
receiveApplyingCoupon( '' );
|
2020-03-03 10:26:02 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeCouponWithNotices = ( couponCode ) => {
|
|
|
|
removeCoupon( couponCode )
|
|
|
|
.then( ( result ) => {
|
|
|
|
if ( result === true ) {
|
2020-03-16 20:57:12 +00:00
|
|
|
addSnackbarNotice(
|
2020-03-03 10:26:02 +00:00
|
|
|
sprintf(
|
|
|
|
// translators: %s coupon code.
|
|
|
|
__(
|
|
|
|
'Coupon code "%s" has been removed from your cart',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
couponCode
|
|
|
|
),
|
|
|
|
{
|
|
|
|
id: 'coupon-form',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
addErrorNotice( error.message, {
|
|
|
|
id: 'coupon-form',
|
|
|
|
} );
|
2020-03-17 11:45:33 +00:00
|
|
|
// Finished handling the coupon.
|
|
|
|
receiveApplyingCoupon( '' );
|
2020-03-03 10:26:02 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
applyCoupon: applyCouponWithNotices,
|
|
|
|
removeCoupon: removeCouponWithNotices,
|
|
|
|
isApplyingCoupon,
|
|
|
|
isRemovingCoupon,
|
|
|
|
};
|
|
|
|
},
|
2020-03-16 20:57:12 +00:00
|
|
|
[ addErrorNotice, addSnackbarNotice ]
|
2020-03-03 10:26:02 +00:00
|
|
|
);
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
appliedCoupons: cartCoupons,
|
2020-03-10 15:49:26 +00:00
|
|
|
cartCouponsErrors: cartErrors,
|
2020-02-25 11:36:53 +00:00
|
|
|
isLoading: cartIsLoading,
|
|
|
|
...results,
|
|
|
|
};
|
|
|
|
};
|