|
|
@ -1,10 +1,12 @@
|
|
|
|
import $ from 'jquery';
|
|
|
|
import $ from 'jquery';
|
|
|
|
|
|
|
|
import _ from 'lodash';
|
|
|
|
import { batchActions } from 'redux-batched-actions';
|
|
|
|
import { batchActions } from 'redux-batched-actions';
|
|
|
|
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
|
|
|
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
|
|
|
import getProviderState from 'Utilities/State/getProviderState';
|
|
|
|
import getProviderState from 'Utilities/State/getProviderState';
|
|
|
|
import { set, updateItem } from '../baseActions';
|
|
|
|
import { set, updateItem } from '../baseActions';
|
|
|
|
|
|
|
|
|
|
|
|
const abortCurrentRequests = {};
|
|
|
|
const abortCurrentRequests = {};
|
|
|
|
|
|
|
|
let lastSaveData = null;
|
|
|
|
|
|
|
|
|
|
|
|
export function createCancelSaveProviderHandler(section) {
|
|
|
|
export function createCancelSaveProviderHandler(section) {
|
|
|
|
return function(getState, payload, dispatch) {
|
|
|
|
return function(getState, payload, dispatch) {
|
|
|
@ -26,25 +28,33 @@ function createSaveProviderHandler(section, url, options = {}) {
|
|
|
|
} = payload;
|
|
|
|
} = payload;
|
|
|
|
|
|
|
|
|
|
|
|
const saveData = getProviderState({ id, ...otherPayload }, getState, section);
|
|
|
|
const saveData = getProviderState({ id, ...otherPayload }, getState, section);
|
|
|
|
|
|
|
|
const requestUrl = id ? `${url}/${id}` : url;
|
|
|
|
|
|
|
|
const params = { ...queryParams };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the user is re-saving the same provider without changes
|
|
|
|
|
|
|
|
// force it to be saved. Only applies to editing existing providers.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (id && _.isEqual(saveData, lastSaveData)) {
|
|
|
|
|
|
|
|
params.forceSave = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lastSaveData = saveData;
|
|
|
|
|
|
|
|
|
|
|
|
const ajaxOptions = {
|
|
|
|
const ajaxOptions = {
|
|
|
|
url: `${url}?${$.param(queryParams, true)}`,
|
|
|
|
url: `${requestUrl}?${$.param(params, true)}`,
|
|
|
|
method: 'POST',
|
|
|
|
method: id ? 'PUT' : 'POST',
|
|
|
|
contentType: 'application/json',
|
|
|
|
contentType: 'application/json',
|
|
|
|
dataType: 'json',
|
|
|
|
dataType: 'json',
|
|
|
|
data: JSON.stringify(saveData)
|
|
|
|
data: JSON.stringify(saveData)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
|
|
|
|
ajaxOptions.url = `${url}/${id}?${$.param(queryParams, true)}`;
|
|
|
|
|
|
|
|
ajaxOptions.method = 'PUT';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { request, abortRequest } = createAjaxRequest(ajaxOptions);
|
|
|
|
const { request, abortRequest } = createAjaxRequest(ajaxOptions);
|
|
|
|
|
|
|
|
|
|
|
|
abortCurrentRequests[section] = abortRequest;
|
|
|
|
abortCurrentRequests[section] = abortRequest;
|
|
|
|
|
|
|
|
|
|
|
|
request.done((data) => {
|
|
|
|
request.done((data) => {
|
|
|
|
|
|
|
|
lastSaveData = null;
|
|
|
|
|
|
|
|
|
|
|
|
dispatch(batchActions([
|
|
|
|
dispatch(batchActions([
|
|
|
|
updateItem({ section, ...data }),
|
|
|
|
updateItem({ section, ...data }),
|
|
|
|
|
|
|
|
|
|
|
|