2018-11-29 15:57:49 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { registerGenericStore } from '@wordpress/data';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import createApiClient from './create-api-client';
|
|
|
|
import wcApiSpec from '../wc-api-spec';
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( process.env.NODE_ENV === 'development' ) {
|
2019-04-09 14:55:01 +00:00
|
|
|
window.__FRESH_DATA_DEV_INFO__ = true;
|
|
|
|
}
|
|
|
|
|
2018-11-29 15:57:49 +00:00
|
|
|
function createWcApiStore() {
|
|
|
|
const apiClient = createApiClient( 'wc-api', wcApiSpec );
|
|
|
|
|
|
|
|
function getComponentSelectors( component ) {
|
|
|
|
const componentRequirements = [];
|
|
|
|
|
2018-12-07 23:04:52 +00:00
|
|
|
return {
|
|
|
|
selectors: apiClient.getSelectors( componentRequirements ),
|
|
|
|
onComplete: () => {
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( componentRequirements.length === 0 ) {
|
2018-12-07 23:04:52 +00:00
|
|
|
apiClient.clearComponentRequirements( component );
|
|
|
|
} else {
|
2020-02-14 02:23:21 +00:00
|
|
|
apiClient.setComponentRequirements(
|
|
|
|
component,
|
|
|
|
componentRequirements
|
|
|
|
);
|
2018-12-07 23:04:52 +00:00
|
|
|
}
|
|
|
|
},
|
2019-04-09 14:56:25 +00:00
|
|
|
onUnmount: () => {
|
|
|
|
apiClient.clearComponentRequirements( component );
|
|
|
|
},
|
2018-12-07 23:04:52 +00:00
|
|
|
};
|
2018-11-29 15:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
// The wrapped function for getSelectors is temporary code.
|
|
|
|
//
|
2019-02-06 06:41:53 +00:00
|
|
|
// @todo Remove the `() =>` after the `@wordpress/data` PR is merged:
|
2018-11-29 15:57:49 +00:00
|
|
|
// https://github.com/WordPress/gutenberg/pull/11460
|
|
|
|
//
|
2020-02-14 02:23:21 +00:00
|
|
|
getSelectors: () => ( context ) => {
|
|
|
|
const component =
|
|
|
|
context && context.component ? context.component : context;
|
2018-11-29 15:57:49 +00:00
|
|
|
return getComponentSelectors( component );
|
|
|
|
},
|
|
|
|
getActions() {
|
2018-12-13 19:24:54 +00:00
|
|
|
const mutations = apiClient.getMutations();
|
|
|
|
return mutations;
|
2018-11-29 15:57:49 +00:00
|
|
|
},
|
|
|
|
subscribe: apiClient.subscribe,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
registerGenericStore( 'wc-api', createWcApiStore() );
|