-
Notifications
You must be signed in to change notification settings - Fork 219
Refactor external dispatch actions from being called inside useSelect #6718
Changes from all commits
f776ee8
c40fe94
6dbfc5f
812160c
44be973
61116bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,103 +29,106 @@ export const useStoreCartCoupons = ( context = '' ): StoreCartCoupon => { | |
const { createNotice } = useDispatch( 'core/notices' ); | ||
const { setValidationErrors } = useValidationContext(); | ||
|
||
const results: Pick< | ||
const { | ||
applyCoupon, | ||
removeCoupon, | ||
isApplyingCoupon, | ||
isRemovingCoupon, | ||
}: Pick< | ||
StoreCartCoupon, | ||
'applyCoupon' | 'removeCoupon' | 'isApplyingCoupon' | 'isRemovingCoupon' | ||
| 'applyCoupon' | ||
| 'removeCoupon' | ||
| 'isApplyingCoupon' | ||
| 'isRemovingCoupon' | ||
| 'receiveApplyingCoupon' | ||
> = useSelect( | ||
( select, { dispatch } ) => { | ||
const store = select( storeKey ); | ||
const isApplyingCoupon = store.isApplyingCoupon(); | ||
const isRemovingCoupon = store.isRemovingCoupon(); | ||
const { | ||
applyCoupon, | ||
removeCoupon, | ||
receiveApplyingCoupon, | ||
}: { | ||
applyCoupon: ( coupon: string ) => Promise< boolean >; | ||
removeCoupon: ( coupon: string ) => Promise< boolean >; | ||
receiveApplyingCoupon: ( coupon: string ) => void; | ||
} = dispatch( storeKey ); | ||
|
||
const applyCouponWithNotices = ( couponCode: string ) => { | ||
applyCoupon( couponCode ) | ||
.then( ( result ) => { | ||
if ( result === true ) { | ||
createNotice( | ||
'info', | ||
sprintf( | ||
/* translators: %s coupon code. */ | ||
__( | ||
'Coupon code "%s" has been applied to your cart.', | ||
'woo-gutenberg-products-block' | ||
), | ||
couponCode | ||
), | ||
{ | ||
id: 'coupon-form', | ||
type: 'snackbar', | ||
context, | ||
} | ||
); | ||
} | ||
} ) | ||
.catch( ( error ) => { | ||
setValidationErrors( { | ||
coupon: { | ||
message: decodeEntities( error.message ), | ||
hidden: false, | ||
}, | ||
} ); | ||
// Finished handling the coupon. | ||
receiveApplyingCoupon( '' ); | ||
} ); | ||
}; | ||
|
||
const removeCouponWithNotices = ( couponCode: string ) => { | ||
removeCoupon( couponCode ) | ||
.then( ( result ) => { | ||
if ( result === true ) { | ||
createNotice( | ||
'info', | ||
sprintf( | ||
/* translators: %s coupon code. */ | ||
__( | ||
'Coupon code "%s" has been removed from your cart.', | ||
'woo-gutenberg-products-block' | ||
), | ||
couponCode | ||
), | ||
{ | ||
id: 'coupon-form', | ||
type: 'snackbar', | ||
context, | ||
} | ||
); | ||
} | ||
} ) | ||
.catch( ( error ) => { | ||
createErrorNotice( error.message, { | ||
id: 'coupon-form', | ||
context, | ||
} ); | ||
// Finished handling the coupon. | ||
receiveApplyingCoupon( '' ); | ||
} ); | ||
}; | ||
const actions = dispatch( storeKey ); | ||
|
||
return { | ||
applyCoupon: applyCouponWithNotices, | ||
removeCoupon: removeCouponWithNotices, | ||
isApplyingCoupon, | ||
isRemovingCoupon, | ||
applyCoupon: actions.applyCoupon, | ||
removeCoupon: actions.removeCoupon, | ||
Comment on lines
+50
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could just move this out of the |
||
isApplyingCoupon: store.isApplyingCoupon(), | ||
isRemovingCoupon: store.isRemovingCoupon(), | ||
receiveApplyingCoupon: actions.receiveApplyingCoupon, | ||
}; | ||
}, | ||
[ createErrorNotice, createNotice ] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These dependencies shouldn't be needed now? |
||
); | ||
|
||
const applyCouponWithNotices = ( couponCode: string ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know how often |
||
applyCoupon( couponCode ) | ||
.then( ( result ) => { | ||
if ( result === true ) { | ||
createNotice( | ||
'info', | ||
sprintf( | ||
/* translators: %s coupon code. */ | ||
__( | ||
'Coupon code "%s" has been applied to your cart.', | ||
'woo-gutenberg-products-block' | ||
), | ||
couponCode | ||
), | ||
{ | ||
id: 'coupon-form', | ||
type: 'snackbar', | ||
context, | ||
} | ||
); | ||
} | ||
} ) | ||
.catch( ( error ) => { | ||
setValidationErrors( { | ||
coupon: { | ||
message: decodeEntities( error.message ), | ||
hidden: false, | ||
}, | ||
} ); | ||
// Finished handling the coupon. | ||
receiveApplyingCoupon( '' ); | ||
} ); | ||
}; | ||
|
||
const removeCouponWithNotices = ( couponCode: string ) => { | ||
removeCoupon( couponCode ) | ||
.then( ( result ) => { | ||
if ( result === true ) { | ||
createNotice( | ||
'info', | ||
sprintf( | ||
/* translators: %s coupon code. */ | ||
__( | ||
'Coupon code "%s" has been removed from your cart.', | ||
'woo-gutenberg-products-block' | ||
), | ||
couponCode | ||
), | ||
{ | ||
id: 'coupon-form', | ||
type: 'snackbar', | ||
context, | ||
} | ||
); | ||
} | ||
} ) | ||
.catch( ( error ) => { | ||
createErrorNotice( error.message, { | ||
id: 'coupon-form', | ||
context, | ||
} ); | ||
// Finished handling the coupon. | ||
receiveApplyingCoupon( '' ); | ||
} ); | ||
}; | ||
|
||
return { | ||
appliedCoupons: cartCoupons, | ||
isLoading: cartIsLoading, | ||
...results, | ||
applyCoupon: applyCouponWithNotices, | ||
removeCoupon: removeCouponWithNotices, | ||
isApplyingCoupon, | ||
isRemovingCoupon, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a broken TS, wasn't sure what's the best fix @opr either add
receiveApplyingCoupon
toStoreCartCoupon
or have another sort of union. Not even sure whatStoreCartCoupon
should be and what should it include.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to type this, it should be inferred, and it also seems weird to still pick dispatch actions inside
useSelect
I propose we drop the explicit typing, remove theactions
from theuseSelect
and get them fromuseDispatch
instead. This is consistent with how it's being done elsewhere in the codebase and the data store refactor work.I tested this on wpcom and it works still.
See this example patch and let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you Thomas! I applied the changes.