2018-02-16 15:39:35 +01:00
|
|
|
import { LOCATION_CHANGE } from 'react-router-redux';
|
|
|
|
import { fork, put, select, take, takeLatest } from 'redux-saga/effects';
|
2018-02-19 13:10:35 +01:00
|
|
|
import { Map } from 'immutable';
|
2018-02-16 15:39:35 +01:00
|
|
|
// 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,
|
2018-02-16 15:39:35 +01:00
|
|
|
getDataSuccess,
|
2018-02-16 14:17:24 +01:00
|
|
|
} from './actions';
|
|
|
|
import {
|
2018-02-19 15:14:32 +01:00
|
|
|
DELETE_DATA,
|
2018-02-16 15:39:35 +01:00
|
|
|
GET_DATA,
|
2018-02-16 14:17:24 +01:00
|
|
|
ON_DROP,
|
|
|
|
ON_SEARCH,
|
|
|
|
} from './constants';
|
2018-02-15 17:46:57 +01:00
|
|
|
import { makeSelectSearch } from './selectors';
|
|
|
|
|
2018-02-19 15:14:32 +01:00
|
|
|
function* dataDelete(action) {
|
|
|
|
try {
|
|
|
|
// const requestURL = `/upload/something/${action.dataToDelete.id}`;
|
|
|
|
yield put(deleteSuccess());
|
|
|
|
} catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 15:39:35 +01:00
|
|
|
function* dataGet() {
|
|
|
|
try {
|
|
|
|
const entriesNumber = 100;
|
|
|
|
const data = [
|
2018-02-19 13:10:35 +01:00
|
|
|
Map({
|
2018-02-16 15:39:35 +01:00
|
|
|
type: 'pdf',
|
|
|
|
hash: '1234',
|
|
|
|
name: 'avatar.pdf',
|
|
|
|
updatedAt: '20/11/2017',
|
|
|
|
size: '24 B',
|
|
|
|
relatedTo: 'John Doe',
|
2018-02-19 15:14:32 +01:00
|
|
|
url: 'https://www.google.com',
|
|
|
|
private: false,
|
2018-02-19 13:10:35 +01:00
|
|
|
}),
|
2018-02-16 15:39:35 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
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;
|
2018-02-19 13:10:35 +01:00
|
|
|
const newFiles = [
|
|
|
|
Map({
|
2018-02-19 14:23:47 +01:00
|
|
|
type: 'mov',
|
|
|
|
hash: `${Math.random()}`,
|
2018-02-19 13:10:35 +01:00
|
|
|
name: 'avatar1.pdf',
|
|
|
|
updatedAt: '20/11/2017',
|
|
|
|
size: '24 B',
|
|
|
|
relatedTo: 'John Doe',
|
2018-02-19 15:14:32 +01:00
|
|
|
url: 'https://www.youtube.com',
|
|
|
|
private: true,
|
2018-02-19 13:10:35 +01:00
|
|
|
}),
|
|
|
|
];
|
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) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 17:46:57 +01:00
|
|
|
function* search() {
|
|
|
|
try {
|
|
|
|
const search = yield select(makeSelectSearch());
|
|
|
|
console.log('will search', search);
|
|
|
|
} catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 12:01:06 +01:00
|
|
|
|
|
|
|
// Individual exports for testing
|
|
|
|
export function* defaultSaga() {
|
2018-02-19 15:14:32 +01:00
|
|
|
yield fork(takeLatest, DELETE_DATA, dataDelete)
|
2018-02-16 14:17:24 +01:00
|
|
|
yield fork(takeLatest, ON_DROP, uploadFiles);
|
2018-02-15 17:46:57 +01:00
|
|
|
yield fork(takeLatest, ON_SEARCH, search);
|
2018-02-16 15:39:35 +01:00
|
|
|
|
|
|
|
const loadDataWatcher = yield fork(takeLatest, GET_DATA, dataGet);
|
|
|
|
|
|
|
|
yield take(LOCATION_CHANGE);
|
|
|
|
|
|
|
|
yield cancel(loadDataWatcher);
|
2018-02-08 12:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// All sagas to be loaded
|
|
|
|
export default defaultSaga;
|