2016-09-25 21:54:59 +02:00
|
|
|
/**
|
2016-10-05 14:18:26 +02:00
|
|
|
* Set of asynchronous functions.
|
2016-09-25 21:54:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { take, call, put, select, fork, cancel } from 'redux-saga/effects';
|
|
|
|
import { LOCATION_CHANGE } from 'react-router-redux';
|
2016-10-05 14:18:26 +02:00
|
|
|
import request from 'utils/request';
|
2016-09-26 17:28:40 +02:00
|
|
|
import {
|
|
|
|
LOAD_GENERAL_SETTINGS,
|
|
|
|
UPDATE_GENERAL_SETTINGS,
|
2016-10-04 17:49:40 +02:00
|
|
|
} from 'containers/HomePage/constants';
|
2016-09-27 18:06:35 +02:00
|
|
|
import {
|
|
|
|
generalSettingsLoaded,
|
|
|
|
generalSettingsLoadingError,
|
|
|
|
generalSettingsUpdated,
|
|
|
|
generalSettingsUpdatedError,
|
|
|
|
} from 'containers/HomePage/actions';
|
2016-09-26 18:28:32 +02:00
|
|
|
import {
|
|
|
|
selectName,
|
|
|
|
selectDescription,
|
|
|
|
selectVersion,
|
|
|
|
} from 'containers/HomePage/selectors';
|
2016-10-05 15:15:43 +02:00
|
|
|
import { apiUrl } from '../../app';
|
2016-09-25 21:54:59 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-05 14:18:26 +02:00
|
|
|
* General Settings request/response handler
|
2016-09-25 21:54:59 +02:00
|
|
|
*/
|
|
|
|
export function* getGeneralSettings() {
|
2016-10-05 15:15:43 +02:00
|
|
|
const requestURL = `${apiUrl}/settings/general`;
|
2016-09-25 21:54:59 +02:00
|
|
|
|
|
|
|
// Call our request helper (see 'utils/request')
|
|
|
|
const generalSettings = yield call(request, requestURL);
|
|
|
|
|
|
|
|
if (!generalSettings.err) {
|
|
|
|
yield put(generalSettingsLoaded(generalSettings.data));
|
|
|
|
} else {
|
2016-10-04 17:49:40 +02:00
|
|
|
yield put(generalSettingsLoadingError(generalSettings.err));
|
2016-09-25 21:54:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:18:26 +02:00
|
|
|
/**
|
|
|
|
* Update general settings
|
|
|
|
*/
|
2016-09-26 17:28:40 +02:00
|
|
|
export function* updateGeneralSettings() {
|
|
|
|
const data = {
|
2016-10-04 16:31:52 +02:00
|
|
|
values: {
|
|
|
|
version: yield select(selectVersion()),
|
|
|
|
name: yield select(selectName()),
|
|
|
|
description: yield select(selectDescription()),
|
|
|
|
},
|
|
|
|
type: 'general',
|
2016-09-26 17:28:40 +02:00
|
|
|
};
|
|
|
|
|
2016-10-05 15:15:43 +02:00
|
|
|
const requestURL = `${apiUrl}/settings`;
|
2016-09-26 17:28:40 +02:00
|
|
|
|
|
|
|
// Call our request helper (see 'utils/request')
|
|
|
|
const generalSettings = yield call(
|
|
|
|
request,
|
|
|
|
requestURL, {
|
|
|
|
method: 'PUT',
|
|
|
|
headers: {
|
2016-10-04 17:49:40 +02:00
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
2016-09-26 17:28:40 +02:00
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
}
|
|
|
|
);
|
2016-09-27 18:06:35 +02:00
|
|
|
|
|
|
|
if (!generalSettings.err) {
|
2016-09-30 18:24:46 +02:00
|
|
|
window.Strapi.notification.success('Your settings have successfully updated.');
|
2016-09-27 18:06:35 +02:00
|
|
|
yield put(generalSettingsUpdated(generalSettings.data));
|
|
|
|
} else {
|
2016-10-04 16:31:52 +02:00
|
|
|
window.Strapi.notification.error(generalSettings.err.message || 'An error occurred during settings update.');
|
2016-09-27 18:06:35 +02:00
|
|
|
yield put(generalSettingsUpdatedError(generalSettings.err));
|
|
|
|
}
|
2016-09-26 17:28:40 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 21:54:59 +02:00
|
|
|
/**
|
2016-10-05 14:18:26 +02:00
|
|
|
* Watches for LOAD_GENERAL_SETTINGS action and calls handler
|
2016-09-25 21:54:59 +02:00
|
|
|
*/
|
|
|
|
export function* getGeneralSettingsWatcher() {
|
|
|
|
while (yield take(LOAD_GENERAL_SETTINGS)) {
|
|
|
|
yield call(getGeneralSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:18:26 +02:00
|
|
|
/**
|
|
|
|
* Watches for UPDATE_GENERAL_SETTINGS action and calls handler
|
|
|
|
*/
|
2016-09-26 17:28:40 +02:00
|
|
|
export function* updateGeneralSettingsWatcher() {
|
|
|
|
while (yield take(UPDATE_GENERAL_SETTINGS)) {
|
|
|
|
yield call(updateGeneralSettings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 21:54:59 +02:00
|
|
|
/**
|
|
|
|
* Root saga manages watcher lifecycle
|
|
|
|
*/
|
|
|
|
export function* generalSettingsData() {
|
2016-10-05 14:18:26 +02:00
|
|
|
// Fork watchers so we can continue execution
|
2016-09-25 21:54:59 +02:00
|
|
|
const watcher = yield fork(getGeneralSettingsWatcher);
|
2016-10-04 17:49:40 +02:00
|
|
|
yield fork(updateGeneralSettingsWatcher);
|
2016-09-25 21:54:59 +02:00
|
|
|
|
|
|
|
// Suspend execution until location changes
|
|
|
|
yield take(LOCATION_CHANGE);
|
|
|
|
yield cancel(watcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bootstrap sagas
|
|
|
|
export default [
|
|
|
|
generalSettingsData,
|
|
|
|
];
|