Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Add querying selected shipping rate to the store #1829

Merged
merged 7 commits into from
Mar 3, 2020

Conversation

senadir
Copy link
Member

@senadir senadir commented Feb 27, 2020

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.

const { selectedShippingRates, selectShippingRate } = useSelectedShippingRates();

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;

wp.data.select('wc/store/cart').selectedShippingRates();
wp.data.dispatch('wc/store/cart').selectShippingRate(['flat_rate:100'])

However, when you run the dispatcher, the cart data keys are all snake_case instead of kebabCase, still investigating why.

@senadir senadir requested review from mikejolley and nerrad February 27, 2020 14:00
@@ -1,8 +1,8 @@
// @ts-nocheck
Copy link
Member Author

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

* @return {string} The data to return.
*/
export const selectedShippingRates = ( state ) => {
return state.cartData.selectedShippingRates;
Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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

Copy link
Contributor

@nerrad nerrad Feb 27, 2020

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/type-defs/cart.js Outdated Show resolved Hide resolved
* @return {string} The data to return.
*/
export const selectedShippingRates = ( state ) => {
return state.cartData.selectedShippingRates;
Copy link
Contributor

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.

@senadir senadir force-pushed the add/default-to-selected-method branch from ec0ef0b to 72b9352 Compare March 2, 2020 22:43
@senadir senadir marked this pull request as ready for review March 2, 2020 22:45
@senadir senadir requested a review from a team as a code owner March 2, 2020 22:45
@senadir senadir merged commit b7273b4 into add/put-method-on-shipping Mar 3, 2020
@senadir senadir deleted the add/default-to-selected-method branch March 3, 2020 10:35
senadir added a commit that referenced this pull request Mar 3, 2020
* 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
senadir added a commit that referenced this pull request Mar 4, 2020
* 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
senadir added a commit that referenced this pull request Mar 5, 2020
* 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
senadir added a commit that referenced this pull request Mar 5, 2020
* 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
senadir added a commit that referenced this pull request Mar 5, 2020
* 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
senadir added a commit that referenced this pull request Mar 5, 2020
* 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]>
@nerrad nerrad added this to the Future Release milestone Apr 4, 2020
@nerrad nerrad modified the milestones: Future Release, 2.6.0 Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants