All files ReactFilestack.jsx

95.12% Statements 39/41
86.36% Branches 19/22
100% Functions 9/9
95.12% Lines 39/41

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                    6x 1x                                                       9x 9x                     9x   9x 6x 6x         9x 1x 1x           9x           9x 9x 9x 9x           9x 2x 2x 2x   1x 1x     7x 1x 6x 1x 5x 1x 4x 1x 3x 1x     2x 2x         20x 20x 1x       19x 19x                          
import React, { Component } from 'react';
import * as filestack from 'filestack-js';
import PropTypes from 'prop-types';
 
class ReactFilestack extends Component {
  static defaultProps = {
    file: null,
    link: false,
    buttonText: 'Pick file',
    buttonClass: '',
    onSuccess: result => console.log(result),
    onError: error => console.error(error),
    mode: 'pick',
    options: {},
    security: null,
    children: null,
    render: null,
    cname: null,
    sessionCache: false,
  };
 
  static propTypes = {
    file: PropTypes.objectOf(PropTypes.any),
    apikey: PropTypes.string.isRequired,
    link: PropTypes.bool,
    mode: PropTypes.string,
    buttonText: PropTypes.string,
    buttonClass: PropTypes.string,
    onSuccess: PropTypes.func,
    onError: PropTypes.func,
    options: PropTypes.objectOf(PropTypes.any),
    security: PropTypes.objectOf(PropTypes.any),
    children: PropTypes.node,
    render: PropTypes.func,
    cname: PropTypes.string,
    sessionCache: PropTypes.bool,
  };
 
  onClickPick = (event) => {
    event.stopPropagation();
    event.preventDefault();
    const {
      apikey,
      onSuccess,
      onError,
      options,
      mode,
      file,
      security,
      cname,
      sessionCache,
    } = this.props;
 
    const onFinished = (result) => {
      Eif (typeof onSuccess === 'function') {
        onSuccess(result);
      } else {
        console.log(result);
      }
    };
    const onFail = (error) => {
      Eif (typeof onError === 'function') {
        onError(error);
      } else {
        console.error(error);
      }
    };
 
    this.initClient(mode, apikey, options, file, security, cname, sessionCache)
      .then(onFinished)
      .catch(onFail);
  };
 
  initClient = (mode, apikey, options, file, security, cname, sessionCache) => {
    const { url, handle } = options;
    delete options.handle;
    delete options.url;
    const client = filestack.init(apikey, {
      security,
      cname,
      sessionCache,
    });
 
    if (mode === 'transform') {
      return new Promise((resolve, reject) => {
        try {
          resolve(client.transform(handle, options));
        } catch (err) {
          console.log(err);
          reject(err);
        }
      });
    } else if (mode === 'retrieve') {
      return client.retrieve(handle, options);
    } else if (mode === 'metadata') {
      return client.metadata(handle, options);
    } else if (mode === 'storeUrl') {
      return client.storeURL(url, options);
    } else if (mode === 'upload') {
      return client.upload(file, options);
    } else if (mode === 'remove') {
      return client.remove(handle, security);
    }
 
    return new Promise((resolve) => {
      client.picker({ ...options, onUploadDone: resolve }).open();
    });
  };
 
  render () {
    const { buttonClass, buttonText, link, children, render: CustomRender } = this.props;
    if (CustomRender) {
      return (
        <CustomRender onPick={this.onClickPick} />
      );
    }
    const Tag = link ? 'a' : 'button';
    return (
      <Tag
        name="filestack"
        onClick={this.onClickPick}
        className={buttonClass}
      >
        {children || buttonText}
      </Tag>
    );
  }
}
 
export default ReactFilestack;