2020-03-28 13:48:27 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { apiFetch } from '@wordpress/data-controls';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
2020-08-13 02:05:22 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-28 13:48:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-10-19 15:18:21 +00:00
|
|
|
import TYPES from './action-types';
|
2020-03-28 13:48:27 +00:00
|
|
|
import { API_NAMESPACE } from './constants';
|
|
|
|
|
|
|
|
export function receiveInstalledPlugins( plugins ) {
|
|
|
|
return {
|
2020-10-19 15:18:21 +00:00
|
|
|
type: TYPES.SET_INSTALLED_PLUGINS,
|
2020-03-28 13:48:27 +00:00
|
|
|
plugins,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function receiveActivatingPlugin( pluginSlug ) {
|
|
|
|
return {
|
2020-10-19 15:18:21 +00:00
|
|
|
type: TYPES.SET_ACTIVATING_PLUGIN,
|
2020-03-28 13:48:27 +00:00
|
|
|
pluginSlug,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeActivatingPlugin( pluginSlug ) {
|
|
|
|
return {
|
2020-10-19 15:18:21 +00:00
|
|
|
type: TYPES.REMOVE_ACTIVATING_PLUGIN,
|
2020-03-28 13:48:27 +00:00
|
|
|
pluginSlug,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:17:17 +00:00
|
|
|
export function receiveRecommendedPlugins( plugins, category ) {
|
2020-03-28 13:48:27 +00:00
|
|
|
return {
|
2020-10-19 15:18:21 +00:00
|
|
|
type: TYPES.SET_RECOMMENDED_PLUGINS,
|
2020-06-12 07:17:17 +00:00
|
|
|
data: { plugins, category },
|
2020-03-28 13:48:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:17:17 +00:00
|
|
|
export function receiveBlogPosts( posts, category ) {
|
2020-03-28 13:48:27 +00:00
|
|
|
return {
|
2020-10-19 15:18:21 +00:00
|
|
|
type: TYPES.SET_BLOG_POSTS,
|
2020-06-12 07:17:17 +00:00
|
|
|
data: { posts, category },
|
2020-03-28 13:48:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handleFetchError( error, message ) {
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
createNotice( 'error', message );
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log( error );
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:18:21 +00:00
|
|
|
export function setError( category, error ) {
|
|
|
|
return {
|
|
|
|
type: TYPES.SET_ERROR,
|
|
|
|
category,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:48:27 +00:00
|
|
|
export function* activateInstalledPlugin( pluginSlug ) {
|
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
yield receiveActivatingPlugin( pluginSlug );
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = yield apiFetch( {
|
|
|
|
path: API_NAMESPACE + '/overview/activate-plugin',
|
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
plugin: pluginSlug,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( response ) {
|
2020-07-28 02:32:58 +00:00
|
|
|
yield createNotice(
|
|
|
|
'success',
|
|
|
|
__(
|
|
|
|
'The extension has been successfully activated.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
2020-03-28 13:48:27 +00:00
|
|
|
);
|
|
|
|
// Deliberately load the new plugin data in a new request.
|
|
|
|
yield loadInstalledPluginsAfterActivation( pluginSlug );
|
|
|
|
} else {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
} catch ( error ) {
|
2020-07-28 02:32:58 +00:00
|
|
|
yield handleFetchError(
|
|
|
|
error,
|
|
|
|
__(
|
|
|
|
'There was an error trying to activate the extension.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
2020-03-28 13:48:27 +00:00
|
|
|
yield removeActivatingPlugin( pluginSlug );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* loadInstalledPluginsAfterActivation( activatedPluginSlug ) {
|
|
|
|
try {
|
|
|
|
const response = yield apiFetch( {
|
2020-07-28 02:32:58 +00:00
|
|
|
path: `${ API_NAMESPACE }/overview/installed-plugins`,
|
2020-03-28 13:48:27 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if ( response ) {
|
|
|
|
yield receiveInstalledPlugins( response );
|
|
|
|
yield removeActivatingPlugin( activatedPluginSlug );
|
|
|
|
} else {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
} catch ( error ) {
|
2020-07-28 02:32:58 +00:00
|
|
|
yield handleFetchError(
|
|
|
|
error,
|
|
|
|
__(
|
|
|
|
'There was an error loading installed extensions.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
)
|
|
|
|
);
|
2020-03-28 13:48:27 +00:00
|
|
|
}
|
|
|
|
}
|