Shipping data context: replace address state with useShippingAddress() (https://github.com/woocommerce/woocommerce-blocks/pull/2037)
* Shipping data context: replace address state with useShippingAddress() * Move External dependencies to the top * Remove unnecessary useCallback
This commit is contained in:
parent
6d0ee74157
commit
86215d064d
|
@ -1,18 +1,3 @@
|
||||||
/**
|
|
||||||
* Internal dependencies
|
|
||||||
*/
|
|
||||||
import {
|
|
||||||
ERROR_TYPES,
|
|
||||||
DEFAULT_SHIPPING_ADDRESS,
|
|
||||||
DEFAULT_SHIPPING_CONTEXT_DATA,
|
|
||||||
} from './constants';
|
|
||||||
import {
|
|
||||||
EMIT_TYPES,
|
|
||||||
emitterSubscribers,
|
|
||||||
reducer as emitReducer,
|
|
||||||
emitEvent,
|
|
||||||
} from './event-emit';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* External dependencies
|
* External dependencies
|
||||||
*/
|
*/
|
||||||
|
@ -22,12 +7,26 @@ import {
|
||||||
useState,
|
useState,
|
||||||
useReducer,
|
useReducer,
|
||||||
useEffect,
|
useEffect,
|
||||||
useCallback,
|
|
||||||
useRef,
|
useRef,
|
||||||
} from '@wordpress/element';
|
} from '@wordpress/element';
|
||||||
import { useShippingRates, useStoreCart } from '@woocommerce/base-hooks';
|
import {
|
||||||
|
useShippingAddress,
|
||||||
|
useShippingRates,
|
||||||
|
useStoreCart,
|
||||||
|
} from '@woocommerce/base-hooks';
|
||||||
import { useCheckoutContext } from '@woocommerce/base-context';
|
import { useCheckoutContext } from '@woocommerce/base-context';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal dependencies
|
||||||
|
*/
|
||||||
|
import { ERROR_TYPES, DEFAULT_SHIPPING_CONTEXT_DATA } from './constants';
|
||||||
|
import {
|
||||||
|
EMIT_TYPES,
|
||||||
|
emitterSubscribers,
|
||||||
|
reducer as emitReducer,
|
||||||
|
emitEvent,
|
||||||
|
} from './event-emit';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('@woocommerce/type-defs/contexts').ShippingDataContext} ShippingDataContext
|
* @typedef {import('@woocommerce/type-defs/contexts').ShippingDataContext} ShippingDataContext
|
||||||
*/
|
*/
|
||||||
|
@ -97,9 +96,7 @@ export const ShippingDataProvider = ( { children } ) => {
|
||||||
NONE
|
NONE
|
||||||
);
|
);
|
||||||
const [ observers, subscriber ] = useReducer( emitReducer, {} );
|
const [ observers, subscriber ] = useReducer( emitReducer, {} );
|
||||||
const [ currentShippingAddress, setAddressState ] = useState(
|
const { shippingAddress, setShippingAddress } = useShippingAddress();
|
||||||
DEFAULT_SHIPPING_ADDRESS
|
|
||||||
);
|
|
||||||
const currentObservers = useRef( observers );
|
const currentObservers = useRef( observers );
|
||||||
const [ shippingOptions, setShippingOptions ] = useState( [] );
|
const [ shippingOptions, setShippingOptions ] = useState( [] );
|
||||||
const [ shippingOptionsLoading, setShippingOptionsLoading ] = useState(
|
const [ shippingOptionsLoading, setShippingOptionsLoading ] = useState(
|
||||||
|
@ -108,12 +105,6 @@ export const ShippingDataProvider = ( { children } ) => {
|
||||||
// @todo, this will need wired up to persistence (useSelectedRates?) which
|
// @todo, this will need wired up to persistence (useSelectedRates?) which
|
||||||
// will be setup similar to `useShippingRates` (or maybe in the same hook?)
|
// will be setup similar to `useShippingRates` (or maybe in the same hook?)
|
||||||
const [ selectedRates, setSelectedRates ] = useState( [] );
|
const [ selectedRates, setSelectedRates ] = useState( [] );
|
||||||
const setShippingAddress = useCallback( ( address ) => {
|
|
||||||
setAddressState( ( prevAddress ) => ( {
|
|
||||||
...prevAddress,
|
|
||||||
...address,
|
|
||||||
} ) );
|
|
||||||
}, [] );
|
|
||||||
const onShippingRateSuccess = emitterSubscribers( subscriber ).onSuccess;
|
const onShippingRateSuccess = emitterSubscribers( subscriber ).onSuccess;
|
||||||
const onShippingRateFail = emitterSubscribers( subscriber ).onFail;
|
const onShippingRateFail = emitterSubscribers( subscriber ).onFail;
|
||||||
const onShippingRateSelectSuccess = emitterSubscribers( subscriber )
|
const onShippingRateSelectSuccess = emitterSubscribers( subscriber )
|
||||||
|
@ -195,7 +186,7 @@ export const ShippingDataProvider = ( { children } ) => {
|
||||||
shippingRatesLoading: shippingOptionsLoading,
|
shippingRatesLoading: shippingOptionsLoading,
|
||||||
selectedRates,
|
selectedRates,
|
||||||
setSelectedRates,
|
setSelectedRates,
|
||||||
shippingAddress: currentShippingAddress,
|
shippingAddress,
|
||||||
setShippingAddress,
|
setShippingAddress,
|
||||||
onShippingRateSuccess,
|
onShippingRateSuccess,
|
||||||
onShippingRateFail,
|
onShippingRateFail,
|
||||||
|
@ -206,7 +197,7 @@ export const ShippingDataProvider = ( { children } ) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ShippingRateCalculation
|
<ShippingRateCalculation
|
||||||
address={ currentShippingAddress }
|
address={ shippingAddress }
|
||||||
onChange={ onRateChange }
|
onChange={ onRateChange }
|
||||||
/>
|
/>
|
||||||
<ShippingDataContext.Provider value={ ShippingData }>
|
<ShippingDataContext.Provider value={ ShippingData }>
|
||||||
|
|
Loading…
Reference in New Issue