-
Notifications
You must be signed in to change notification settings - Fork 219
Add querying selected shipping rate to the store #1829
Add querying selected shipping rate to the store #1829
Conversation
@@ -1,8 +1,8 @@ | |||
// @ts-nocheck |
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 was added by my editor I guess
assets/js/data/cart/selectors.js
Outdated
* @return {string} The data to return. | ||
*/ | ||
export const selectedShippingRates = ( state ) => { | ||
return state.cartData.selectedShippingRates; |
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.
so this works fine, but if I dispatch this no longer works and all keys are now snake_case.
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.
So it looks like the other place receiveCart
is being used automatically handles snake-casing the keys: https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/abfa541dc9398519a845a17a9581bcfb1e2005a1/assets/js/data/cart/resolvers.js#L28-L33
So what could be done is doing this key conversion in the action itself otherwise this is going to happen anytime the action creator is called.
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.
what confuses me is that apply coupon and removeCoupn do not call it. I will test this and see
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.
Huh! You're right. Might be a bug unless the api is returning a different shape for coupon requests - which if so, probably isn't a good idea anyways.
So, here's what I suggest then (that differs a bit from my original suggestion). Since this is a state shaped/maintained by the reducer, the key normalization should happen in the reducer for any actions updating the cartData
property. That way we're assured the state is always in the expected shape when it's updated.
assets/js/data/cart/selectors.js
Outdated
* @return {string} The data to return. | ||
*/ | ||
export const selectedShippingRates = ( state ) => { | ||
return state.cartData.selectedShippingRates; |
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.
So it looks like the other place receiveCart
is being used automatically handles snake-casing the keys: https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/abfa541dc9398519a845a17a9581bcfb1e2005a1/assets/js/data/cart/resolvers.js#L28-L33
So what could be done is doing this key conversion in the action itself otherwise this is going to happen anytime the action creator is called.
ec0ef0b
to
72b9352
Compare
* add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues
* add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues
* add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues
* add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues
* add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues
* add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (#1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (#1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <[email protected]> Co-authored-by: Albert Juhé Lluveras <[email protected]>
This PR builds #1794 it adds a new hook
useSelectedShippingRates
that returns an array of selected shipping rates, and an action dispatcher that selects a shipping rate to persist it to the user session.So far I wasn’t successful at linking it to the current shipping control (cc @Aljullu for some help).
so if you want to test this directly, from the console you can dispatch or select the current things;
However, when you run the dispatcher, the cart data keys are all snake_case instead of kebabCase, still investigating why.