2017-01-23 20:04:12 +01:00
|
|
|
import { takeLatest } from 'redux-saga';
|
2017-04-11 11:34:59 +02:00
|
|
|
import { put, select, fork, call } from 'redux-saga/effects';
|
|
|
|
|
import request from 'utils/request';
|
2017-01-23 20:04:12 +01:00
|
|
|
|
|
|
|
|
import {
|
2017-04-11 11:34:59 +02:00
|
|
|
loadedRecord,
|
|
|
|
|
loadedCount,
|
2017-01-23 20:04:12 +01:00
|
|
|
} from './actions';
|
|
|
|
|
|
|
|
|
|
import {
|
2017-04-11 11:34:59 +02:00
|
|
|
LOAD_RECORDS,
|
|
|
|
|
LOAD_COUNT,
|
2017-01-23 20:04:12 +01:00
|
|
|
} from './constants';
|
|
|
|
|
|
2017-03-20 22:08:49 +01:00
|
|
|
import {
|
2017-04-10 16:28:30 +02:00
|
|
|
makeSelectCurrentModelName,
|
2017-04-11 17:35:40 +02:00
|
|
|
makeSelectLimit,
|
2017-04-11 11:34:59 +02:00
|
|
|
makeSelectCurrentPage,
|
2017-04-11 13:44:47 +02:00
|
|
|
makeSelectSort,
|
2017-03-20 22:08:49 +01:00
|
|
|
} from './selectors';
|
|
|
|
|
|
2017-01-23 20:04:12 +01:00
|
|
|
export function* getRecords() {
|
2017-04-10 16:28:30 +02:00
|
|
|
const currentModel = yield select(makeSelectCurrentModelName());
|
2017-04-11 17:35:40 +02:00
|
|
|
const limit = yield select(makeSelectLimit());
|
2017-04-11 11:34:59 +02:00
|
|
|
const currentPage = yield select(makeSelectCurrentPage());
|
2017-04-11 13:44:47 +02:00
|
|
|
const sort = yield select(makeSelectSort());
|
2017-04-11 11:34:59 +02:00
|
|
|
|
|
|
|
|
// Calculate the number of values to be skip
|
2017-04-11 17:35:40 +02:00
|
|
|
const skip = (currentPage - 1) * limit;
|
2017-04-11 11:34:59 +02:00
|
|
|
|
|
|
|
|
// Init `params` object
|
|
|
|
|
const params = {
|
|
|
|
|
skip,
|
2017-04-11 17:35:40 +02:00
|
|
|
limit,
|
2017-04-11 13:44:47 +02:00
|
|
|
sort,
|
2017-04-11 11:34:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const requestURL = `http://localhost:1337/content-manager/explorer/${currentModel}`;
|
|
|
|
|
|
|
|
|
|
// Call our request helper (see 'utils/request')
|
|
|
|
|
const data = yield call(request, requestURL, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
params,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
yield put(loadedRecord(data));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* getCount() {
|
|
|
|
|
const currentModel = yield select(makeSelectCurrentModelName());
|
2017-01-23 20:04:12 +01:00
|
|
|
|
2017-01-28 18:11:54 +01:00
|
|
|
try {
|
|
|
|
|
const opts = {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
mode: 'cors',
|
|
|
|
|
cache: 'default'
|
|
|
|
|
};
|
2017-04-11 11:34:59 +02:00
|
|
|
const response = yield fetch(`http://localhost:1337/content-manager/explorer/${currentModel}/count`, opts);
|
|
|
|
|
|
2017-01-28 18:11:54 +01:00
|
|
|
const data = yield response.json();
|
|
|
|
|
|
2017-04-11 11:34:59 +02:00
|
|
|
yield put(loadedCount(data.count));
|
2017-01-28 18:11:54 +01:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
}
|
2017-01-23 20:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-20 16:22:57 +01:00
|
|
|
// Individual exports for testing
|
|
|
|
|
export function* defaultSaga() {
|
2017-04-11 11:34:59 +02:00
|
|
|
yield fork(takeLatest, LOAD_RECORDS, getRecords);
|
|
|
|
|
yield fork(takeLatest, LOAD_COUNT, getCount);
|
2017-01-20 16:22:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All sagas to be loaded
|
|
|
|
|
export default [
|
|
|
|
|
defaultSaga,
|
|
|
|
|
];
|