118 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-12-02 13:37:58 +01:00
import { LOCATION_CHANGE } from 'react-router-redux';
import {
2017-12-12 18:30:09 +01:00
call,
2017-12-02 13:37:58 +01:00
cancel,
fork,
put,
select,
2017-12-02 13:37:58 +01:00
take,
takeLatest,
} from 'redux-saga/effects';
2017-12-12 18:30:09 +01:00
import request from 'utils/request';
2017-12-02 13:37:58 +01:00
import { selectLocale } from '../LanguageProvider/selectors';
import {
downloadPluginError,
downloadPluginSucceeded,
2018-08-08 17:25:25 +02:00
getAvailablePluginsSucceeded,
getInstalledPluginsSucceeded,
} from './actions';
2018-08-08 17:25:25 +02:00
import { DOWNLOAD_PLUGIN, GET_AVAILABLE_PLUGINS, GET_INSTALLED_PLUGINS } from './constants';
import { makeSelectPluginToDownload } from './selectors';
export function* pluginDownload() {
try {
const pluginToDownload = yield select(makeSelectPluginToDownload());
2017-12-20 11:23:50 +01:00
const opts = {
method: 'POST',
body: {
plugin: pluginToDownload,
port: window.location.port,
},
};
const response = yield call(request, '/admin/plugins/install', opts, true);
2017-12-16 17:35:16 +01:00
if (response.ok) {
2018-01-04 16:03:34 +01:00
2018-01-15 16:43:08 +01:00
yield new Promise(resolve => {
setTimeout(() => {
resolve();
}, 8000);
});
yield put(downloadPluginSucceeded());
window.location.reload();
2017-12-16 17:35:16 +01:00
}
} catch(err) {
yield put(downloadPluginError());
}
}
2017-12-02 13:37:58 +01:00
2018-08-08 17:25:25 +02:00
export function* getAvailablePlugins() {
2017-12-02 13:37:58 +01:00
try {
// Get current locale.
const locale = yield select(selectLocale());
2017-12-20 11:23:50 +01:00
const opts = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
params: {
lang: locale,
},
2017-12-20 11:23:50 +01:00
};
let availablePlugins;
try {
// Retrieve plugins list.
availablePlugins = yield call(request, 'https://marketplace.strapi.io/plugins', opts);
} catch (e) {
availablePlugins = [];
}
2018-08-08 17:25:25 +02:00
yield put(getAvailablePluginsSucceeded(availablePlugins));
2017-12-02 13:37:58 +01:00
} catch(err) {
strapi.notification.error('notification.error');
}
}
2018-08-08 17:25:25 +02:00
export function* getInstalledPlugins() {
try {
const opts = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
};
2017-12-02 13:37:58 +01:00
2018-08-08 17:25:25 +02:00
let installedPlugins;
try {
// Retrieve plugins list.
installedPlugins = yield call(request, '/admin/plugins', opts);
} catch (e) {
installedPlugins = [];
}
yield put(getInstalledPluginsSucceeded(Object.keys(installedPlugins.plugins)));
} catch(err) {
strapi.notification.error('notification.error');
}
}
2017-12-02 13:37:58 +01:00
// Individual exports for testing
export default function* defaultSaga() {
2018-08-08 17:25:25 +02:00
const loadAvailablePluginsWatcher = yield fork(takeLatest, GET_AVAILABLE_PLUGINS, getAvailablePlugins);
const loadInstalledPluginsWatcher = yield fork(takeLatest, GET_INSTALLED_PLUGINS, getInstalledPlugins);
yield fork(takeLatest, DOWNLOAD_PLUGIN, pluginDownload);
2017-12-02 13:37:58 +01:00
yield take(LOCATION_CHANGE);
2018-08-08 17:25:25 +02:00
yield cancel(loadAvailablePluginsWatcher);
yield cancel(loadInstalledPluginsWatcher);
2017-12-02 13:37:58 +01:00
}