98 lines
2.2 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) {
yield put(downloadPluginSucceeded());
2018-01-04 16:03:34 +01:00
setTimeout(() => {
window.location.reload();
}, 500);
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);
// Add support us card to the plugins list.
2017-12-02 13:37:58 +01:00
const supportUs = {
description: {
short: 'app.components.InstallPluginPage.plugin.support-us.description',
long: 'app.components.InstallPluginPage.plugin.support-us.description',
},
2017-12-02 13:37:58 +01:00
id: 'support-us',
icon: '',
logo: '',
2017-12-02 13:37:58 +01:00
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);
}