diff options
Diffstat (limited to 'src/refactor.js')
-rw-r--r-- | src/refactor.js | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/refactor.js b/src/refactor.js new file mode 100644 index 0000000..90c5c35 --- /dev/null +++ b/src/refactor.js @@ -0,0 +1,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(); |