All files / src/components/DatatableCore/Body BodyActionsCell.js

94.59% Statements 35/37
89.13% Branches 41/46
95.45% Functions 21/22
94.59% Lines 35/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304                                                                                                    37x 37x         37x 3x 3x     3x                                                     46x   46x 46x 46x     46x   46x   46x                               2x           1x               1x                                   2x                           5x                           1x                 3x 3x                           2x                         2x                               2x                             7x 30x 2x   2x 2x 2x 2x 2x       7x 30x                           7x                                                                  
import React, { Component, Fragment } from "react";
import { Tooltip, IconButton, Checkbox, withStyles } from "@material-ui/core";
import {
  Delete as DeleteIcon,
  Create as CreateIcon,
  Save as SaveIcon,
  Clear as ClearIcon,
  DeleteForever as DeleteForeverIcon,
  Queue as QueueIcon
} from "@material-ui/icons";
import { connect } from "react-redux";
import { compose } from "redux";
import {
  addRowEdited as addRowEditedAction,
  saveRowEdited as saveRowEditedAction,
  revertRowEdited as revertRowEditedAction,
  selectRow as selectRowAction,
  deleteRow as deleteRowAction,
  addToDeleteRow as addToDeleteRowAction,
  duplicateRow as duplicateRowAction
} from "../../../redux/actions/datatableActions";
import {
  columnPropType,
  isScrollingPropType,
  canEditPropType,
  canEditRowPropType,
  canDeletePropType,
  canSelectRowPropType,
  rowPropType,
  classesPropType,
  saveRowEditedPropType,
  editingPropType,
  addRowEditedPropType,
  revertRowEditedPropType,
  selectRowPropType,
  checkedPropType,
  stylePropType,
  deleteRowPropType,
  canGlobalEditPropType,
  rowsEditedPropType,
  keyColumnPropType,
  addToDeleteRowPropType,
  additionalActionsPropType,
  canDuplicatePropType,
  duplicateRowPropType
} from "../../../proptypes";
import { customVariant } from "../../MuiTheme";
 
export class BodyActionsCell extends Component {
  constructor(props) {
    super(props);
    this.state = {
      deleting: false
    };
  }
 
  dispatchToDeleteRow = row => {
    const { deleteRow, addToDeleteRow, canGlobalEdit } = this.props;
    Iif (canGlobalEdit) {
      addToDeleteRow(row);
    } else {
      deleteRow(row);
    }
  };
 
  render() {
    const {
      style,
      column,
      isScrolling,
      canEdit,
      canGlobalEdit,
      canDelete,
      canDuplicate,
      canSelectRow,
      row,
      checked,
      editing,
      addRowEdited,
      saveRowEdited,
      revertRowEdited,
      selectRow,
      classes,
      rowsEdited,
      canEditRow,
      keyColumn,
      additionalActions,
      duplicateRow
    } = this.props;
 
    const { deleting } = this.state;
    const { hasBeenEdited, idOfColumnErr } = row;
    const saveDisabled = !hasBeenEdited || idOfColumnErr.length > 0;
 
    const canEditDisable =
      rowsEdited.length > 0 && rowsEdited[0][keyColumn] !== row[keyColumn];
 
    const editableRow = canEditRow ? canEditRow(row) : true;
 
    return (
      <div
        className={
          isScrolling
            ? "Table-Cell action scrolling-shadow"
            : "Table-Cell action"
        }
        style={{
          backgroundColor: style.backgroundColor
        }}
      >
        <div style={{ width: column.colSize }}>
          {canSelectRow && (
            <Checkbox
              className="select"
              color="primary"
              onChange={e => selectRow({ checked: e.target.checked, row })}
              checked={checked}
            />
          )}
 
          {additionalActions.map(aa => (
            <Tooltip title={aa.title} key={aa.title}>
              <span>
                <IconButton
                  className={
                    !editing && canGlobalEdit
                      ? `disabled-icon additional-action-icon`
                      : `additional-action-icon`
                  }
                  onClick={() => aa.onClick(row)}
                  disabled={!editing && canGlobalEdit}
                >
                  {aa.icon}
                </IconButton>
              </span>
            </Tooltip>
          ))}
 
          {canDuplicate && (
            <Tooltip title="Duplicate">
              <span>
                <IconButton
                  className={
                    !editing && canGlobalEdit
                      ? `disabled-icon duplicate-icon`
                      : `duplicate-icon`
                  }
                  onClick={() => duplicateRow(row)}
                  disabled={!editing && canGlobalEdit}
                >
                  <QueueIcon color="primary" />
                </IconButton>
              </span>
            </Tooltip>
          )}
 
          {canDelete && (!editing || canGlobalEdit) && !deleting && (
            <Tooltip title="Confirm delete">
              <span>
                <IconButton
                  className={`delete ${classes.defaultIcon}`}
                  onClick={() => this.setState({ deleting: true })}
                  disabled={!editing && canGlobalEdit}
                >
                  <DeleteIcon />
                </IconButton>
              </span>
            </Tooltip>
          )}
 
          {deleting && (
            <Fragment>
              <Tooltip title="Cancel delete">
                <IconButton
                  className={`cancel-delete ${classes.defaultIcon}`}
                  onClick={() => this.setState({ deleting: false })}
                >
                  <ClearIcon />
                </IconButton>
              </Tooltip>
              <Tooltip title="Confirm delete">
                <IconButton
                  className={`confirm-delete ${classes.errorIcon}`}
                  onClick={() => {
                    this.setState({ deleting: false });
                    this.dispatchToDeleteRow(row);
                  }}
                >
                  <DeleteForeverIcon />
                </IconButton>
              </Tooltip>
            </Fragment>
          )}
 
          {canEdit && editableRow && !editing && !deleting && (
            <Tooltip title="Edit row">
              <IconButton
                className="edit"
                color="primary"
                onClick={() => addRowEdited(row)}
                disabled={canEditDisable}
              >
                <CreateIcon />
              </IconButton>
            </Tooltip>
          )}
 
          {editing && !deleting && !canGlobalEdit && (
            <Fragment>
              <Tooltip title="Clear row">
                <IconButton
                  className={`revert ${classes.errorIcon}`}
                  onClick={() => revertRowEdited(row)}
                >
                  <ClearIcon />
                </IconButton>
              </Tooltip>
              <Tooltip
                title="Save row"
                classes={{
                  popper: saveDisabled
                    ? classes.disabledButtonPopper
                    : classes.enabledButtonPopper
                }}
              >
                <span>
                  <IconButton
                    className={`save ${classes.validIcon}`}
                    onClick={() => saveRowEdited(row)}
                    disabled={saveDisabled}
                  >
                    <SaveIcon />
                  </IconButton>
                </span>
              </Tooltip>
            </Fragment>
          )}
        </div>
      </div>
    );
  }
}
 
