-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
use-store-cart-coupons.ts
149 lines (140 loc) · 3.84 KB
/
use-store-cart-coupons.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import {
CART_STORE_KEY,
VALIDATION_STORE_KEY,
CHECKOUT_STORE_KEY,
} from '@woocommerce/block-data';
import { decodeEntities } from '@wordpress/html-entities';
import type { StoreCartCoupon, ApiErrorResponse } from '@woocommerce/types';
import { applyCheckoutFilter } from '@woocommerce/blocks-checkout';
/**
* Internal dependencies
*/
import { useStoreCart } from './use-store-cart';
/**
* 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/trunk/src/RestApi/StoreApi
*/
export const useStoreCartCoupons = ( context = '' ): StoreCartCoupon => {
const { cartCoupons, cartIsLoading } = useStoreCart();
const { createErrorNotice } = useDispatch( 'core/notices' );
const { createNotice } = useDispatch( 'core/notices' );
const { setValidationErrors } = useDispatch( VALIDATION_STORE_KEY );
const {
isApplyingCoupon,
isRemovingCoupon,
}: Pick< StoreCartCoupon, 'isApplyingCoupon' | 'isRemovingCoupon' > =
useSelect(
( select ) => {
const store = select( CART_STORE_KEY );
return {
isApplyingCoupon: store.isApplyingCoupon(),
isRemovingCoupon: store.isRemovingCoupon(),
};
},
[ createErrorNotice, createNotice ]
);
const { applyCoupon, removeCoupon } = useDispatch( CART_STORE_KEY );
const orderId = useSelect( ( select ) =>
select( CHECKOUT_STORE_KEY ).getOrderId()
);
// Return cart, checkout or generic error message.
const getCouponErrorMessage = ( error: ApiErrorResponse ) => {
if ( orderId && orderId > 0 && error?.data?.details?.checkout ) {
return error.data.details.checkout;
} else if ( error?.data?.details?.cart ) {
return error.data.details.cart;
}
return error.message;
};
const applyCouponWithNotices = ( couponCode: string ) => {
return applyCoupon( couponCode )
.then( () => {
if (
applyCheckoutFilter( {
filterName: 'showApplyCouponNotice',
defaultValue: true,
arg: { couponCode, context },
} )
) {
createNotice(
'info',
sprintf(
/* translators: %s coupon code. */
__(
'Coupon code "%s" has been applied to your cart.',
'woocommerce'
),
couponCode
),
{
id: 'coupon-form',
type: 'snackbar',
context,
}
);
}
return Promise.resolve( true );
} )
.catch( ( error ) => {
const errorMessage = getCouponErrorMessage( error );
setValidationErrors( {
coupon: {
message: decodeEntities( errorMessage ), // TODO fix the circular loop with ApiErrorResponseData and ApiErrorResponseDataDetails
hidden: false,
},
} );
return Promise.resolve( false );
} );
};
const removeCouponWithNotices = ( couponCode: string ) => {
return removeCoupon( couponCode )
.then( () => {
if (
applyCheckoutFilter( {
filterName: 'showRemoveCouponNotice',
defaultValue: true,
arg: { couponCode, context },
} )
) {
createNotice(
'info',
sprintf(
/* translators: %s coupon code. */
__(
'Coupon code "%s" has been removed from your cart.',
'woocommerce'
),
couponCode
),
{
id: 'coupon-form',
type: 'snackbar',
context,
}
);
}
return Promise.resolve( true );
} )
.catch( ( error ) => {
createErrorNotice( error.message, {
id: 'coupon-form',
context,
} );
return Promise.resolve( false );
} );
};
return {
appliedCoupons: cartCoupons,
isLoading: cartIsLoading,
applyCoupon: applyCouponWithNotices,
removeCoupon: removeCouponWithNotices,
isApplyingCoupon,
isRemovingCoupon,
};
};