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.
95 lines
2.3 KiB
95 lines
2.3 KiB
import _ from 'lodash';
|
|
import $ from 'jquery';
|
|
import { batchActions } from 'redux-batched-actions';
|
|
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
|
import getNewSeries from 'Utilities/Series/getNewSeries';
|
|
import * as types from './actionTypes';
|
|
import { set, update, updateItem } from './baseActions';
|
|
|
|
let abortCurrentRequest = null;
|
|
const section = 'addArtist';
|
|
|
|
const addArtistActionHandlers = {
|
|
[types.LOOKUP_ARTIST]: function(payload) {
|
|
return function(dispatch, getState) {
|
|
dispatch(set({ section, isFetching: true }));
|
|
|
|
if (abortCurrentRequest) {
|
|
abortCurrentRequest();
|
|
}
|
|
|
|
const { request, abortRequest } = createAjaxRequest({
|
|
url: '/artist/lookup',
|
|
data: {
|
|
term: payload.term
|
|
}
|
|
});
|
|
|
|
abortCurrentRequest = abortRequest;
|
|
|
|
request.done((data) => {
|
|
dispatch(batchActions([
|
|
update({ 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
|
|
}));
|
|
});
|
|
};
|
|
},
|
|
|
|
[types.ADD_ARTIST]: function(payload) {
|
|
return function(dispatch, getState) {
|
|
dispatch(set({ section, isAdding: true }));
|
|
|
|
const foreignArtistId = payload.foreignArtistId;
|
|
const items = getState().addArtist.items;
|
|
const newSeries = getNewSeries(_.cloneDeep(_.find(items, { foreignArtistId })), payload);
|
|
|
|
const promise = $.ajax({
|
|
url: '/artist',
|
|
method: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify(newSeries)
|
|
});
|
|
|
|
promise.done((data) => {
|
|
dispatch(batchActions([
|
|
updateItem({ section: 'artist', ...data }),
|
|
|
|
set({
|
|
section,
|
|
isAdding: false,
|
|
isAdded: true,
|
|
addError: null
|
|
})
|
|
]));
|
|
});
|
|
|
|
promise.fail((xhr) => {
|
|
dispatch(set({
|
|
section,
|
|
isAdding: false,
|
|
isAdded: false,
|
|
addError: xhr
|
|
}));
|
|
});
|
|
};
|
|
}
|
|
};
|
|
|
|
export default addArtistActionHandlers;
|