All files / publisher/form mediaList.js

100% Statements 22/22
100% Branches 8/8
100% Functions 3/3
100% Lines 21/21

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                      12x 12x 12x   12x 56x 56x   56x   56x 11x 11x 45x 12x         56x 56x 11x     56x                                           12x       12x   12x     13x                                   3x                  
import React, { Fragment } from "react";
 
import { PropTypes } from "prop-types";
 
import MediaItem, { mediaClasses } from "./mediaItem";
import FileInput from "./fileInput";
 
import { SortableContainer } from "react-sortable-hoc";
 
class MediaList extends React.Component {
  renderInputs() {
    const { mediaLimit, restrictions } = this.props;
    const currentMedia = this.props.mediaData.length;
    const inputs = [];
 
    for (let i = 0; i < mediaLimit; i++) {
      const classes = [...mediaClasses];
      classes.push("is-empty");
 
      let isActive = false;
 
      if (i === currentMedia) {
        classes.push("p-listing-images__add-image");
        isActive = true;
      } else if (i < currentMedia) {
        classes.push("u-hide");
      }
 
      // We always want the "add" item to be before any hidden inputs
      // So if it's one of those unshift rather then push
      let method = "push";
      if (isActive) {
        method = "unshift";
      }
 
      inputs[method](
        <Fragment key={`add-screenshot-${i}`}>
          <FileInput
            restrictions={restrictions}
            className={classes.join(" ")}
            inputName="screenshots"
            inputId={`screenshot-${i}`}
            fileChangedCallback={this.props.mediaChanged}
            active={isActive}
          >
            {isActive && (
              <span role="button" className="u-align-text--center">
                <i className="p-icon--plus" />
                <br />
                Add image
              </span>
            )}
          </FileInput>
        </Fragment>,
      );
    }
 
    return inputs;
  }
 
  render() {
    const mediaList = this.props.mediaData;
 
    return (
      <div className="p-listing-images">
        {mediaList.map((item, i) => {
          return (
            <MediaItem
              key={`${item.url}-${i}`}
              index={i}
              url={item.url}
              type={item.type}
              status={item.status}
              markForDeletion={this.props.markForDeletion}
              overflow={i > 4}
            />
          );
        })}
        {this.renderInputs()}
      </div>
    );
  }
}
 
MediaList.propTypes = {
  mediaLimit: PropTypes.number,
  mediaData: PropTypes.array,
  restrictions: PropTypes.object,
  mediaChanged: PropTypes.func,
  markForDeletion: PropTypes.func,
};
 
export default SortableContainer(MediaList);