All files / src react.js

57.69% Statements 30/52
50% Branches 13/26
50% Functions 8/16
57.69% Lines 30/52

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172                                                                                            1x                 1x             6x   6x                       6x           6x 6x 6x 6x 6x 6x   6x                     6x             3x 8x 8x 8x 4x   8x 2x   8x               1x               12x                       12x           12x     12x 8x     4x       1x             1x      
import PropTypes from 'prop-types'
import React from 'react'
 
import Airship from './index'
export {Airship}
 
export class FlagProvider extends React.Component {
  constructor(props) {
    super(props)
 
    this.state = {
      loading: !Airship.environment
    }
 
    if (Airship.environment) {
      this.configure()
    }
  }
 
  async configure() {
    if (!Airship.environment) {
      await Airship.configure({
        envKey: this.props.envKey,
        flagConfig: this.props.flagConfig,
        subscribeToUpdates: this.props.subscribeToUpdates
      })
    }
    if (this.props.entity) {
      Airship.identify(this.props.entity)
    }
  }
 
  async componentDidMount() {
    if (Airship.environment) {
      return
    }
 
    await this.configure()
    this.setState({loading: false})
  }
 
  render() {
    return this.state.loading ? this.props.loadingView : this.props.children
  }
}
 
FlagProvider.propTypes = {
  envKey: PropTypes.string,
  entity: PropTypes.object,
  flagConfig: PropTypes.object,
  loadingView: PropTypes.node,
  subscribeToUpdates: PropTypes.bool,
  children: PropTypes.node
}
 
FlagProvider.defaultProps = {
  children: null,
  loadingView: null,
  subscribeToUpdates: false
}
 
export function withFlag(WrappedComponent, ...flagNames) {
  return class FlaggedComponent extends React.Component {
    componentDidMount() {
      Airship.addGatingInfoListener(this.handleChange)
    }
 
    componentWillUnmount() {
      Airship.removeGatingInfoListener(this.handleChange)
    }
 
    handleChange = () => {
      this.forceUpdate()
    }
 
    render() {
      Iif (flagNames.length === 0) {
        // eslint-disable-next-line no-console
        console.warn('withFlag did not receive a valid flag name')
        return <WrappedComponent flags={{}} {...this.props} />
      }
 
      const flags = flagNames.reduce((flags, flagName) => {
        const flag = Airship.flag(flagName)
        const enabled = flag.isEnabled(this.props.entity)
        const eligible = flag.isEligible(this.props.entity)
        const treatment = flag.getTreatment(this.props.entity)
        const payload = flag.getPayload(this.props.entity)
 
        return {
          ...flags,
          [flagName]: {
            enabled,
            eligible,
            treatment,
            payload
          }
        }
      }, {})
 
      return <WrappedComponent flags={flags} {...this.props} />
    }
  }
}
 
export class FlagSwitch extends React.Component {
  render() {
    return React.Children.map(this.props.children, child => {
      Eif (child.type.prototype instanceof Flag || child.type === Flag) {
        const props = {}
        if (!child.props.flag) {
          props.flag = this.props.flag
        }
        if (!child.props.entity && this.props.entity) {
          props.entity = this.props.entity
        }
        return React.cloneElement(child, props, child.props.children)
      } else {
        return child
      }
    })
  }
}
 
FlagSwitch.propTypes = {
  flag: PropTypes.string.isRequired,
  children: PropTypes.node,
  entity: PropTypes.object
}
 
export class Flag extends React.Component {
  componentDidMount() {
    Airship.addGatingInfoListener(this.handleChange)
  }
 
  componentWillUnmount() {
    Airship.removeGatingInfoListener(this.handleChange)
  }
 
  handleChange = () => {
    this.forceUpdate()
  }
 
  render() {
    Iif (!this.props.flag) {
      // eslint-disable-next-line no-console
      console.warn('<Flag> component missing flag name')
      return null
    }
 
    const treatment = Airship.flag(this.props.flag).getTreatment(
      this.props.entity
    )
    if (treatment !== this.props.case) {
      return null
    }
 
    return this.props.children
  }
}
 
Flag.propTypes = {
  flag: PropTypes.string,
  case: PropTypes.string.isRequired,
  children: PropTypes.node,
  entity: PropTypes.object
}
 
Flag.defaultProps = {
  children: null
}