All files / src/gadgets/file-attachment edit.js

100% Statements 20/20
100% Branches 11/11
100% Functions 10/10
100% Lines 20/20

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                              6x                       5x 5x       1x 1x       14x 14x 6x   8x                       6x 6x   6x 2x     6x                 1x                         6x       1x                   2x 2x         1x            
/* Copyright © 2005-2017 Kuali, Inc. - All Rights Reserved
 * You may use and modify this code under the terms of the Kuali, Inc.
 * Pre-Release License Agreement. You may not distribute it.
 *
 * You should have received a copy of the Kuali, Inc. Pre-Release License
 * Agreement with this file. If not, please write to license@kuali.co.
 */
 
import AttachmentPreview from './attachment-preview'
import Dropzone from './dropzone'
import PropTypes from 'prop-types'
import { Icon, RaisedButton } from '@kuali/kuali-ui'
import React, { Component } from 'react'
 
export default function (pdfUrl) {
  return class FileAttachmentEdit extends Component {
    static displayName = 'FileAttachmentEdit'
 
    static propTypes = {
      details: PropTypes.object.isRequired,
      onChange: PropTypes.func.isRequired,
      value: PropTypes.object,
      context: PropTypes.object.isRequired,
      maxFileSize: PropTypes.number.isRequired
    }
 
    constructor (props) {
      super(props)
      this.state = { replace: false, preview: false }
    }
 
    onChange = value => {
      this.setState({ replace: false })
      this.props.onChange(value)
    }
 
    render () {
      const { details, value } = this.props
      if (value && value.fileName && !this.state.replace) {
        return this.renderViewControl()
      } else {
        return (
          <Dropzone
            onChange={this.onChange}
            details={details}
            maxFileSize={this.props.maxFileSize}
            multiple={false}
          />
        )
      }
    }
 
    renderViewControl = () => {
      let preview = false
      const { value, readOnly } = this.props
 
      if (this.state.preview) {
        preview = this.renderPreview()
      }
 
      return (
        <div style={{ border: '1px solid #CCC', padding: 15 }}>
          <div style={{ marginBottom: 20 }}>{value.fileName}</div>
          <div style={{ display: 'flex' }}>
            {this.viewAttachment()}
            {!readOnly && (
              <div className='replace'>
                <RaisedButton
                  label='Replace'
                  onClick={() => this.setState({ replace: true })}
                >
                  <Icon name='sync' variant='info' />
                </RaisedButton>
              </div>
            )}
          </div>
          {preview}
        </div>
      )
    }
 
    viewAttachment = () => {
      return (
        <div className='view-attachment'>
          <RaisedButton
            style={{ marginRight: 10 }}
            onClick={() => this.setState({ preview: true })}
            label='View Attachment'
          >
            <Icon name='remove_red_eye' variant='info' />
          </RaisedButton>
        </div>
      )
    }
 
    renderPreview = () => {
      const { value } = this.props
      return (
        <AttachmentPreview
          file={value}
          url={value.url || URL.createObjectURL(value.newFileAttachmentFile)} // eslint-disable-line no-undef
          pdfUrl={pdfUrl}
          onClose={() => this.setState({ preview: false })}
        />
      )
    }
  }
}