All files / src/Map MapComponent.js

34.04% Statements 16/47
30% Branches 6/20
33.33% Functions 5/15
32.61% Lines 15/46

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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221          1x           1x   1x                                               1x   1x 1x                             1x       1x                                                                                                                                                                           1x   1x 1x                         1x                                   1x                                                               1x               19x  
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import {camelize, loadJS} from './Utilities/Utils'
 
const BOOL_PROPS_TO_CLASS_NAMES = {
  small: 'rev-Map--small',
  medium: 'rev-Map--medium',
  large: 'rev-Map--large',
}
 
const BOOL_PROPS = Object.keys(BOOL_PROPS_TO_CLASS_NAMES)
 
const evtNames = [
  'bounds_changed',
  'center_changed',
  'click',
  'dblclick',
  'drag',
  'dragend',
  'dragstart',
  'heading_changed',
  'idle',
  'maptypeid_changed',
  'mousemove',
  'mouseout',
  'mouseover',
  'projection_changed',
  'resize',
  'rightclick',
  'tilesloaded',
  'tilt_changed',
  'zoom_changed',
]
 
export default class MapComponent extends Component {
  componentDidMount() {
    const mapScript = window.document.getElementById('mapScript')
 
    Eif (!mapScript) {
      this.initMap()
    } else {
      this.loadMap()
    }
  }
 
  componentWillUnmount() {
    Object.keys(this.listeners).forEach((e) => {
      google.maps.event.removeListener(this.listeners[e])
    })
  }
 
  initMap = async () => {
    // this is added for the examples pruposes
    const apiKey =
      typeof this.props.apiKey === 'string'
        ? this.props.apiKey
        : process.env.GATSBY_apiKey
 
    await loadJS(`https://maps.googleapis.com/maps/api/js?key=${apiKey}`)
    this.loadMap()
  }
 
  loadMap() {
    const propClassNames = BOOL_PROPS.reduce((acc, key) => {
      const value = BOOL_PROPS_TO_CLASS_NAMES[key]
 
      acc[value] = this.props[key]
      return acc
    }, {})
 
    this.newClassName = classNames(propClassNames)
 
    this.mapConfig = {
      apiKey: this.props.apiKey,
      backgroundColor: this.props.backgroundColor,
      center: this.props.center,
      clickableIcons: !!this.props.clickableIcons,
      disableDefaultUI: this.props.disableDefaultUI,
      disableDoubleClickZoom: this.props.disableDoubleClickZoom,
      draggable: this.props.draggable,
      draggableCursor: this.props.draggableCursor,
      draggingCursor: this.props.draggingCursor,
      fullscreenControl: this.props.fullscreenControl,
      gestureHandling: this.props.gestureHandling,
      heading: this.props.heading,
      keyboardShortcuts: this.props.keyboardShortcuts,
      mapTypeControl: this.props.mapTypeControl,
      mapTypeId: this.props.mapTypeId,
      maxZoom: this.props.maxZoom,
      minZoom: this.props.minZoom,
      noClear: this.props.noClear,
      panControl: this.props.panControl,
      rotateControl: this.props.rotateControl,
      scaleControl: this.props.scaleControl,
      scrollwheel: this.props.scrollwheel,
      streetViewControl: this.props.streetViewControl,
      style: this.props.style,
      styles: this.props.styles,
      tilt: this.props.tilt,
      zoom: this.props.zoom,
      zoomControl: this.props.zoomControl,
    }
 
    if (typeof this.props.center === 'string') {
      const geocoder = new google.maps.Geocoder()
 
      geocoder.geocode({address: this.props.center}, (results, status) => {
        if (status === 'OK') {
          this.mapConfig.center = results[0].geometry.location
          this.buildMap()
        } else {
          alert(`Map geocoder problem type: ${status}`)
        }
      })
    } else {
      this.buildMap()
    }
  }
 
  buildMap() {
    this.map = new google.maps.Map(
      document.getElementById('map'),
      this.mapConfig
    )
    this.listeners = {} // attached to MapComponent
    evtNames.forEach((e) => {
      this.listeners[e] = this.map.addListener(e, this.handleEvent(e))
    })
    this.forceUpdate()
    /*  https://reactjs.org/docs/react-component.html#forceupdate
    https://reactjs.org/docs/react-component.html#shouldcomponentupdate */
  }
 
  handleEvent(evt) {
    return (e) => {
      const evtName = `on${camelize(evt)}`
 
      if (this.props[evtName]) {
        this.props[evtName](this.props, this.map, e)
      }
    }
  }
 
  renderChildren() {
    const {children} = this.props
 
    Eif (!children) {
      return ''
    }
    return React.Children.map(children, (child) => {
      if (!child) {
        return ''
      }
      return React.cloneElement(child, {
        map: this.map,
      })
    })
  }
  /* eslint complexity: [2, 4] */
  render() {
    return (
      <div>
        {this.props.apiKey ? '' : <h4>Missing API Key!</h4>}
        {this.props.center ? '' : <h4>Missing center props!</h4>}
        <div
          id="map"
          className={this.newClassName}
          style={this.newClassName ? {} : this.props.style}
        />
        {this.renderChildren()}
      </div>
    )
  }
}
 
/* See Documentation
https://developers.google.com/maps/documentation/javascript/reference/3.exp/map#MapOptions */
 
MapComponent.propTypes = {
  apiKey: PropTypes.string.isRequired,
  backgroundColor: PropTypes.string,
  center: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
  children: PropTypes.array,
  clickableIcons: PropTypes.bool,
  disableDefaultUI: PropTypes.bool,
  disableDoubleClickZoom: PropTypes.bool,
  draggable: PropTypes.bool,
  draggableCursor: PropTypes.string,
  draggingCursor: PropTypes.string,
  fullscreenControl: PropTypes.bool,
  gestureHandling: PropTypes.string,
  heading: PropTypes.number,
  keyboardShortcuts: PropTypes.bool,
  mapTypeControl: PropTypes.bool,
  mapTypeId: PropTypes.string,
  maxZoom: PropTypes.number,
  minZoom: PropTypes.number,
  noClear: PropTypes.bool,
  panControl: PropTypes.bool,
  rotateControl: PropTypes.bool,
  scaleControl: PropTypes.bool,
  scrollwheel: PropTypes.bool,
  streetViewControl: PropTypes.bool,
  style: PropTypes.object,
  styles: PropTypes.array,
  tilt: PropTypes.number,
  zoom: PropTypes.number,
  zoomControl: PropTypes.bool,
}
 
MapComponent.defaultProps = {
  zoom: 14,
  style: {
    width: '55vw',
    height: '55vh',
  },
}
 
evtNames.forEach((e) => (MapComponent.propTypes[camelize(e)] = PropTypes.func))