136 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-02-19 13:10:35 +01:00
import { Map } from 'immutable';
import { isEmpty, get, isObject } from 'lodash';
2019-04-16 18:23:26 +02:00
import { all, call, fork, put, select, takeLatest } from 'redux-saga/effects';
import { request } from 'strapi-helper-plugin';
import pluginId from '../../pluginId';
/* eslint-disable */
2018-02-16 14:17:24 +01:00
import {
2018-02-19 15:14:32 +01:00
deleteSuccess,
2018-02-16 14:17:24 +01:00
dropSuccess,
getDataSuccess,
2018-02-23 15:28:01 +01:00
onSearchSuccess,
setLoading,
unsetLoading,
2018-02-16 14:17:24 +01:00
} from './actions';
2019-04-16 18:23:26 +02:00
import { DELETE_DATA, GET_DATA, ON_DROP, ON_SEARCH } from './constants';
import { makeSelectParams, makeSelectSearch } from './selectors';
2018-02-22 18:24:17 +01:00
function* dataDelete(action) {
2018-02-19 15:14:32 +01:00
try {
2018-02-22 18:24:17 +01:00
const dataId = action.dataToDelete.id || action.dataToDelete._id;
const requestURL = `/upload/files/${dataId}`;
yield call(request, requestURL, { method: 'DELETE' });
2018-02-19 15:14:32 +01:00
yield put(deleteSuccess());
2018-02-22 18:24:17 +01:00
strapi.notification.success('upload.notification.delete.success');
2019-04-16 18:23:26 +02:00
} catch (err) {
2018-02-23 12:21:57 +01:00
strapi.notification.error('notification.error');
2018-02-19 15:14:32 +01:00
}
}
function* dataGet() {
try {
2018-02-23 12:21:57 +01:00
const pageParams = yield select(makeSelectParams());
2019-04-16 18:23:26 +02:00
const _start = (pageParams._page - 1) * pageParams._limit;
2018-02-23 12:21:57 +01:00
const params = {
_limit: pageParams._limit,
_sort: pageParams._sort,
2018-02-23 12:21:57 +01:00
_start,
};
2018-11-06 09:56:27 +01:00
const data = yield all([
2018-02-23 12:21:57 +01:00
call(request, '/upload/files', { method: 'GET', params }),
2018-02-22 18:24:17 +01:00
call(request, '/upload/files/count', { method: 'GET' }),
2018-11-06 09:56:27 +01:00
]);
2018-02-23 12:21:57 +01:00
const entries = data[0].length === 0 ? [] : data[0].map(obj => Map(obj));
2018-02-22 18:24:17 +01:00
yield put(getDataSuccess(entries, data[1].count));
2019-04-16 18:23:26 +02:00
} catch (err) {
strapi.notification.error('notification.error');
}
}
2018-02-22 18:24:17 +01:00
function* uploadFiles(action) {
2018-02-16 14:17:24 +01:00
try {
yield put(setLoading());
const headers = {};
2019-04-16 18:23:26 +02:00
const response = yield call(
request,
'/upload',
{ method: 'POST', headers, body: action.formData },
false,
2019-08-21 11:05:33 +02:00
false
2019-04-16 18:23:26 +02:00
);
2018-02-22 18:24:17 +01:00
const newFiles = response.map(file => Map(file));
2018-02-16 14:17:24 +01:00
yield put(dropSuccess(newFiles));
if (newFiles.length > 1) {
2019-04-16 18:23:26 +02:00
strapi.notification.success({
2019-12-29 22:24:25 +01:00
id: 'upload.notification.dropFiles.success',
values: { number: newFiles.length },
2019-04-16 18:23:26 +02:00
});
2018-02-16 14:17:24 +01:00
} else {
2019-04-16 18:23:26 +02:00
strapi.notification.success({
2019-12-29 22:24:25 +01:00
id: 'upload.notification.dropFile.success',
2019-04-16 18:23:26 +02:00
});
2018-02-16 14:17:24 +01:00
}
2019-04-16 18:23:26 +02:00
} catch (error) {
let message = get(error, [
'response',
'payload',
'message',
'0',
'messages',
'0',
]);
if (isObject(message))
message = { ...message, id: `${pluginId}.${message.id}` };
strapi.notification.error(message || 'notification.error');
} finally {
yield put(unsetLoading());
2018-02-16 14:17:24 +01:00
}
}
function* search() {
try {
const search = yield select(makeSelectSearch());
2018-02-23 15:28:01 +01:00
const pageParams = yield select(makeSelectParams());
2019-04-16 18:23:26 +02:00
const _start = (pageParams._page - 1) * pageParams._limit;
const requestURL = !isEmpty(search)
? `/upload/search/${search}`
: '/upload/files';
const params = isEmpty(search)
? {
2019-08-21 11:05:33 +02:00
_limit: pageParams._limit,
_sort: pageParams._sort,
_start,
}
2019-04-16 18:23:26 +02:00
: {};
2018-02-23 15:28:01 +01:00
const response = yield call(request, requestURL, { method: 'GET', params });
const entries = response.length === 0 ? [] : response.map(obj => Map(obj));
2018-02-23 15:28:01 +01:00
yield put(onSearchSuccess(entries));
2019-04-16 18:23:26 +02:00
} catch (err) {
2018-02-22 14:01:24 +01:00
strapi.notification.error('notification.error');
}
}
// Individual exports for testing
export function* defaultSaga() {
2018-02-22 12:08:11 +01:00
yield fork(takeLatest, DELETE_DATA, dataDelete);
2018-02-16 14:17:24 +01:00
yield fork(takeLatest, ON_DROP, uploadFiles);
yield fork(takeLatest, ON_SEARCH, search);
2019-02-11 14:05:06 +01:00
yield fork(takeLatest, GET_DATA, dataGet);
// TODO: Fix router (Other PR)
// const loadDataWatcher = yield fork(takeLatest, GET_DATA, dataGet);
2019-02-11 14:05:06 +01:00
// yield take(LOCATION_CHANGE);
2019-02-11 14:05:06 +01:00
// yield cancel(loadDataWatcher);
}
// All sagas to be loaded
export default defaultSaga;