84 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 {
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) {
2017-12-20 11:23:50 +01:00
yield new Promise(resolve => {
setTimeout(() => {
resolve();
}, 2500);
});
2017-12-16 17:35:16 +01:00
yield put(downloadPluginSucceeded());
2017-12-20 11:23:50 +01:00
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 {
2017-12-20 11:23:50 +01:00
const opts = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
};
const availablePlugins = yield call(request, 'https://marketplace.strapi.io/plugins', opts);
2017-12-02 13:37:58 +01:00
const supportUs = {
description: 'app.components.InstallPluginPage.plugin.support-us.description',
id: 'support-us',
icon: '',
name: 'buy a t-shirt',
price: 30,
2017-12-02 16:10:18 +01:00
ratings: 5,
2017-12-02 18:46:22 +01:00
isCompatible: true,
2017-12-02 13:37:58 +01:00
};
yield put(getPluginsSucceeded(availablePlugins.concat([supportUs])));
} 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);
}