This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
19 changed files
with
208 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** @typedef { import('@woocommerce/type-defs/hooks').SelectedShippingRates } SelectedShippingRates */ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data'; | ||
|
||
/** | ||
* This is a custom hook for loading the selected shipping rate from the cart store and actions for selecting a rate. | ||
* See also: https://github.com/woocommerce/woocommerce-gutenberg-products-block/tree/master/src/RestApi/StoreApi | ||
* | ||
* @return {SelectedShippingRates} An object exposing data and actions from/for the | ||
* store api /cart/select-shipping endpoint. | ||
*/ | ||
export const useSelectShippingRate = () => { | ||
const { selectShippingRate } = useDispatch( storeKey ); | ||
return { | ||
selectShippingRate, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useDebounce } from 'use-debounce'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useCollection } from './use-collection'; | ||
import { useStoreCart } from './use-store-cart'; | ||
|
||
/** | ||
* This is a custom hook that is wired up to the `wc/store/collections` data | ||
* store for the `wc/store/cart/shipping-rates` route. Given a query object, this | ||
* will ensure a component is kept up to date with the shipping rates matching that | ||
* query in the store state. | ||
* | ||
* @param {Object} query An object containing any query arguments to be | ||
* included with the collection request for the | ||
* shipping rates. Does not have to be included. | ||
* | ||
* @return {Object} This hook will return an object with three properties: | ||
* - shippingRates An array of shipping rate objects. | ||
* - shippingRatesLoading A boolean indicating whether the shipping | ||
* rates are still loading or not. | ||
*/ | ||
export const useShippingRates = ( query ) => { | ||
const [ debouncedQuery ] = useDebounce( query, 300 ); | ||
export const useShippingRates = () => { | ||
const { shippingRates } = useStoreCart(); | ||
const results = useSelect( ( select, { dispatch } ) => { | ||
const store = select( storeKey ); | ||
const shippingRatesLoading = store.areShippingRatesLoading(); | ||
const { updateShipping } = dispatch( storeKey ); | ||
|
||
const { | ||
results: shippingRates, | ||
isLoading: shippingRatesLoading, | ||
} = useCollection( { | ||
namespace: '/wc/store', | ||
resourceName: 'cart/shipping-rates', | ||
query: debouncedQuery, | ||
} ); | ||
return { | ||
shippingRatesLoading, | ||
updateShipping, | ||
}; | ||
}, [] ); | ||
|
||
return { | ||
shippingRates, | ||
shippingRatesLoading, | ||
...results, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.