84 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-01-23 20:04:12 +01:00
import { takeLatest } from 'redux-saga';
2017-05-10 11:41:53 +02:00
import { put, select, fork, call, take, cancel } from 'redux-saga/effects';
import { LOCATION_CHANGE } from 'react-router-redux';
2017-01-23 20:04:12 +01:00
2017-05-16 16:32:54 +02:00
import request from 'utils/request';
2017-01-23 20:04:12 +01:00
import { loadedRecord, loadedCount } from './actions';
2017-05-11 10:54:44 +02:00
import { LOAD_RECORDS, LOAD_COUNT } from './constants';
import {
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,
} from './selectors';
2017-01-23 20:04:12 +01:00
export function* getRecords() {
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) {
2017-05-11 10:54:44 +02:00
window.Strapi.notification.error('An error occurred during records fetch.');
2017-04-11 11:34:59 +02:00
}
}
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',
2017-05-11 10:54:44 +02:00
cache: 'default',
2017-01-28 18:11:54 +01:00
};
2017-05-11 10:54:44 +02:00
const response = yield fetch(
`http://localhost:1337/content-manager/explorer/${currentModel}/count`,
opts
);
2017-04-11 11:34:59 +02:00
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) {
2017-05-11 10:54:44 +02:00
window.Strapi.notification.error(
'An error occurred during count records fetch.'
);
2017-01-28 18:11:54 +01:00
}
2017-01-23 20:04:12 +01:00
}
// Individual exports for testing
export function* defaultSaga() {
2017-05-10 11:41:53 +02:00
const loadRecordsWatcher = yield fork(takeLatest, LOAD_RECORDS, getRecords);
const loudCountWatcher = yield fork(takeLatest, LOAD_COUNT, getCount);
// Suspend execution until location changes
yield take(LOCATION_CHANGE);
yield cancel(loadRecordsWatcher);
yield cancel(loudCountWatcher);
}
// All sagas to be loaded
2017-05-11 10:54:44 +02:00
export default [defaultSaga];