|
1 | 1 | /* eslint-disable no-undef */ |
2 | 2 | import { instantMeiliSearch } from '@meilisearch/instant-meilisearch' |
3 | | -import injectScript from 'scriptjs' |
| 3 | +import { setOptions, importLibrary } from '@googlemaps/js-api-loader' |
4 | 4 |
|
5 | | -const GOOGLE_API = import.meta.env.GOOGLE_API |
| 5 | +const GOOGLE_API_KEY = import.meta.env.VITE_GOOGLE_MAPS_KEY |
6 | 6 |
|
7 | | -injectScript( |
8 | | - `https://maps.googleapis.com/maps/api/js?v=quarterly&key=${GOOGLE_API}`, |
9 | | - () => { |
10 | | - const search = instantsearch({ |
11 | | - indexName: 'world_cities_geojson', |
12 | | - searchClient: instantMeiliSearch( |
13 | | - 'https://edge.meilisearch.com', |
14 | | - 'a63da4928426f12639e19d62886f621130f3fa9ff3c7534c5d179f0f51c4f303', |
15 | | - {} |
16 | | - ).searchClient, |
17 | | - }) |
| 7 | +setOptions({ |
| 8 | + apiKey: GOOGLE_API_KEY, |
| 9 | + version: 'weekly', |
| 10 | +}) |
18 | 11 |
|
19 | | - search.addWidgets([ |
20 | | - instantsearch.widgets.sortBy({ |
21 | | - container: '#sort-by', |
22 | | - items: [ |
23 | | - { value: 'world_cities_geojson', label: 'Relevant' }, |
24 | | - { |
25 | | - value: 'world_cities_geojson:population:desc', |
26 | | - label: 'Most Populated', |
27 | | - }, |
28 | | - { |
29 | | - value: 'world_cities_geojson:population:asc', |
30 | | - label: 'Least Populated', |
31 | | - }, |
32 | | - ], |
33 | | - }), |
34 | | - instantsearch.widgets.searchBox({ |
35 | | - container: '#searchbox', |
36 | | - }), |
37 | | - instantsearch.widgets.configure({ |
38 | | - hitsPerPage: 20, |
39 | | - }), |
40 | | - // instantsearch.widgets.geoSearch({ |
41 | | - // container: '#maps', |
42 | | - // googleReference: window.google, |
43 | | - // initialZoom: 7, |
44 | | - // initialPosition: { |
45 | | - // lat: 50.655250871381355, |
46 | | - // lng: 4.843585698860502, |
47 | | - // }, |
48 | | - // }), |
49 | | - instantsearch.widgets.infiniteHits({ |
50 | | - container: '#hits', |
51 | | - templates: { |
52 | | - item: ` |
53 | | - <div> |
54 | | - <div class="hit-name"> |
55 | | - City: {{#helpers.highlight}}{ "attribute": "name" }{{/helpers.highlight}} |
56 | | - </div> |
57 | | - <div class="hit-name"> |
58 | | - Country: {{#helpers.highlight}}{ "attribute": "country" }{{/helpers.highlight}} |
59 | | - </div> |
60 | | - <div class="hit-name"> |
61 | | - Population: {{#helpers.highlight}}{ "attribute": "population" }{{/helpers.highlight}} |
62 | | - </div> |
63 | | - </div> |
64 | | - `, |
| 12 | +importLibrary('maps').then(() => { |
| 13 | + const search = instantsearch({ |
| 14 | + indexName: 'world_cities_geojson', |
| 15 | + searchClient: instantMeiliSearch( |
| 16 | + 'https://edge.meilisearch.com', |
| 17 | + 'a63da4928426f12639e19d62886f621130f3fa9ff3c7534c5d179f0f51c4f303', |
| 18 | + {} |
| 19 | + ).searchClient, |
| 20 | + }) |
| 21 | + |
| 22 | + search.addWidgets([ |
| 23 | + instantsearch.widgets.sortBy({ |
| 24 | + container: '#sort-by', |
| 25 | + items: [ |
| 26 | + { value: 'world_cities_geojson', label: 'Relevant' }, |
| 27 | + { |
| 28 | + value: 'world_cities_geojson:population:desc', |
| 29 | + label: 'Most Populated', |
| 30 | + }, |
| 31 | + { |
| 32 | + value: 'world_cities_geojson:population:asc', |
| 33 | + label: 'Least Populated', |
65 | 34 | }, |
66 | | - }), |
67 | | - ]) |
| 35 | + ], |
| 36 | + }), |
| 37 | + instantsearch.widgets.searchBox({ |
| 38 | + container: '#searchbox', |
| 39 | + }), |
| 40 | + instantsearch.widgets.configure({ |
| 41 | + hitsPerPage: 20, |
| 42 | + }), |
| 43 | + instantsearch.widgets.geoSearch({ |
| 44 | + container: '#maps', |
| 45 | + googleReference: window.google, |
| 46 | + initialZoom: 7, |
| 47 | + initialPosition: { |
| 48 | + lat: 50.655250871381355, |
| 49 | + lng: 4.843585698860502, |
| 50 | + }, |
| 51 | + }), |
| 52 | + instantsearch.widgets.infiniteHits({ |
| 53 | + container: '#hits', |
| 54 | + templates: { |
| 55 | + item: ` |
| 56 | + <div> |
| 57 | + <div class="hit-name"> |
| 58 | + City: {{#helpers.highlight}}{ "attribute": "name" }{{/helpers.highlight}} |
| 59 | + </div> |
| 60 | + <div class="hit-name"> |
| 61 | + Country: {{#helpers.highlight}}{ "attribute": "country" }{{/helpers.highlight}} |
| 62 | + </div> |
| 63 | + <div class="hit-name"> |
| 64 | + Population: {{#helpers.highlight}}{ "attribute": "population" }{{/helpers.highlight}} |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + `, |
| 68 | + }, |
| 69 | + }), |
| 70 | + ]) |
68 | 71 |
|
69 | | - search.start() |
70 | | - } |
71 | | -) |
| 72 | + search.start() |
| 73 | +}) |
0 commit comments