All files / publisher/form banner.js

100% Statements 39/39
93.75% Branches 15/16
100% Functions 10/10
100% Lines 39/39

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                  7x   7x 7x   7x 7x   7x               2x 2x   2x 1x           1x   1x             1x                     2x   2x         2x       1x 1x         2x 2x           13x 13x 1x   12x       13x 13x 13x   13x 13x 6x     13x                                                                                       13x                                                                                           13x 13x 13x 1x   13x                   3x           3x                  
import React, { Fragment } from "react";
import PropTypes from "prop-types";
 
import FileInput from "../form/fileInput";
import AccordionHelp from "./AccordionHelp";
import RenderErrors from "../shared/RenderErrors";
 
class Banner extends React.Component {
  constructor(props) {
    super(props);
 
    this.bannerImageChangeHandler = this.bannerImageChangeHandler.bind(this);
    this.handleRemoveImageClick = this.handleRemoveImageClick.bind(this);
 
    this.keyboardEvent = this.keyboardEvent.bind(this);
    this.toggleFocus = this.toggleFocus.bind(this);
 
    this.state = {
      bannerImage: this.props.bannerImage,
      focused: false,
      errors: {},
    };
  }
 
  bannerImageChangeHandler(files) {
    const { updateImageState } = this.props;
    const file = files[0];
 
    if (file.errors) {
      this.setState({
        errors: {
          [file.name]: file.errors,
        },
      });
    } else {
      const url = URL.createObjectURL(file);
 
      this.setState({
        errors: {},
        bannerImage: {
          url: url,
        },
      });
 
      updateImageState({
        url: url,
        file,
        name: file.name,
        status: "new",
        type: "banner",
      });
    }
  }
 
  handleRemoveImageClick() {
    const { updateImageState } = this.props;
 
    this.setState({
      errors: {},
      bannerImage: {},
    });
 
    updateImageState(null);
  }
 
  keyboardEvent(e) {
    Eif (e.key === "Delete" || e.key === "Backspace") {
      this.handleRemoveImageClick();
    }
  }
 
  toggleFocus() {
    const { focused } = this.state;
    this.setState({
      focused: !focused,
    });
  }
 
  renderErrors() {
    const { errors } = this.state;
    if (Object.keys(errors).length > 0) {
      return <RenderErrors errors={errors} />;
    }
    return false;
  }
 
  renderBackground() {
    const { bannerImage } = this.state;
    const { restrictions } = this.props;
    const background = bannerImage.url;
 
    let backgroundClasses = ["p-market-banner__image"];
    if (!background) {
      backgroundClasses.push("is-empty");
    }
 
    return (
      <Fragment>
        <FileInput
          inputName="banner-image"
          inputId="banner-image"
          active={true}
          fileChangedCallback={this.bannerImageChangeHandler}
          noFocus={true}
          restrictions={restrictions}
        >
          <div
            className={backgroundClasses.join(" ")}
            tabIndex="0"
            onKeyDown={this.keyboardEvent}
            onFocus={this.toggleFocus}
            onBlur={this.toggleFocus}
          >
            {background ? (
              <img src={background} />
            ) : (
              <div className="u-align-text--center">
                <i className="p-icon--plus" />
                Add banner
              </div>
            )}
          </div>
        </FileInput>
        {background && (
          <Fragment>
            <span
              className="p-market-banner__remove p-market-remove"
              role="button"
              onClick={this.handleRemoveImageClick}
            >
              <i className="p-icon--delete" />
            </span>
            <div className="p-market-banner__change">Edit banner</div>
          </Fragment>
        )}
      </Fragment>
    );
  }
 
  renderHelp() {
    return (
      <Fragment>
        <p className="p-form-help-text">
          Adding a featured banner will increase your chances of being featured
          on snapcraft.io and in GNOME software but does not immediately make
          you eligible to be featured.
        </p>
        <AccordionHelp name="banner restrictions">
          <ul>
            <li>
              <small>
                Accepted image formats include: <b>JPEG & PNG files</b>
              </small>
            </li>
            <li>
              <small>
                Min resolution: <b>720 x 240 pixels</b>
                <br />
                <i>
                  For best results on <a href="/store">the store</a> we
                  recommend a resolution of <b>1920 x 640 pixels</b> or greater
                </i>
              </small>
            </li>
            <li>
              <small>
                Max resolution: <b>4320 x 1440 pixels</b>
              </small>
            </li>
            <li>
              <small>
                Aspect ratio: <b>3:1</b>
              </small>
            </li>
            <li>
              <small>
                File size limit: <b>2MB</b>
              </small>
            </li>
          </ul>
        </AccordionHelp>
      </Fragment>
    );
  }
 
  render() {
    const { focused } = this.state;
    const classNames = ["p-market-banner__image-holder"];
    if (focused) {
      classNames.push("is-focused");
    }
    return (
      <Fragment>
        {this.renderErrors()}
        <div className={classNames.join(" ")}>{this.renderBackground()}</div>
        {this.renderHelp()}
      </Fragment>
    );
  }
}
 
Banner.defaultProps = {
  bannerImage: {},
  restrictions: {},
  updateImageState: () => {},
};
 
Banner.propTypes = {
  bannerImage: PropTypes.shape({
    url: PropTypes.string,
  }),
  restrictions: PropTypes.object,
  updateImageState: PropTypes.func,
};
 
export { Banner as default };