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.
Link shipping form fields to shipping rates to load them. (#1890)
* add plukedAddress util function in order to use it for shallowEqual * refactor useShipping so it accepts and returns the address * refactor fields * fix test and return shippingRates to hook * remove unneeded shippingAddress from ShippingRatesControl * move keys logic to hook * refactor tests again * increase cart size
- Loading branch information
Showing
8 changed files
with
126 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,67 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { useReducer, useEffect } from '@wordpress/element'; | ||
import isShallowEqual from '@wordpress/is-shallow-equal'; | ||
import { useDebounce } from 'use-debounce'; | ||
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useStoreCart } from './use-store-cart'; | ||
|
||
import { pluckAddress } from '../utils'; | ||
/** | ||
* 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. | ||
* This is a custom hook that is wired up to the `wc/store/cart/shipping-rates` route. | ||
* Given a a set of default fields keys, this will handle shipping form state and load | ||
* new rates when certain fields change. | ||
* | ||
* @param {Array} addressFieldsKeys an array containing default fields keys. | ||
* | ||
* @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. | ||
* - updateShipping An action dispatcher to update the shipping address. | ||
* - {Boolean} shippingRatesLoading A boolean indicating whether the shipping | ||
* rates are still loading or not. | ||
* - {Function} setShippingAddress An function that optimistically | ||
* update shipping address and dispatches async rate fetching. | ||
* - {Object} shippingAddress An object containing shipping address. | ||
*/ | ||
export const useShippingRates = () => { | ||
export const useShippingRates = ( addressFieldsKeys ) => { | ||
const { shippingRates } = useStoreCart(); | ||
const results = useSelect( ( select, { dispatch } ) => { | ||
const store = select( storeKey ); | ||
const shippingRatesLoading = store.areShippingRatesLoading(); | ||
const { updateShippingAddress } = dispatch( storeKey ); | ||
|
||
return { | ||
shippingRatesLoading, | ||
updateShippingAddress, | ||
}; | ||
}, [] ); | ||
const addressFields = Object.fromEntries( | ||
addressFieldsKeys.map( ( key ) => [ key, '' ] ) | ||
); | ||
const derivedAddress = shippingRates[ 0 ]?.destination; | ||
const initialAddress = { ...addressFields, ...derivedAddress }; | ||
const shippingAddressReducer = ( state, address ) => ( { | ||
...state, | ||
...address, | ||
} ); | ||
const [ shippingAddress, setShippingAddress ] = useReducer( | ||
shippingAddressReducer, | ||
initialAddress | ||
); | ||
const [ debouncedShippingAddress ] = useDebounce( shippingAddress, 400 ); | ||
const shippingRatesLoading = useSelect( | ||
( select ) => select( storeKey ).areShippingRatesLoading(), | ||
[] | ||
); | ||
const { updateShippingAddress } = useDispatch( storeKey ); | ||
|
||
useEffect( () => { | ||
if ( | ||
! isShallowEqual( | ||
pluckAddress( debouncedShippingAddress ), | ||
pluckAddress( initialAddress ) | ||
) && | ||
debouncedShippingAddress.country | ||
) { | ||
updateShippingAddress( debouncedShippingAddress ); | ||
} | ||
}, [ debouncedShippingAddress ] ); | ||
return { | ||
shippingRates, | ||
...results, | ||
shippingAddress, | ||
setShippingAddress, | ||
shippingRatesLoading, | ||
}; | ||
}; |
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,14 @@ | ||
/** | ||
* pluckAddress takes a full address object and returns relevant fields for calculating | ||
* shipping, so we can track when one of them change to update rates. | ||
* | ||
* @param {Object} address An object containing all address information | ||
* | ||
* @return {Object} pluckedAddress An object containing shipping address that are needed to fetch an address. | ||
*/ | ||
export const pluckAddress = ( { country, state, city, postcode } ) => ( { | ||
country, | ||
state, | ||
city, | ||
postcode, | ||
} ); |
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,2 +1,3 @@ | ||
export * from './errors'; | ||
export * from './price'; | ||
export * from './address'; |
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