2019-05-28 14:45:52 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
2019-10-07 11:51:25 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
|
|
|
|
2019-05-28 14:45:52 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { DEFAULT_REQUIREMENT } from '../constants';
|
|
|
|
import { getResourceName } from '../utils';
|
|
|
|
|
|
|
|
const getProfileItems = ( getResource, requireResource ) => (
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
|
|
|
const resourceName = 'onboarding-profile';
|
|
|
|
const ids = requireResource( requirement, resourceName ).data || [];
|
2019-10-07 11:51:25 +00:00
|
|
|
const { profile } = getSetting( 'onboarding', {} );
|
2019-05-28 14:45:52 +00:00
|
|
|
|
|
|
|
if ( ! ids.length ) {
|
2019-12-20 12:58:38 +00:00
|
|
|
const data = {};
|
2020-02-14 02:23:21 +00:00
|
|
|
Object.keys( profile ).forEach( ( id ) => {
|
|
|
|
data[ id ] =
|
|
|
|
getResource( getResourceName( resourceName, id ) ).data ||
|
|
|
|
profile[ id ];
|
2019-12-20 12:58:38 +00:00
|
|
|
} );
|
|
|
|
return data;
|
2019-05-28 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const items = {};
|
2020-02-14 02:23:21 +00:00
|
|
|
ids.forEach( ( id ) => {
|
2019-05-28 14:45:52 +00:00
|
|
|
items[ id ] = getResource( getResourceName( resourceName, id ) ).data;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const getProfileItemsError = ( getResource ) => () => {
|
2019-05-28 14:45:52 +00:00
|
|
|
return getResource( 'onboarding-profile' ).error;
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const isGetProfileItemsRequesting = ( getResource ) => () => {
|
2019-05-28 14:45:52 +00:00
|
|
|
const { lastReceived, lastRequested } = getResource( 'onboarding-profile' );
|
|
|
|
|
|
|
|
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
export default {
|
|
|
|
getProfileItems,
|
|
|
|
getProfileItemsError,
|
|
|
|
isGetProfileItemsRequesting,
|
|
|
|
};
|