87 lines
1.9 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,
getPluginsSucceeded,
} from './actions';
import { DOWNLOAD_PLUGIN, GET_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
export function* pluginsGet() {
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
};
// Retrieve plugins list.
2017-12-20 11:23:50 +01:00
const availablePlugins = yield call(request, 'https://marketplace.strapi.io/plugins', opts);
2018-04-05 15:10:25 +02:00
yield put(getPluginsSucceeded(availablePlugins));
2017-12-02 13:37:58 +01:00
} catch(err) {
strapi.notification.error('notification.error');
}
}
// Individual exports for testing
export default function* defaultSaga() {
const loadPluginsWatcher = yield fork(takeLatest, GET_PLUGINS, pluginsGet);
yield fork(takeLatest, DOWNLOAD_PLUGIN, pluginDownload);
2017-12-02 13:37:58 +01:00
yield take(LOCATION_CHANGE);
yield cancel(loadPluginsWatcher);
}