All files / publisher/form media.js

86.11% Statements 31/36
90% Branches 9/10
90.9% Functions 10/11
85.71% Lines 30/35

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                    9x   9x 9x   9x             3x 3x   3x 3x         1x 3x         2x 2x 2x 2x 2x 1x   1x                   2x             12x 1x                     11x       12x 12x 1x   11x       12x                                                                                                                       12x   12x                                       3x             3x                
import React, { Fragment } from "react";
 
import { PropTypes } from "prop-types";
 
import AccordionHelp from "./AccordionHelp";
import SortableMediaList from "./mediaList";
import RenderErrors from "../shared/RenderErrors";
 
class Media extends React.Component {
  constructor(props) {
    super(props);
 
    this.markForDeletion = this.markForDeletion.bind(this);
    this.mediaChanged = this.mediaChanged.bind(this);
 
    this.state = {
      mediaData: props.mediaData,
      errors: {},
    };
  }
 
  componentDidUpdate(prevProps, prevState) {
    const { mediaData } = this.state;
    const { mediaLimit } = this.props;
 
    Eif (prevState.mediaData !== mediaData && mediaData.length <= mediaLimit) {
      this.props.updateState(mediaData);
    }
  }
 
  markForDeletion(key) {
    this.setState({
      mediaData: this.state.mediaData.filter((item) => item.url !== key),
    });
  }
 
  mediaChanged(files) {
    const newMediaData = [...this.state.mediaData];
    const errors = {};
    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      if (file.errors) {
        errors[file.name] = file.errors;
      } else {
        newMediaData.push({
          file,
          url: URL.createObjectURL(file),
          name: file.name,
          type: "screenshot",
          status: "new",
        });
      }
    }
 
    this.setState({
      mediaData: newMediaData,
      errors: errors,
    });
  }
 
  renderOverLimit() {
    if (this.state.mediaData.length > this.props.mediaLimit) {
      return (
        <div className="p-notification--caution">
          <div className="p-notification__content">
            <p className="p-notification__message">
              You have over 5 images uploaded. Not all image will be visible to
              users.
            </p>
          </div>
        </div>
      );
    }
    return false;
  }
 
  renderErrors() {
    const { errors } = this.state;
    if (Object.keys(errors).length > 0) {
      return <RenderErrors errors={errors} />;
    }
    return false;
  }
 
  renderRescrictions() {
    return (
      <AccordionHelp name="image restrictions">
        <ul>
          <li>
            <small>
              Accepted image formats include: <b>GIF, JPEG & PNG files</b>
            </small>
          </li>
          <li>
            <small>
              Min resolution: <b>480 x 480 pixels</b>
            </small>
          </li>
          <li>
            <small>
              Max resolution: <b>3840 x 2160 pixels</b>
            </small>
          </li>
          <li>
            <small>
              Aspect ratio: <b>Between 1:2 and 2:1</b>
            </small>
          </li>
          <li>
            <small>
              File size limit: <b>2MB</b>
            </small>
          </li>
          <li>
            <small>
              Animation min fps: <b>1</b>
            </small>
          </li>
          <li>
            <small>
              Animation max fps: <b>30</b>
            </small>
          </li>
          <li>
            <small>
              Animation max length: <b>40 seconds</b>
            </small>
          </li>
        </ul>
      </AccordionHelp>
    );
  }
 
  onSortEnd(params) {
    const { oldIndex, newIndex } = params;
    const newMediaData = [...this.state.mediaData];
    const moved = newMediaData.splice(oldIndex, 1);
    newMediaData.splice(newIndex, 0, moved[0]);
 
    this.setState({
      mediaData: newMediaData,
    });
  }
 
  render() {
    const mediaList = this.state.mediaData;
 
    return (
      <Fragment>
        {this.renderOverLimit()}
        {this.renderErrors()}
        <SortableMediaList
          distance={10}
          onSortEnd={this.onSortEnd.bind(this)}
          axis="xy"
          mediaData={mediaList}
          mediaLimit={this.props.mediaLimit}
          restrictions={this.props.restrictions}
          markForDeletion={this.markForDeletion}
          mediaChanged={this.mediaChanged}
        />
        {this.renderRescrictions()}
      </Fragment>
    );
  }
}
 
Media.defaultProps = {
  mediaLimit: 5,
  mediaData: [],
  updateState: () => {},
  restrictions: {},
};
 
Media.propTypes = {
  mediaLimit: PropTypes.number,
  mediaData: PropTypes.array,
  updateState: PropTypes.func,
  restrictions: PropTypes.object,
};
 
export { Media as default };