UNPKG

@bigfishtv/cockpit

Version:

110 lines (96 loc) 3.69 kB
'use strict'; exports.__esModule = true; exports.handleEdit = handleEdit; exports.handleModalFormSave = handleModalFormSave; exports.handleModalFormClose = handleModalFormClose; exports.handleSave = handleSave; var _deepEqual = require('deep-equal'); var _deepEqual2 = _interopRequireDefault(_deepEqual); var _xhrUtils = require('../api/xhrUtils'); var _ModalHost = require('../components/modal/ModalHost'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Takes Component, and formValue. Intended for modals that directly update a formValue selection * @param {React.Component} Component - Modal React Component * @param {ReactForms.Value} formValue - selected formValue instance e.g. formValue.select('for_modal') * @param {Boolean} isNew - new instance/editing existing * @param {Function} onSave - function called on modal save * @param {Function} onClose - function called on modal close */ function handleEdit(Component, formValue) { var isNew = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; var onSave = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : handleModalFormSave; var onClose = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : handleModalFormClose; _ModalHost.modalHandler.add({ Component: Component, props: { formValue: formValue, isNew: isNew, onSave: onSave.bind(this, formValue), onClose: onClose.bind(this, formValue) } }); } /** * Uses oldFormValue instance and updates with value of newForValue * @param {ReactForms.Value} oldFormValue * @param {ReactForms.Value} newFormValue */ /** * Modal Utilities * @module Utilities/modalUtils */ function handleModalFormSave(oldFormValue, newFormValue) { oldFormValue.update(newFormValue.value); } /** * Called on modal form close, if wasn't saved and was a new instance then delete that instance * @param {ReactForms.Value} oldFormValue * @param {ReactForms.Value} newFormValue * @param {Boolean} didSave * @param {Boolean} isNew */ function handleModalFormClose(oldFormValue, newFormValue, didSave, isNew) { if (!didSave && isNew) { var index = oldFormValue.keyPath[oldFormValue.keyPath.length - 1]; var parent = oldFormValue.parent; var Items = parent.value.filter(function (item, i) { return i !== index; }); parent.update(Items); } } /** * Posts data directly to url on modal close if new or edited. Will attempt to smartly generate 'queryUrl' if 'model' is provided * @param {Object} props - Object of props * @param {String} props.subject * @param {String} props.queryUrl * @param {Array} props.data * @param {Array} props.oldValue * @param {Array} props.newValue * @param {Boolean} props.isNew */ function handleSave(props) { var queryUrl = props.queryUrl, data = props.data, oldValue = props.oldValue, newValue = props.newValue, subject = props.subject, isNew = props.isNew; var model = props.model ? props.model : subject ? subject.toLowerCase().replace(' ', '_') + 's' : null; var url = null; if (queryUrl) url = queryUrl;else if (model && isNew) url = '/admin/' + model + '/add.json';else if (model && oldValue.id) url = '/admin/' + model + '/edit/' + oldValue.id + '.json'; if (isNew || !(0, _deepEqual2.default)(oldValue, newValue)) { (0, _xhrUtils.post)({ url: url, subject: props.subject, data: newValue, callback: function callback(newItem) { var newData = isNew ? [].concat(data, [newItem]) : data.map(function (item) { return item.id === oldValue.id ? newItem : item; }); props.callback(newData); } }); } }