103 lines
2.8 KiB
JavaScript
Raw Normal View History

import { LOCATION_CHANGE } from 'react-router-redux';
2018-02-19 13:10:35 +01:00
import { Map } from 'immutable';
2018-02-22 18:24:17 +01:00
import { call, fork, put, select, take, takeLatest } from 'redux-saga/effects';
import request from 'utils/request';
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-16 14:17:24 +01:00
} from './actions';
import {
2018-02-19 15:14:32 +01:00
DELETE_DATA,
GET_DATA,
2018-02-16 14:17:24 +01:00
ON_DROP,
ON_SEARCH,
} from './constants';
2018-02-22 18:24:17 +01:00
import {
2018-02-23 12:21:57 +01:00
makeSelectParams,
2018-02-22 18:24:17 +01:00
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');
2018-02-19 15:14:32 +01: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());
const _start = ( pageParams.page - 1) * pageParams.limit;
const params = {
_limit: pageParams.limit,
_sort: pageParams.sort,
_start,
};
2018-02-22 18:24:17 +01:00
const data = yield [
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-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));
} 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 {
2018-02-22 18:24:17 +01:00
const headers = {
'X-Forwarded-Host': 'strapi',
};
const response = yield call(request, '/upload', { method: 'POST', headers, body: action.formData }, false, false);
const newFiles = response.map(file => Map(file));
2018-02-16 14:17:24 +01:00
yield put(dropSuccess(newFiles));
if (newFiles.length > 1) {
strapi.notification.success({ id: 'upload.notification.dropFile.success' });
} else {
strapi.notification.success({ id: 'upload.notification.dropFiles.success', values: { number: newFiles.length } });
}
} catch(err) {
2018-02-22 12:55:13 +01:00
strapi.notification.error('notification.error');
2018-02-16 14:17:24 +01:00
}
}
function* search() {
try {
const search = yield select(makeSelectSearch());
2018-02-23 15:11:04 +01:00
const requestURL = `/upload/search/${search}`;
const response = yield call(request, requestURL, { method: 'GET' });
console.log(response);
} 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);
const loadDataWatcher = yield fork(takeLatest, GET_DATA, dataGet);
yield take(LOCATION_CHANGE);
yield cancel(loadDataWatcher);
}
// All sagas to be loaded
export default defaultSaga;