"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.UPDATE_SERIES_TYPE = exports.UPDATE_ORDER_TYPE = exports.SORT_SERIES_ITEM = exports.REMOVE_SERIES_ITEM = exports.REMOVE_ALL_SERIES_ITEMS = exports.EDIT_SERIES_ITEM = exports.ADD_SERIES_ITEM = exports.ADD_BULK_SERIES_ITEMS = void 0;
exports.addBulkSeriesItems = addBulkSeriesItems;
exports.addSeriesItem = addSeriesItem;
exports.editSeriesItem = editSeriesItem;
exports.removeAllSeriesItems = removeAllSeriesItems;
exports.removeSeriesItem = removeSeriesItem;
exports.sortSeriesItems = sortSeriesItems;
exports.updateOrderType = updateOrderType;
exports.updateSeriesType = updateSeriesType;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
require("core-js/modules/es.object.values.js");
require("core-js/modules/es.array.sort.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.array.concat.js");
require("core-js/modules/es.array.splice.js");
require("core-js/modules/es.array.reduce.js");
var _helpers = require("../helpers");
var _utils = require("../../../common/helpers/utils");
/*
* Copyright (C) 2021 Akash Gupta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* eslint-disable no-inline-comments */
var UPDATE_ORDER_TYPE = 'UPDATE_ORDER_TYPE';
exports.UPDATE_ORDER_TYPE = UPDATE_ORDER_TYPE;
var UPDATE_SERIES_TYPE = 'UPDATE_SERIES_TYPE';
exports.UPDATE_SERIES_TYPE = UPDATE_SERIES_TYPE;
var ADD_SERIES_ITEM = 'ADD_SERIES_ITEM';
exports.ADD_SERIES_ITEM = ADD_SERIES_ITEM;
var EDIT_SERIES_ITEM = 'EDIT_SERIES_ITEM';
exports.EDIT_SERIES_ITEM = EDIT_SERIES_ITEM;
var REMOVE_SERIES_ITEM = 'REMOVE_SERIES_ITEM';
exports.REMOVE_SERIES_ITEM = REMOVE_SERIES_ITEM;
var SORT_SERIES_ITEM = 'SORT_SERIES_ITEM';
exports.SORT_SERIES_ITEM = SORT_SERIES_ITEM;
var REMOVE_ALL_SERIES_ITEMS = 'REMOVE_ALL_SERIES_ITEMS';
exports.REMOVE_ALL_SERIES_ITEMS = REMOVE_ALL_SERIES_ITEMS;
var ADD_BULK_SERIES_ITEMS = 'ADD_BULK_SERIES_ITEMS';
exports.ADD_BULK_SERIES_ITEMS = ADD_BULK_SERIES_ITEMS;
/**
* Produces an action indicating that the series type for the series being
* edited should be updated with the provided value.
*
* @param {number} seriesType - The new value to be used for the series type ID.
* @returns {Action} The resulting UPDATE_SERIES_TYPE action.
*/
function updateSeriesType(seriesType) {
return {
payload: {
seriesType: seriesType
},
type: UPDATE_SERIES_TYPE
};
}
/**
* Produces an action indicating that the ordering type for the series being
* edited should be updated with the provided value.
*
* @param {number} newType - The new value to be used for the series type ID.
* @returns {Action} The resulting UPDATE_ORDER_TYPE action.
*/
function updateOrderType(newType) {
return {
payload: {
newType: newType
},
type: UPDATE_ORDER_TYPE
};
}
var nextRowID = 0;
/**
* Produces an action indicating that a row for a new series item should be added
* to the series section. The row is assigned an ID based on an incrementing
* variable existing on the client.
* @param {Relationship} data - The new entity to be added in the list.
* @param {string} rowID - The rowID of the new entity to be added in the list.
* @returns {Action} The resulting ADD_SERIES_ITEM action.
*/
function addSeriesItem(data, rowID) {
return {
payload: {
data: data,
rowID: rowID !== null && rowID !== void 0 ? rowID : "n".concat(nextRowID++)
},
type: ADD_SERIES_ITEM
};
}
/**
* This action creator first sorts the series items object and then pass the
* sorted object in the payload while dispatching the action.
*
* @param {number} oldIndex - Old Position of the series item.
* @param {number} newIndex - New Position of the series item.
* @returns {void}
*/
function sortSeriesItems(oldIndex, newIndex) {
return function (dispatch, getState) {
var state = getState();
var seriesItems = state.get('seriesSection').get('seriesItems');
var orderTypeValue = state.get('seriesSection').get('orderType');
var seriesItemsObject = seriesItems.toJS();
var seriesItemsArray = Object.values(seriesItemsObject);
// Attach the deeply nested attributes to the first level of the each seriesItem object.
(0, _helpers.attachAttribToRelForDisplay)(seriesItemsArray);
if (orderTypeValue === 1) {
// OrderType 1 for Automatic Ordering
seriesItemsArray.sort((0, _utils.sortRelationshipOrdinal)('number')); // sorts the array of series items on number attribute
} else {
seriesItemsArray.sort((0, _utils.sortRelationshipOrdinal)('position')); // sorts the array of series items on position attribute
}
// Before saving the seriesItems to the store, we need to detach back the attributes from each seriesItem object.
seriesItemsArray.forEach(function (seriesItem) {
delete seriesItem.number;
delete seriesItem.position;
});
var sortedSeriesItems = [].concat(seriesItemsArray);
var _sortedSeriesItems$sp = sortedSeriesItems.splice(oldIndex, 1),
_sortedSeriesItems$sp2 = (0, _slicedToArray2.default)(_sortedSeriesItems$sp, 1),
itemToMove = _sortedSeriesItems$sp2[0];
sortedSeriesItems.splice(newIndex, 0, itemToMove);
sortedSeriesItems.forEach(function (seriesItem, index) {
seriesItem.attributes.forEach(function (attribute) {
if (attribute.attributeType === 1) {
// Attribute type 1 for position
attribute.value.textValue = "".concat(index); // assigns the position value to the sorted series item array
}
});
});
var payload = sortedSeriesItems.reduce(function (accumulator, rel) {
accumulator[rel.rowID] = rel;
return accumulator;
}, {});
dispatch({
payload: payload,
type: SORT_SERIES_ITEM
});
};
}
/**
* Produces an action indicating that the attribute value of the entity being
* edited should be updated with the provided value.
*
* @param {Attribute} data - The new attribute value to be used for the entity.
* @param {string} rowID - The ID of the series item that is being edited.
* @returns {Action} The resulting EDIT_SERIES_ITEM action.
*/
function editSeriesItem(data, rowID) {
return {
payload: {
data: data,
nextRowID: "n".concat(nextRowID++),
rowID: rowID
},
type: EDIT_SERIES_ITEM
};
}
/**
* Produces an action indicating that the series item with the provided ID should be
* removed.
* @param {string} rowID - The ID of the series item that is being removed.
* @returns {Action} The resulting REMOVE_SERIES_ITEM action.
*/
function removeSeriesItem(rowID) {
return {
payload: {
rowID: rowID
},
type: REMOVE_SERIES_ITEM
};
}
/**
* Produces an action indicating that all series items should be removed.
*
* @returns {Action} The resulting REMOVE_ALL_SERIES_ITEMS action.
*/
function removeAllSeriesItems() {
return {
payload: {},
type: REMOVE_ALL_SERIES_ITEMS
};
}
/**
* Produces an action indicating that the new series items should replace the old one.
*
* @param {Object} seriesItems - The series items object to be added.
* @returns {Action} The resulting ADD_BULK_SERIES_ITEMS action.
*/
function addBulkSeriesItems(seriesItems) {
return {
payload: seriesItems,
type: ADD_BULK_SERIES_ITEMS
};
}
//# sourceMappingURL=actions.js.map