2019-06-16 13:09:54 +00:00
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
2020-03-29 08:23:08 +00:00
|
|
|
var db = require('./persistences/db.js');
|
2019-06-16 13:09:54 +00:00
|
|
|
import I18n from 'react-native-i18n'
|
2020-03-29 08:23:08 +00:00
|
|
|
import { readUser } from './AuthApi'
|
|
|
|
import { isDebugMode, MARKER_URL } from "./IlinkConstants";
|
2019-06-16 13:09:54 +00:00
|
|
|
|
2020-03-29 08:23:08 +00:00
|
|
|
let GEOCODDING_URL = "https://maps.googleapis.com/maps/api/geocode/json"
|
|
|
|
let API_KEY = "AIzaSyAYOEp-Pckvc3TwOIulCICokKgmp14rGHI"
|
|
|
|
export const getLocalMarkers = () => {
|
|
|
|
return db.markers.get_all()
|
|
|
|
}
|
|
|
|
export const getMakersFrom = function (country) {
|
|
|
|
let tag = { tag: 'location', type: 'all', country: country }
|
|
|
|
return timeout(15000, queryMap(tag))
|
2019-06-16 13:09:54 +00:00
|
|
|
}
|
|
|
|
function timeout(ms, promise) {
|
2020-03-29 08:23:08 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
setTimeout(function () {
|
|
|
|
reject(new Error(I18n.t("TEXT_NETWORK_UNABLE")))
|
|
|
|
}, ms)
|
|
|
|
promise.then(resolve, reject)
|
|
|
|
})
|
2019-06-16 13:09:54 +00:00
|
|
|
}
|
2020-03-29 08:23:08 +00:00
|
|
|
export const loadMarkers = (position, reseau, distance, page) => {
|
|
|
|
return readUser().then((users) => {
|
|
|
|
let user = users;
|
|
|
|
if (user !== undefined) {
|
|
|
|
let tag = {
|
|
|
|
tag: 'location', type: 'around',
|
|
|
|
page: page,
|
|
|
|
id: user.id, position: position, network: reseau, distance: distance
|
|
|
|
}
|
2019-06-16 13:09:54 +00:00
|
|
|
return queryMap(tag)
|
2020-03-29 08:23:08 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-06-16 13:09:54 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 08:23:08 +00:00
|
|
|
export const loadGroupeAgent = (member_code) => {
|
|
|
|
let data = { tag: 'location', type: 'all_network', codeparrain: member_code }
|
|
|
|
return queryMap(data)
|
2019-06-16 13:09:54 +00:00
|
|
|
}
|
2020-03-29 08:23:08 +00:00
|
|
|
export const loadNetwork = (network, userid) => {
|
|
|
|
let data = { tag: 'location', type: "all_network_point", id: userid, network: network }
|
|
|
|
return queryMap(data)
|
2019-06-16 13:09:54 +00:00
|
|
|
}
|
2020-03-29 08:23:08 +00:00
|
|
|
export const getPositionInformation = async (position) => {
|
|
|
|
let url = GEOCODDING_URL.concat('?latlng=' + position.latitude + "," + position.longitude + '&key=' + API_KEY);
|
|
|
|
const data = fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((responseJson) =>
|
|
|
|
responseJson
|
|
|
|
).catch((error) => {
|
|
|
|
console.log("error get position")
|
|
|
|
console.log(error)
|
|
|
|
});
|
2019-06-16 13:09:54 +00:00
|
|
|
|
2020-03-29 08:23:08 +00:00
|
|
|
console.warn("freud", data);
|
|
|
|
return data;
|
2019-06-16 13:09:54 +00:00
|
|
|
}
|
2020-03-29 08:23:08 +00:00
|
|
|
const queryMap = async (data) => {
|
|
|
|
data['lang'] = I18n.currentLocale()
|
|
|
|
data["test"] = isDebugMode
|
|
|
|
let response = await fetch(MARKER_URL, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(data),
|
2019-06-16 13:09:54 +00:00
|
|
|
|
2020-03-29 08:23:08 +00:00
|
|
|
});
|
|
|
|
let responseText = await response.text()
|
|
|
|
//console.log("response= " + responseText)
|
|
|
|
try {
|
2020-10-25 16:39:19 +00:00
|
|
|
let responseJson = JSON.parse(responseText);
|
|
|
|
console.warn("JSON RESPONSE", responseJson);
|
2020-03-29 08:23:08 +00:00
|
|
|
return responseJson;
|
|
|
|
} catch (e) {
|
|
|
|
//console.log("erreur=" + responseText)
|
|
|
|
//console.log(response)
|
|
|
|
throw new Error("Erreur lors de la conversion en json")
|
|
|
|
}
|
|
|
|
throw new Error("Erreur lors de la conversion en json")
|
2019-06-16 13:09:54 +00:00
|
|
|
|
|
|
|
}
|