"use strict";

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

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

var _reactRouterDom = require("react-router-dom");

var _actionButton = _interopRequireDefault(require("../action-button"));

var _propertyType = _interopRequireDefault(require("../../property-type"));

var _designSystem = require("../../design-system");

var _viewHelpers = _interopRequireDefault(require("../../../../backend/utils/view-helpers"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

class RecordInList extends _react.default.PureComponent {
  constructor(props) {
    super(props);
    const {
      record
    } = props;
    this.handleClick = this.handleClick.bind(this);
    const show = record.recordActions.find(({
      name
    }) => name === 'show');
    const edit = record.recordActions.find(({
      name
    }) => name === 'edit');
    this.actionName = show && show.name || edit && edit.name;
  }

  handleClick(event) {
    const h = new _viewHelpers.default();
    const {
      resource,
      record,
      history
    } = this.props;
    const targetTagName = event.target.tagName.toLowerCase();

    if (this.actionName && targetTagName !== 'a' && targetTagName !== 'button' && targetTagName !== 'svg') {
      const actionUrl = h.recordActionUrl({
        resourceId: resource.id,
        recordId: record.id,
        actionName: this.actionName,
        search: window.location.search
      });
      history.push(actionUrl);
    }
  }

  render() {
    const {
      resource,
      record,
      actionPerformed,
      isLoading,
      onSelect,
      isSelected
    } = this.props;
    const {
      recordActions
    } = record;
    return _react.default.createElement(_designSystem.TableRow, {
      onClick: event => this.handleClick(event)
    }, _react.default.createElement(_designSystem.TableCell, {
      className: isSelected ? 'selected' : 'not-selected'
    }, onSelect && record.bulkActions.length ? _react.default.createElement(_designSystem.CheckBox, {
      onChange: () => onSelect(record),
      checked: isSelected
    }) : null), resource.listProperties.map(property => _react.default.createElement(_designSystem.TableCell, {
      style: {
        cursor: 'pointer'
      },
      key: property.name,
      "data-property-name": property.name
    }, isLoading ? _react.default.createElement(_designSystem.Placeholder, {
      style: {
        height: 14
      }
    }) : _react.default.createElement(_propertyType.default, {
      key: property.name,
      where: "list",
      property: property,
      resource: resource,
      record: record
    }))), _react.default.createElement(_designSystem.TableCell, {
      key: "options"
    }, recordActions.length ? _react.default.createElement(_designSystem.DropDown, null, _react.default.createElement(_designSystem.DropDownTrigger, {
      py: "sm",
      px: "xl"
    }, _react.default.createElement(_designSystem.Icon, {
      icon: "OverflowMenuHorizontal"
    })), _react.default.createElement(_designSystem.DropDownMenu, null, recordActions.map(action => _react.default.createElement(_designSystem.DropDownItem, {
      key: action.name
    }, _react.default.createElement(_actionButton.default, {
      action: action,
      resourceId: resource.id,
      recordId: record.id,
      actionPerformed: actionPerformed
    }, _react.default.createElement(_designSystem.Icon, {
      icon: action.icon
    }), action.label))))) : ''));
  }

}

var _default = (0, _reactRouterDom.withRouter)(RecordInList);

exports.default = _default;