All files / publisher/form icon.js

100% Statements 26/26
90% Branches 9/10
100% Functions 6/6
100% Lines 26/26

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                  5x   5x 5x   5x             1x 1x 1x       1x       2x 2x   2x 1x           1x   1x             1x                     8x 8x 1x   7x       8x                                                                   8x 8x   8x   8x                                                                         3x           3x                
import React, { Fragment } from "react";
import PropTypes from "prop-types";
 
import AccordionHelp from "./AccordionHelp";
import FileInput from "./fileInput";
import RenderErrors from "../shared/RenderErrors";
 
class Icon extends React.Component {
  constructor(props) {
    super(props);
 
    this.iconChanged = this.iconChanged.bind(this);
    this.removeIconHandler = this.removeIconHandler.bind(this);
 
    this.state = {
      icon: props.icon,
      errors: {},
    };
  }
 
  removeIconHandler(e) {
    const { updateIcon } = this.props;
    e.preventDefault();
    this.setState({
      icon: {},
    });
 
    updateIcon(null);
  }
 
  iconChanged(files) {
    const { updateIcon } = this.props;
    const iconFile = files[0];
 
    if (iconFile.errors) {
      this.setState({
        errors: {
          [iconFile.name]: iconFile.errors,
        },
      });
    } else {
      const iconURL = URL.createObjectURL(iconFile);
 
      this.setState({
        errors: {},
        icon: {
          url: iconURL,
        },
      });
 
      updateIcon({
        url: iconURL,
        file: iconFile,
        name: iconFile.name,
        status: "new",
        type: "icon",
      });
    }
  }
 
  renderErrors() {
    const { errors } = this.state;
    if (Object.keys(errors).length > 0) {
      return <RenderErrors errors={errors} />;
    }
    return false;
  }
 
  renderRescrictions() {
    return (
      <AccordionHelp name="icon restrictions">
        <ul>
          <li>
            <small>
              Accepted image formats include: <b>PNG, JPEG & SVG files</b>
            </small>
          </li>
          <li>
            <small>
              Min resolution: <b>40 x 40 pixels</b>
            </small>
          </li>
          <li>
            <small>
              Max resolution: <b>512 x 512 pixels</b>
            </small>
          </li>
          <li>
            <small>
              Aspect ratio: <b>1:1</b>
            </small>
          </li>
          <li>
            <small>
              File size limit: <b>256kB</b>
            </small>
          </li>
        </ul>
      </AccordionHelp>
    );
  }
 
  render() {
    const { title, restrictions } = this.props;
    const { icon } = this.state;
 
    const iconUrl = icon ? icon.url : null;
 
    return (
      <Fragment>
        {this.renderErrors()}
        <div className="u-flex u-vertically-center">
          <div className="p-editable-icon" data-tour="listing-icon">
            <FileInput
              restrictions={restrictions}
              className="p-editable-icon__icon"
              inputName="icon"
              inputId="snap-icon"
              fileChangedCallback={this.iconChanged}
              isSmall={true}
            >
              {iconUrl && <img src={iconUrl} alt={`${title} snap`} />}
              <span className="p-editable-icon__change">Edit</span>
            </FileInput>
          </div>
          {iconUrl && (
            <div className="p-editable-icon__actions">
              <a
                href="#"
                role="button"
                tabIndex="0"
                className="p-editable-icon__delete"
                onClick={this.removeIconHandler}
              >
                <i className="p-icon--delete" />
              </a>
            </div>
          )}
        </div>
        {this.renderRescrictions()}
      </Fragment>
    );
  }
}
 
Icon.defaultProps = {
  icon: {},
  title: "Snap icon",
  restrictions: {},
};
 
Icon.propTypes = {
  icon: PropTypes.object,
  title: PropTypes.string,
  updateIcon: PropTypes.func.isRequired,
  restrictions: PropTypes.object,
};
 
export { Icon as default };