# All files / react/IntentIframe IntentIframe.jsx

23.08% Statements 3/13
0% Branches 0/13
0% Functions 0/6
23.08% Lines 3/13

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

    2x

    2x

    2x

     /* global cozy */
    import React from 'react'
    import PropTypes from 'prop-types'
    import Spinner from '../Spinner'
    import styles from './styles.styl'

    const DEFAULT_DATA = {
      // TODO remove `closeable` since it is only there for backward compatibility
      // https://mattermost.cozycloud.cc/test-team/pl/t1iagfhqp3n8mqf3nchp6bxsur
      closeable: false,
      exposeIntentFrameRemoval: true
    }

    class IntentIframe extends React.Component {
      state = { loading: false }

      componentDidMount() {
        const { action, data, type, onCancel, onError, onTerminate } = this.props

        const create = this.props.create || cozy.client.intents.create

        this.setState({ loading: true })
        create(action, type, {
          ...DEFAULT_DATA,
          ...data
        })
          .start(this.intentViewer, this.onFrameLoaded)
          .then(result => {
            result ? onTerminate && onTerminate(result) : onCancel()
          })
          .catch(error => {
            ;(onError && onError(error)) ||
              this.setState({ error: error, loading: false })
          })
      }

      onFrameLoaded = () => {
        this.setState({ loading: false })
      }

      render() {
        const { error, loading } = this.state
        return (
          <div
            ref={intentViewer => (this.intentViewer = intentViewer)}
            className={styles.intentContainer}
            aria-busy={loading}
          >
            {loading && <Spinner size="xxlarge" />}
            {error && (
              <div className={styles.intentContainer__error}>{error.message}</div>
            )}
            {/* intent iframe will be appended here */}
          </div>
        )
      }
    }

    IntentIframe.propTypes = {
      action: PropTypes.string.isRequired,
      create: PropTypes.func,
      type: PropTypes.string.isRequired,
      data: PropTypes.object,
      onCancel: PropTypes.func,
      onError: PropTypes.func,
      onTerminate: PropTypes.func.isRequired
    }

    IntentIframe.defaultProps = {
      data: {}
    }

    export default IntentIframe