All files / src/components/Messaging/Attachment index.js

95% Statements 19/20
95.45% Branches 21/22
50% Functions 2/4
94.12% Lines 16/17
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          11x 11x 11x 11x 11x 11x 11x 11x                         11x   7x 6x                   2x 1x 1x                   2x 1x                              
/* eslint-disable no-param-reassign */
import { ATTACHMENT_TYPE, ATTACHMENT_SOURCE } from '../constants';
 
class Attachment {
  constructor(root, props) {
    this.root = root;
    this.props = props;
    this.type = props.type || ATTACHMENT_TYPE.IMAGE;
    this.source = props.source || ATTACHMENT_SOURCE.URL;
    this.reusable = typeof props.reusable === 'boolean' ? props.reusable : true;
    this.url = props.url || false;
    this.attachment_id = props.attachment_id || false;
    this.file = props.file || false;
  }
 
  appendChild() {
    // noop
  }
 
  removeChild() {
    // noop
  }
 
  render(Message) {
    // Todo: validation attachment type
    switch (this.source) {
      case ATTACHMENT_SOURCE.URL:
        if (!this.url) throw new Error('Must provide url for attachment');
        return Message.addAttachment({
          attachment: {
            type: this.type,
            payload: {
              url: this.url,
              is_reusable: this.reusable
            }
          }
        });
      case ATTACHMENT_SOURCE.FILE:
        if (!this.file) throw new Error('Must provide file for attachment');
        Message.useFormdata();
        return Message.addAttachment({
          attachment: {
            type: this.type,
            payload: {
              is_reusable: this.reusable
            }
          },
          filedata: this.file
        });
      case ATTACHMENT_SOURCE.SAVED_ASSET:
        if (!this.attachment_id) throw new Error('Must provide attachment_id for attachment');
        return Message.addAttachment({
          attachment: {
            type: this.type,
            payload: {
              attachment_id: this.attachment_id
            }
          }
        });
      default:
        throw new Error('Source type is not valid');
    }
  }
}
 
export default Attachment;