1 | import fetch from 'node-fetch';
|
2 |
|
3 | import { formatUsersLocations } from '../../Mapper/Geolocation';
|
4 |
|
5 | const BASE_URL = 'https://geolocations-dev.madbean.ovh';
|
6 |
|
7 | const updateGeolocation = (location) => {
|
8 | const options = {
|
9 | method: 'POST',
|
10 | headers: {
|
11 | 'Content-Type': 'application/json',
|
12 | Accept: 'application/json',
|
13 | },
|
14 | body: JSON.stringify([{
|
15 | latitude: location.lat,
|
16 | longitude: location.lon,
|
17 | key: location.key,
|
18 | }]),
|
19 | };
|
20 |
|
21 | fetch(`${BASE_URL}/${location.collectionID}`, options)
|
22 | .then(data => data.json())
|
23 | .catch(console.error);
|
24 | };
|
25 |
|
26 |
|
27 | const getJSONFromRelativeURL = relativeURL => fetch(`${BASE_URL}${relativeURL}`)
|
28 | .then(res => res.json())
|
29 | .catch(console.error);
|
30 |
|
31 | const getLocation = ({ id, key }) => getJSONFromRelativeURL(`/${id}/${key}`)
|
32 | .then(json => json);
|
33 |
|
34 | const getDistanceLocation = ({ id, key, foreignKey }) => getJSONFromRelativeURL(`/${id}/${key}/${foreignKey}`)
|
35 | .then(json => json);
|
36 |
|
37 | const getCloseUsersLocations = ({
|
38 | id, key, distance, unit,
|
39 | }) => getJSONFromRelativeURL(`/${id}/${key}/${distance}/${unit}`)
|
40 | .then(json => formatUsersLocations(json.data))
|
41 | .catch(console.error);
|
42 |
|
43 | export default {
|
44 | getCloseUsersLocations,
|
45 | getDistanceLocation,
|
46 | getLocation,
|
47 | updateGeolocation,
|
48 | };
|