"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("core-js/modules/web.dom.iterable");

var _formik = require("formik");

var _react = _interopRequireWildcard(require("react"));

var _styled = require("./styled");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * This component will render a set of fields along with a 'Change/Edit' button
 * that toggles an editable state. In its editable state, the fields/values are
 * rendered as form inputs, which can be edited and then saved/persisted.
 */
class EditableSection extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "state", {
      isEditing: false
    });

    _defineProperty(this, "_exists", val => val !== null && typeof val !== 'undefined');

    _defineProperty(this, "_getVal", fieldName => this._exists(this.props.request[fieldName]) ? this.props.request[fieldName] : '');

    _defineProperty(this, "_onClickSave", data => {
      const {
        onChange,
        request
      } = this.props; // Convert all null values received to '',
      // otherwise they will appear in the backend as the 'null' string.

      for (const field in data) {
        if (data[field] === null) data[field] = '';
      }

      onChange(request, data);
      this.setState({
        isEditing: false
      });
    });

    _defineProperty(this, "_toggleEditing", () => {
      this.setState({
        isEditing: !this.state.isEditing
      });
    });
  }

  render() {
    const {
      children,
      fields,
      inputStyle,
      request,
      valueFirst
    } = this.props;
    const {
      isEditing
    } = this.state;
    if (!request) return null;
    return /*#__PURE__*/_react.default.createElement(_formik.Formik // Force Formik to reload initialValues when we update them.
    , {
      enableReinitialize: true,
      initialValues: request,
      onSubmit: this._onClickSave
    }, formikProps => /*#__PURE__*/_react.default.createElement(_formik.Form, null, children, /*#__PURE__*/_react.default.createElement("span", {
      className: "pull-right" // Limit height so that it does not collide with form elements
      // below when actively editing.
      ,
      style: {
        height: '10px',
        marginRight: '15px'
      }
    }, !isEditing ? /*#__PURE__*/_react.default.createElement(_styled.Button, {
      bsSize: "xsmall",
      onClick: this._toggleEditing
    }, "Change") : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_styled.Button, {
      bsSize: "xsmall",
      bsStyle: "link",
      onClick: this._toggleEditing
    }, "Cancel"), /*#__PURE__*/_react.default.createElement(_styled.Button, {
      bsSize: "xsmall",
      bsStyle: "link",
      disabled: !formikProps.dirty,
      type: "submit"
    }, "Save"))), fields.map(f => {
      const input = /*#__PURE__*/_react.default.createElement(InputToggle, {
        fieldName: f.fieldName,
        inputProps: f.inputProps,
        isEditing: isEditing,
        key: f.fieldName,
        options: f.options,
        style: inputStyle,
        value: this._getVal(f.fieldName)
      });

      return /*#__PURE__*/_react.default.createElement(_styled.Para, {
        key: f.fieldName
      }, valueFirst ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, input, " ", f.label) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, f.label, ": ", input));
    })));
  }

}
/**
 * This component renders either the specified value for a given field or, if
 * in an active editing state, the associated field's respective input (e.g.
 * text, number, or select).
 */


exports.default = EditableSection;

class InputToggle extends _react.Component {
  render() {
    const {
      fieldName,
      inputProps,
      isEditing,
      options,
      style,
      value
    } = this.props;

    if (isEditing) {
      if (options) {
        return /*#__PURE__*/_react.default.createElement(_formik.Field, {
          as: "select",
          name: fieldName
        }, Object.keys(options).map(k => /*#__PURE__*/_react.default.createElement("option", {
          value: k
        }, options[k])));
      } else {
        return /*#__PURE__*/_react.default.createElement(_formik.Field, _extends({}, inputProps, {
          name: fieldName,
          style: style
        }));
      }
    }

    return /*#__PURE__*/_react.default.createElement(_styled.Val, null, options ? options[value] : value);
  }

}

module.exports = exports.default;

//# sourceMappingURL=editable-section.js