89 lines
2.0 KiB
JavaScript
Raw Normal View History

import { LOCATION_CHANGE } from 'react-router-redux';
import { fork, put, select, take, takeLatest } from 'redux-saga/effects';
// import request from 'utils/request';
2018-02-16 14:17:24 +01:00
import {
dropSuccess,
getDataSuccess,
2018-02-16 14:17:24 +01:00
} from './actions';
import {
GET_DATA,
2018-02-16 14:17:24 +01:00
ON_DROP,
ON_SEARCH,
} from './constants';
import { makeSelectSearch } from './selectors';
function* dataGet() {
try {
const entriesNumber = 100;
const data = [
{
type: 'pdf',
hash: '1234',
name: 'avatar.pdf',
updatedAt: '20/11/2017',
size: '24 B',
relatedTo: 'John Doe',
},
];
yield put(getDataSuccess(data, entriesNumber));
// TODO: prepare for API call
// const data = yield [
// call(request, 'PATH', { method: 'GET' }),
// call(request, 'PATH', { method: 'GET' }),
// ];
} catch(err) {
strapi.notification.error('notification.error');
}
}
2018-02-16 14:17:24 +01:00
function* uploadFiles(action) {
try {
const files = action.files;
console.log(files);
2018-02-16 14:17:24 +01:00
const newFiles = Object.keys(files).reduce((acc, current) => {
acc.push(files[current]);
return acc;
}, []);
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) {
console.log(err);
}
}
function* search() {
try {
const search = yield select(makeSelectSearch());
console.log('will search', search);
} catch(err) {
console.log(err);
}
}
// Individual exports for testing
export function* defaultSaga() {
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;