You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.1 KiB
49 lines
1.1 KiB
import { batchActions } from 'redux-batched-actions';
|
|
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
|
import { set, update, updateItem } from '../baseActions';
|
|
|
|
function createFetchHandler(section, url) {
|
|
return function(payload = {}) {
|
|
return function(dispatch, getState) {
|
|
dispatch(set({ section, isFetching: true }));
|
|
|
|
const {
|
|
id,
|
|
...otherPayload
|
|
} = payload;
|
|
|
|
const { request, abortRequest } = createAjaxRequest({
|
|
url: id == null ? url : `${url}/${id}`,
|
|
data: otherPayload,
|
|
traditional: true
|
|
});
|
|
|
|
request.done((data) => {
|
|
dispatch(batchActions([
|
|
id == null ? update({ section, data }) : updateItem({ section, ...data }),
|
|
|
|
set({
|
|
section,
|
|
isFetching: false,
|
|
isPopulated: true,
|
|
error: null
|
|
})
|
|
]));
|
|
});
|
|
|
|
request.fail((xhr) => {
|
|
dispatch(set({
|
|
section,
|
|
isFetching: false,
|
|
isPopulated: false,
|
|
error: xhr.aborted ? null : xhr
|
|
}));
|
|
});
|
|
|
|
return abortRequest;
|
|
};
|
|
};
|
|
}
|
|
|
|
export default createFetchHandler;
|