const mapDispatchToProps = dispatch => {
  return {
    addRowEdited: row => dispatch(addRowEditedAction(row)),
    addToDeleteRow: row => dispatch(addToDeleteRowAction(row)),
    saveRowEdited: row => dispatch(saveRowEditedAction(row)),
    selectRow: row => dispatch(selectRowAction(row)),
    revertRowEdited: row => dispatch(revertRowEditedAction(row)),
    deleteRow: row => dispatch(deleteRowAction(row)),
    duplicateRow: row => dispatch(duplicateRowAction(row))
  };
};
 
const mapStateToProps = state => {
  return {
    additionalActions: state.datatableReducer.features.additionalActions,
    keyColumn: state.datatableReducer.keyColumn,
    rowsEdited: state.datatableReducer.rowsEdited,
    isScrolling: state.datatableReducer.dimensions.isScrolling,
    canEdit: state.datatableReducer.features.canEdit,
    canEditRow: state.datatableReducer.features.canEditRow,
    canGlobalEdit: state.datatableReducer.features.canGlobalEdit,
    canDelete: state.datatableReducer.features.canDelete,
    canSelectRow: state.datatableReducer.features.canSelectRow,
    canDuplicate: state.datatableReducer.features.canDuplicate
  };
};
 
BodyActionsCell.propTypes = {
  column: columnPropType.isRequired,
  rowsEdited: rowsEditedPropType.isRequired,
  keyColumn: keyColumnPropType.isRequired,
  editing: editingPropType.isRequired,
  classes: classesPropType.isRequired,
  isScrolling: isScrollingPropType.isRequired,
  style: stylePropType.isRequired,
  canEdit: canEditPropType.isRequired,
  canEditRow: canEditRowPropType,
  canDelete: canDeletePropType.isRequired,
  canSelectRow: canSelectRowPropType.isRequired,
  checked: checkedPropType.isRequired,
  row: rowPropType.isRequired,
  saveRowEdited: saveRowEditedPropType,
  addRowEdited: addRowEditedPropType,
  selectRow: selectRowPropType,
  revertRowEdited: revertRowEditedPropType,
  deleteRow: deleteRowPropType,
  addToDeleteRow: addToDeleteRowPropType,
  canGlobalEdit: canGlobalEditPropType.isRequired,
  additionalActions: additionalActionsPropType.isRequired,
  canDuplicate: canDuplicatePropType.isRequired,
  duplicateRow: duplicateRowPropType
};
 
export default compose(
  withStyles(customVariant),
  connect(
    mapStateToProps,
    mapDispatchToProps
  )
)(BodyActionsCell);