2022-06-10 16:33:15 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-08-22 13:56:44 +00:00
|
|
|
import {
|
|
|
|
createReduxStore,
|
|
|
|
register,
|
|
|
|
subscribe,
|
|
|
|
select as wpDataSelect,
|
|
|
|
dispatch as wpDataDispatch,
|
|
|
|
} from '@wordpress/data';
|
2022-06-10 16:33:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from './constants';
|
|
|
|
import * as selectors from './selectors';
|
|
|
|
import * as actions from './actions';
|
|
|
|
import reducer from './reducers';
|
2022-06-21 14:09:22 +00:00
|
|
|
import { DispatchFromMap, SelectFromMap } from '../mapped-types';
|
2022-08-22 13:56:44 +00:00
|
|
|
import { checkPaymentMethodsCanPay } from '../payment-methods/check-payment-methods';
|
2022-06-10 16:33:15 +00:00
|
|
|
|
|
|
|
export const config = {
|
|
|
|
reducer,
|
|
|
|
selectors,
|
|
|
|
actions,
|
2022-06-21 14:09:22 +00:00
|
|
|
// TODO: Gutenberg with Thunks was released in WP 6.0. Once 6.1 is released, remove the experimental flag here
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore We pass this in case there is an older version of Gutenberg running.
|
2022-06-10 16:33:15 +00:00
|
|
|
__experimentalUseThunks: true,
|
|
|
|
};
|
|
|
|
|
2022-06-21 14:09:22 +00:00
|
|
|
const store = createReduxStore( STORE_KEY, config );
|
|
|
|
register( store );
|
2022-06-10 16:33:15 +00:00
|
|
|
|
2022-08-22 13:56:44 +00:00
|
|
|
const isEditor = !! wpDataSelect( 'core/editor' );
|
|
|
|
|
|
|
|
if ( isEditor ) {
|
|
|
|
subscribe( async () => {
|
|
|
|
await checkPaymentMethodsCanPay();
|
|
|
|
await checkPaymentMethodsCanPay( true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
const unsubscribeInitializePaymentMethodDataStore = subscribe( async () => {
|
|
|
|
wpDataDispatch(
|
|
|
|
'wc/store/payment-methods'
|
|
|
|
).initializePaymentMethodDataStore();
|
|
|
|
unsubscribeInitializePaymentMethodDataStore();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2022-06-10 16:33:15 +00:00
|
|
|
export const CHECKOUT_STORE_KEY = STORE_KEY;
|
2022-06-21 14:09:22 +00:00
|
|
|
declare module '@wordpress/data' {
|
|
|
|
function dispatch(
|
|
|
|
key: typeof CHECKOUT_STORE_KEY
|
|
|
|
): DispatchFromMap< typeof actions >;
|
2022-06-23 09:15:25 +00:00
|
|
|
function select( key: typeof CHECKOUT_STORE_KEY ): SelectFromMap<
|
|
|
|
typeof selectors
|
|
|
|
> & {
|
2022-06-21 14:09:22 +00:00
|
|
|
hasFinishedResolution: ( selector: string ) => boolean;
|
|
|
|
};
|
|
|
|
}
|