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-10-05 12:01:56 +00:00
|
|
|
import { checkPaymentMethodsCanPay } from '../payment/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' );
|
|
|
|
|
2022-09-01 10:08:42 +00:00
|
|
|
// This is needed to ensure that the payment methods are displayed in the editor
|
2022-08-22 13:56:44 +00:00
|
|
|
if ( isEditor ) {
|
2022-10-07 16:41:44 +00:00
|
|
|
const unsubscribeEditor = subscribe( async () => {
|
2022-08-22 13:56:44 +00:00
|
|
|
await checkPaymentMethodsCanPay();
|
|
|
|
await checkPaymentMethodsCanPay( true );
|
|
|
|
} );
|
|
|
|
|
2022-10-06 12:46:46 +00:00
|
|
|
const unsubscribeInitializePaymentStore = subscribe( async () => {
|
|
|
|
wpDataDispatch( 'wc/store/payment' ).__internalInitializePaymentStore();
|
2022-10-07 16:41:44 +00:00
|
|
|
unsubscribeEditor();
|
2022-10-06 12:46:46 +00:00
|
|
|
unsubscribeInitializePaymentStore();
|
2022-08-22 13:56:44 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|