@bigfishtv/cockpit
Version:
185 lines (163 loc) • 7.57 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _dec, _class, _class2, _temp2;
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { withFormValue, Fieldset } from '@bigfishtv/react-forms';
import { connect } from 'react-redux';
import _get from 'lodash/get';
import MainContent from '../container/MainContent';
import Bulkhead from '../page/Bulkhead';
import Panel from '../container/panel/Panel';
import Button from '../button/Button';
import Checkbox from '../input/Checkbox';
import Field from '../form/Field';
import EditForm from '../form/EditForm';
import { notifySuccess, notifyFailure } from '../../actions/notifications';
import { titleCase } from '../../utils/stringUtils';
import { resolveFieldInputFromColumn, filterOutProtectedEditSchema, getSchemaWithAssociations } from '../../utils/formUtils';
var DefaultToolbar = function DefaultToolbar(props) {
return React.createElement(Button, { type: 'submit', text: 'Save', style: 'secondary', size: 'large' });
};
/**
* Used for automatically generating an entity edit page, relies on associations, schema, and a few assumptions.
*/
var AutoEdit = (_dec = connect(), _dec(_class = (_temp2 = _class2 = function (_Component) {
_inherits(AutoEdit, _Component);
function AutoEdit() {
var _temp, _this, _ret;
_classCallCheck(this, AutoEdit);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.getSubmitUrl = function (data) {
if (data.id) {
return '/admin/' + _this.props.model + '/edit/' + data.id + '.json';
} else {
return '/admin/' + _this.props.model + '/add.json';
}
}, _this.handleSubmitSuccess = function (data) {
var _this$props = _this.props,
dispatch = _this$props.dispatch,
model = _this$props.model,
modelLabel = _this$props.modelLabel;
dispatch(notifySuccess('The ' + modelLabel + ' has been saved.'));
history.replaceState(null, null, '/admin/' + model + '/edit/' + data.id);
}, _this.handleSubmitError = function () {
_this.props.dispatch(notifyFailure('The ' + _this.props.modelLabel + ' could not be saved.'));
}, _temp), _possibleConstructorReturn(_this, _ret);
}
AutoEdit.prototype.render = function render() {
var _props = this.props,
schema = _props.schema,
associations = _props.associations,
panels = _props.panels,
modelLabel = _props.modelLabel,
componentResolver = _props.componentResolver,
Toolbar = _props.Toolbar,
props = _objectWithoutProperties(_props, ['schema', 'associations', 'panels', 'modelLabel', 'componentResolver', 'Toolbar']);
var columns = getSchemaWithAssociations(filterOutProtectedEditSchema(schema), associations);
return React.createElement(
MainContent,
null,
React.createElement(
EditForm,
_extends({
submitUrl: this.getSubmitUrl,
onSubmitSuccess: this.handleSubmitSuccess,
onSubmitError: this.handleSubmitError,
Header: withFormValue(function (props) {
return React.createElement(Bulkhead, {
title: _get(props.formValue, 'value.id') ? 'Edit ' + titleCase(modelLabel) : 'New ' + titleCase(modelLabel),
Toolbar: Toolbar
});
})
}, props, {
panels: panels,
associations: associations,
componentResolver: componentResolver,
Sidebar: panels.length ? AutoEditSidebar : null }),
React.createElement(
Panel,
null,
React.createElement(
Fieldset,
null,
columns.map(function (column, i) {
var InputComponent = componentResolver && componentResolver(column) || resolveFieldInputFromColumn(associations, column);
var fieldProps = column.fieldProps || {};
return React.createElement(
Field,
{
key: i,
select: column.property,
autoLabel: InputComponent != Checkbox,
text: InputComponent === Checkbox && column.title },
InputComponent && React.createElement(InputComponent, fieldProps)
);
})
)
)
)
);
};
return AutoEdit;
}(Component), _class2.propTypes = {
/** the lowercase plural model e.g. volunteer_applications */
model: PropTypes.string,
/** to be used for things like buttons e.g. Volunteer Application */
modelLabel: PropTypes.string,
/** array of entity assocations passed in from backend */
associations: PropTypes.array,
/** array of table schema passed in from backend */
schema: PropTypes.array,
/** the entity */
defaultValue: PropTypes.object,
/** function that receives assocations and schema column, returns a component */
componentResolver: PropTypes.func,
/** array of objects, containing schema array and title, these become panels on the right. panel schema shouldn't be in main schema */
panels: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.string,
schema: PropTypes.arrayOf(PropTypes.object)
}))
}, _class2.defaultProps = {
modelLabel: 'item',
associations: [],
schema: [],
panels: [],
componentResolver: null,
Toolbar: DefaultToolbar
}, _temp2)) || _class);
export { AutoEdit as default };
var AutoEditSidebar = withFormValue(function (_ref) {
var panels = _ref.panels,
associations = _ref.associations,
componentResolver = _ref.componentResolver,
props = _objectWithoutProperties(_ref, ['panels', 'associations', 'componentResolver']);
return React.createElement(
'div',
null,
panels.map(function (panel, p) {
return React.createElement(
Panel,
{ key: p, title: panel.title, collapsible: true, uncontrolled: true },
panel.schema.map(function (column, i) {
var InputComponent = componentResolver && componentResolver(column) || resolveFieldInputFromColumn(associations, column);
var fieldProps = column.fieldProps || {};
return React.createElement(
Field,
{
key: i,
select: column.property,
autoLabel: InputComponent != Checkbox,
text: InputComponent === Checkbox && column.title },
InputComponent && React.createElement(InputComponent, fieldProps)
);
})
);
})
);
});