aboutsummaryrefslogtreecommitdiff
path: root/src/refactor.js
blob: 90c5c35c39ab7cbc725b485398b0db0b373c2ac3 (plain)
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
import { algoliasearch } from "algoliasearch";
const ALGOLIA_KEY = "9fb3db0222f7b5aef0e2b30791ee6201";
const INDEX_NAME = "pubfinder";
const client = algoliasearch("YSWWVAX5RB", ALGOLIA_KEY);

const algolia_params = {
  hitsPerPage: 1000,
};

const dataController = (function () {
  const londonBounds = {
    north: 51.532,
    south: 51.478,
    east: -0.072,
    west: -0.16,
  };

  const maxMapSpace = {
    north: 51.74,
    south: 51.27,
    west: -0.51,
    east: 0.23,
  };

  const centralPosition = { lat: 51.508616, lng: -0.125319 };

  return {
    londonBounds,
    centralPosition,
    maxMapSpace,
    getSearchResults: async function () {
      const res = await client.searchSingleIndex({
        indexName: INDEX_NAME,
        searchParams: {
          ...algolia_params,
          insideBoundingBox: [londonBounds],
          attributesToRetrieve: ["_geoloc", "name"],
        },
      });
    },
  };
})();

const interfaceController = (function () {
  let markers = [];
  let map;
  return {
    generateMap: async function (bounds, position, maxSpace) {
      const { Map, Polygon, Rectangle, InfoWindow } =
        await google.maps.importLibrary("maps");
      const { AdvancedMarkerElement } = await google.maps.importLibrary(
        "marker"
      );
      map = new Map(document.getElementById("map"), {
        zoom: 4,
        center: position,
        mapId: "pub_map",
        restriction: {
          latLngBounds: maxSpace,
          strictBounds: true,
        },
      });

      const rectangle = new Rectangle({
        bounds: bounds,
        editable: true,
        draggable: true,
      });

      rectangle.setMap(map);
    },
  };
})();

const controller = (function (dataCTRL, uiCTRL) {
  const initialSetup = function () {
    const londonBounds = dataCTRL.londonBounds;
    const centralPoint = dataCTRL.centralPosition;
    const mapBounds = dataCTRL.maxMapSpace;
    uiCTRL.generateMap(londonBounds, centralPoint, mapBounds);
    dataCTRL.getSearchResults();
  };

  return {
    init: function () {
      initialSetup();
    },
  };
})(dataController, interfaceController);

controller.init();