2019-05-28 14:45:52 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = {};
|
|
|
|
Object.keys( profile ).forEach( id => {
|
|
|
|
data[ id ] = getResource( getResourceName( resourceName, id ) ).data || profile[ id ];
|
|
|
|
} );
|
|
|
|
return data;
|
2019-05-28 14:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const items = {};
|
|
|
|
ids.forEach( id => {
|
|
|
|
items[ id ] = getResource( getResourceName( resourceName, id ) ).data;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getProfileItemsError = getResource => () => {
|
|
|
|
return getResource( 'onboarding-profile' ).error;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isGetProfileItemsRequesting = getResource => () => {
|
|
|
|
const { lastReceived, lastRequested } = getResource( 'onboarding-profile' );
|
|
|
|
|
|
|
|
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
const getJetpackConnectUrl = ( getResource, requireResource ) => (
|
2019-10-16 22:00:47 +00:00
|
|
|
query = {},
|
2019-08-21 06:34:21 +00:00
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
2019-10-16 22:00:47 +00:00
|
|
|
const resourceName = getResourceName( 'jetpack-connect-url', query );
|
|
|
|
return requireResource( requirement, resourceName ).data;
|
2019-08-21 06:34:21 +00:00
|
|
|
};
|
|
|
|
|
2019-10-16 22:00:47 +00:00
|
|
|
const getJetpackConnectUrlError = getResource => ( query = {} ) => {
|
|
|
|
const resourceName = getResourceName( 'jetpack-connect-url', query );
|
|
|
|
return getResource( resourceName ).error;
|
2019-08-21 06:34:21 +00:00
|
|
|
};
|
|
|
|
|
2019-10-16 22:00:47 +00:00
|
|
|
const isGetJetpackConnectUrlRequesting = getResource => ( query = {} ) => {
|
|
|
|
const resourceName = getResourceName( 'jetpack-connect-url', query );
|
|
|
|
const { lastReceived, lastRequested } = getResource( resourceName );
|
2019-08-21 06:34:21 +00:00
|
|
|
|
|
|
|
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPluginInstallations = getResource => plugins => {
|
|
|
|
const resourceName = 'plugin-install';
|
|
|
|
|
|
|
|
const installations = {};
|
|
|
|
plugins.forEach( plugin => {
|
|
|
|
const data = getResource( getResourceName( resourceName, plugin ) ).data;
|
|
|
|
if ( data ) {
|
|
|
|
installations[ plugin ] = data;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return installations;
|
|
|
|
};
|
|
|
|
|
2019-08-29 16:41:04 +00:00
|
|
|
const isJetpackConnected = ( getResource, requireResource ) => (
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
2019-10-07 11:51:25 +00:00
|
|
|
const activePlugins = getSetting(
|
|
|
|
'onboarding',
|
|
|
|
{},
|
|
|
|
ob => requireResource( requirement, 'active-plugins' ).data || ob.activePlugins
|
|
|
|
);
|
2019-08-29 16:41:04 +00:00
|
|
|
|
|
|
|
// Avoid issuing API calls, since Jetpack is obviously not connected.
|
|
|
|
if ( ! activePlugins.includes( 'jetpack' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-07 11:51:25 +00:00
|
|
|
const data = getSetting(
|
|
|
|
'dataEndpoints',
|
|
|
|
{},
|
2019-10-16 22:01:56 +00:00
|
|
|
de => requireResource( requirement, 'jetpack-status' ).data || de.jetpackStatus
|
2019-10-07 11:51:25 +00:00
|
|
|
);
|
2019-10-16 22:01:56 +00:00
|
|
|
|
2019-08-29 16:41:04 +00:00
|
|
|
return ( data && data.isActive ) || false;
|
|
|
|
};
|
|
|
|
|
2019-08-23 12:56:57 +00:00
|
|
|
const getActivePlugins = ( getResource, requireResource ) => (
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
|
|
|
const resourceName = 'active-plugins';
|
|
|
|
const data = requireResource( requirement, resourceName ).data || [];
|
|
|
|
if ( ! data.length ) {
|
2019-10-07 11:51:25 +00:00
|
|
|
return getSetting( 'onboarding', {}, ob => ob.activePlugins || [] );
|
2019-08-23 12:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getActivePluginsError = getResource => () => {
|
|
|
|
return getResource( 'active-plugins' ).error;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isGetActivePluginsRequesting = getResource => () => {
|
|
|
|
const { lastReceived, lastRequested } = getResource( 'active-plugins' );
|
|
|
|
|
|
|
|
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
2019-08-21 06:34:21 +00:00
|
|
|
const getPluginActivations = getResource => plugins => {
|
|
|
|
const resourceName = 'plugin-activate';
|
|
|
|
|
|
|
|
const activations = {};
|
|
|
|
plugins.forEach( plugin => {
|
|
|
|
const data = getResource( getResourceName( resourceName, plugin ) ).data;
|
|
|
|
if ( data ) {
|
|
|
|
activations[ plugin ] = data;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return activations;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPluginActivationErrors = getResource => plugins => {
|
|
|
|
const resourceName = 'plugin-activate';
|
|
|
|
|
|
|
|
const errors = {};
|
|
|
|
plugins.forEach( plugin => {
|
|
|
|
const error = getResource( getResourceName( resourceName, plugin ) ).error;
|
|
|
|
if ( error ) {
|
|
|
|
errors[ plugin ] = error;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getPluginInstallationErrors = getResource => plugins => {
|
|
|
|
const resourceName = 'plugin-install';
|
|
|
|
|
|
|
|
const errors = {};
|
|
|
|
plugins.forEach( plugin => {
|
|
|
|
const error = getResource( getResourceName( resourceName, plugin ) ).error;
|
|
|
|
if ( error ) {
|
|
|
|
errors[ plugin ] = error;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isPluginActivateRequesting = getResource => () => {
|
|
|
|
const { lastReceived, lastRequested } = getResource( 'plugin-activate' );
|
|
|
|
|
|
|
|
if ( ! isNil( lastRequested ) && isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isPluginInstallRequesting = getResource => () => {
|
|
|
|
const { lastReceived, lastRequested } = getResource( 'plugin-install' );
|
|
|
|
|
|
|
|
if ( ! isNil( lastRequested ) && isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
2019-05-28 14:45:52 +00:00
|
|
|
export default {
|
|
|
|
getProfileItems,
|
|
|
|
getProfileItemsError,
|
|
|
|
isGetProfileItemsRequesting,
|
2019-08-21 06:34:21 +00:00
|
|
|
getJetpackConnectUrl,
|
|
|
|
getJetpackConnectUrlError,
|
|
|
|
isGetJetpackConnectUrlRequesting,
|
2019-08-23 12:56:57 +00:00
|
|
|
getActivePlugins,
|
|
|
|
getActivePluginsError,
|
|
|
|
isGetActivePluginsRequesting,
|
2019-08-21 06:34:21 +00:00
|
|
|
getPluginActivations,
|
|
|
|
getPluginInstallations,
|
|
|
|
getPluginInstallationErrors,
|
|
|
|
getPluginActivationErrors,
|
|
|
|
isPluginActivateRequesting,
|
|
|
|
isPluginInstallRequesting,
|
2019-08-29 16:41:04 +00:00
|
|
|
isJetpackConnected,
|
2019-05-28 14:45:52 +00:00
|
|
|
};
|