# All files / react/IntentModal IntentModal.jsx

75% Statements 6/8
0% Branches 0/2
66.67% Functions 2/3
75% Lines 6/8

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

    1x
    1x
    21x

    1x

    2x

    2x

     import React, { Component } from 'react'
    import styles from './styles.styl'
    import PropTypes from 'prop-types'

    import once from 'lodash/once'

    import IntentIframe from '../IntentIframe'
    import Modal from '../Modal'

    /**
     * Render a modal for the specified intent.
     *
     * The modal for an intent takes the majority of the viewport.
     */
    class IntentModal extends Component {
      // As dismissAction is passed twice to the modal, both for closing and for
      // intent cancellation, we need to ensure that it is only actually
      // called once.
      // FIXME: this should be fixed by diferenciating dismissAction (for closing
      // modal) and onCancel (for intent cancellation), but it implies deprecating
      // dismissAction first, ensure legacy, prevent regressions, etc.
      dismiss = once(() => {
        const { dismissAction } = this.props
        dismissAction && dismissAction()
      })

      render() {
        const { action, doctype, options, onComplete, onError, create } = this.props
        const modalProps = Object.keys(Modal.propTypes).reduce((props, key) => {
          return { ...props, [key]: this.props[key] }
        }, {})
        return (
          <Modal
            {...modalProps}
            key="modal"
            className={styles.intentModal}
            crossClassName={styles.intentModal__cross}
            dismissAction={this.dismiss}
            overflowHidden
          >
            <IntentIframe
              action={action}
              create={create}
              data={options}
              onCancel={this.dismiss}
              onError={onError}
              onTerminate={onComplete}
              type={doctype}
            />
          </Modal>
        )
      }
    }

    IntentModal.propTypes = {
      ...Modal.propTypes,
      /** What should happen when the intent has completed */
      onComplete: PropTypes.func.isRequired,
      /** What should happen if an intent error occurs */
      onError: PropTypes.func,
      /** Action you want to execute */
      action: PropTypes.string.isRequired,
      /** Doctype on which you want to execute the action */
      doctype: PropTypes.string.isRequired
    }

    IntentModal.defaultProps = { ...Modal.defaultProps }

    export default IntentModal