File size: 3,812 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { LatLngPropType, BoundingBoxPropType } from './propTypes';
import GeoSearchContext from './GeoSearchContext';

class Provider extends Component {
  static propTypes = {
    google: PropTypes.object.isRequired,
    hits: PropTypes.arrayOf(PropTypes.object).isRequired,
    isRefineOnMapMove: PropTypes.bool.isRequired,
    hasMapMoveSinceLastRefine: PropTypes.bool.isRequired,
    isRefineEnable: PropTypes.bool.isRequired,
    refine: PropTypes.func.isRequired,
    toggleRefineOnMapMove: PropTypes.func.isRequired,
    setMapMoveSinceLastRefine: PropTypes.func.isRequired,
    children: PropTypes.func.isRequired,
    position: LatLngPropType,
    currentRefinement: BoundingBoxPropType,
  };

  isPendingRefine = false;
  boundingBox = null;
  previousBoundingBox = null;

  refineWithInstance = (instance) => {
    const { refine } = this.props;

    const bounds = instance.getBounds();

    refine({
      northEast: bounds.getNorthEast().toJSON(),
      southWest: bounds.getSouthWest().toJSON(),
    });
  };

  mapValue = {
    isRefineOnMapMove: this.props.isRefineOnMapMove,
    hasMapMoveSinceLastRefine: this.props.hasMapMoveSinceLastRefine,
    toggleRefineOnMapMove: this.props.toggleRefineOnMapMove,
    setMapMoveSinceLastRefine: this.props.setMapMoveSinceLastRefine,
    refineWithInstance: this.refineWithInstance,
  };

  getMapValue = () => {
    const haveContextValuesChanges =
      this.mapValue.isRefineOnMapMove !== this.props.isRefineOnMapMove ||
      this.mapValue.hasMapMoveSinceLastRefine !==
        this.props.hasMapMoveSinceLastRefine;

    if (haveContextValuesChanges) {
      this.mapValue = {
        ...this.mapValue,
        isRefineOnMapMove: this.props.isRefineOnMapMove,
        hasMapMoveSinceLastRefine: this.props.hasMapMoveSinceLastRefine,
      };
    }

    return this.mapValue;
  };

  createBoundingBoxFromHits(hits) {
    const { google } = this.props;

    const latLngBounds = hits.reduce(
      (acc, hit) => acc.extend(hit._geoloc),
      new google.maps.LatLngBounds()
    );

    return {
      northEast: latLngBounds.getNorthEast().toJSON(),
      southWest: latLngBounds.getSouthWest().toJSON(),
    };
  }

  onChange = () => {
    const { isRefineOnMapMove, isRefineEnable, setMapMoveSinceLastRefine } =
      this.props;

    if (isRefineEnable) {
      setMapMoveSinceLastRefine(true);

      if (isRefineOnMapMove) {
        this.isPendingRefine = true;
      }
    }
  };

  onIdle = ({ instance }) => {
    if (this.isPendingRefine) {
      this.isPendingRefine = false;

      this.refineWithInstance(instance);
    }
  };

  shouldUpdate = () => {
    const { hasMapMoveSinceLastRefine } = this.props;

    return !this.isPendingRefine && !hasMapMoveSinceLastRefine;
  };

  render() {
    const { hits, currentRefinement, children } = this.props;

    // We use this value for differentiate the padding to apply during
    // fitBounds. When we don't have a currenRefinement (boundingBox)
    // we let GoogleMaps compute the automatic padding. But when we
    // provide the currentRefinement we explicitly set the padding
    // to `0` otherwise the map will decrease the zoom on each refine.
    const boundingBoxPadding = !currentRefinement ? undefined : 0;
    const boundingBox =
      !currentRefinement && Boolean(hits.length)
        ? this.createBoundingBoxFromHits(hits)
        : currentRefinement;

    return (
      <GeoSearchContext.Provider value={this.getMapValue()}>
        {children({
          onChange: this.onChange,
          onIdle: this.onIdle,
          shouldUpdate: this.shouldUpdate,
          boundingBox,
          boundingBoxPadding,
        })}
      </GeoSearchContext.Provider>
    );
  }
}

export default Provider;