diff --git a/app/screens/login/createUserStep2.js b/app/screens/login/createUserStep2.js index 2f89f8ea..9a8d6aee 100755 --- a/app/screens/login/createUserStep2.js +++ b/app/screens/login/createUserStep2.js @@ -102,8 +102,11 @@ export default class CreateUserStep2 extends Component { retreiveCodeInformation() { const membre = this.state.user.member; + console.log("membre===>> ", membre); + console.log("1===>>"); return new Promise(async (resolve, reject) => { let datas = await getCodeInformation(membre) + console.log("datas===>> ", datas); resolve(datas) }) @@ -145,9 +148,10 @@ export default class CreateUserStep2 extends Component { if (indicatif) { let result = {} try { + console.log("RESULTAT1===>> ", indicatif); result = await this.retreiveCodeInformation() const {category} = result - console.log("RESULTAT ", result); + console.log("RESULTAT===>> ", result); this.setState({result}); } catch (e) { result = false @@ -178,7 +182,7 @@ export default class CreateUserStep2 extends Component { console.log("need enable") getCountryNetwork(indicatif).then((result) => { this.reseaux = []; - let networks = result; + let networks = result;; for (let prop in networks) { if (networks[prop] !== "") { @@ -246,7 +250,7 @@ export default class CreateUserStep2 extends Component { } componentDidMount() { - + this.fetchCountries(); this.mounted = true; const {type} = this.props; @@ -260,6 +264,93 @@ export default class CreateUserStep2 extends Component { } } + getCountryByLocation = async (countriesArray) => { + try { + // Vérification des permissions Android + if (Platform.OS === 'android') { + const granted = await PermissionsAndroid.request( + PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION + ); + + if (granted !== PermissionsAndroid.RESULTS.GRANTED) { + return this.findDefaultCountry(countriesArray); + } + } + + // Récupération de la position + const position = await new Promise((resolve, reject) => { + Geolocation.getCurrentPosition(resolve, reject, GEOLOCATION_OPTIONS); + }); + // Reverse geocoding + const geocodeResponse = await getPositionInformation(position.coords); + const countryComponent = geocodeResponse.results[0]?.address_components?.find( + c => c.types.includes("country") + ); + + if (!countryComponent) return this.findDefaultCountry(countriesArray); + + // Recherche dans le tableau + const foundCountry = countriesArray.find( + country => country.code_country === countryComponent.short_name + ); + return foundCountry || this.findDefaultCountry(countriesArray); + + } catch (error) { + console.error('Erreur de géolocalisation:', error); + return this.findDefaultCountry(countriesArray); + } + }; + + // Fonction helper pour trouver le Cameroun + findDefaultCountry = (countriesArray) => { + return countriesArray.find(c => c.code_country === "CM") || countriesArray[0]; + }; + fetchCountries = async () => { + try { + const response = await getListCountriesActive(); + + let countriesData = []; + if (response.error === "error" && response.error_msg) { + const jsonMatch = response.error_msg.match(/\[.*\]/); + if (jsonMatch) { + countriesData = JSON.parse(jsonMatch[0]); + } + }else{ + countriesData = response; + } + + + const formattedCountries = countriesData.map(country => ({ + label: country.name, + value: country.name, + code_dial: country.code_dial, + code_country: country.code_country + })); + const detectedCountry = await this.getCountryByLocation(formattedCountries); + this.setState({ + country: formattedCountries, + countries: formattedCountries[0], + isLoading: false, + selectedCountry: detectedCountry, // Sélectionne le premier pays par défaut + indicatif: detectedCountry?.code_dial + }); + this.getNetworks(detectedCountry.code_dial); + } catch (error) { + console.error('Erreur lors de la récupération des pays:', error); + this.setState({ isLoading: false }); + } + }; + + handleCountryChange = (value, index, data) => { + const selectedCountry = this.state.countries[index]; + this.setState({ + selectedCountry: selectedCountry, + indicatif: selectedCountry.code_dial, + network: null + }); + this.getNetworks(selectedCountry.code_dial); + + }; async requestGeolocationPermission() { try { @@ -393,7 +484,7 @@ export default class CreateUserStep2 extends Component { latitude: position.coords.latitude, longitude: position.coords.longitude }, () => { - console.log("latitude", this.state.latitude); + }); if (this.result.child) { this.setState({disableNetwork: true}); @@ -437,104 +528,159 @@ export default class CreateUserStep2 extends Component { } - treatPosition(position) { - const myLastPosition = this.state.myPosition; + // treatPosition(position) { + // console.log("debugTreatPosition1===>>") + // const myLastPosition = this.state.myPosition; + // const myPosition = position.coords; + + // if (!isEqual(myPosition, myLastPosition)) { + // console.log("debugTreatPosition2===>>") + // getPositionInformation(myPosition).then((response) => { + // console.log("debugTreatPosition3===>>", response) + // if (response.results !== undefined) { + // console.log("debugTreatPosition4===>>") + // if (response.results.length > 0) { + // console.log("debugTreatPosition5===>>") + // let most = response.results[0] + // let {address_components, formatted_address, place_id} = most + // this.setState({address: address_components, textadress: formatted_address, place: place_id}) + + // let results = response.results; + // let shortcountry; + // let mcountry; + // for (let i = 0; i < results[0].address_components.length; i++) { + // for (let j = 0; j < results[0].address_components[i].types.length; j++) { + // console.log("debugTreatPositionX===>>",JSON.stringify(results)) + // if (results[0].address_components[i].types[j] === "country") { + // console.log("debugTreatPosition6===>>") + // mcountry = results[0].address_components[i]; + // shortcountry = mcountry.short_name; + // this.setState({shortCountry: mcountry.short_name, longCountry: mcountry.long_name}) + // } else if (results[0].address_components[i].types[j] === "locality") { + // console.log("debugTreatPosition7===>>") + // const name = results[0].address_components[i].short_name; + // this.setState({townName: name}); + // getTownInformationName(name).then((result) => { + // console.log("debugTreatPosition8===>>",result) + // let town = null; + // if (result instanceof Array) { + // town = result[0]; + // } else + // town = result; + // console.log("Mise a jour getTownInformationNametown===>>", town); + // this.setState({town: town}); + // }) + // } else { + // console.log("debugTreatPosition10===>>") + // getDefaultTown().then(result => { + // getTownInformationName(result.default_locality).then((resultTowwn) => { + // let town = null; + // if (resultTowwn instanceof Array) { + // town = resultTowwn[0]; + // } else + // town = resultTowwn; + // this.setState({town: town}); + // }); + // }) + // } + // } + // } + // getListCountriesActive().then((cnt) => { + // this.setState({countries: cnt}) + // console.debug(cnt, shortcountry); + // var found = false + // for (let i of cnt) { + // if (i.code_country === shortcountry) { + // found = true + // this.setState({indicatif: i.code_dial, country: i.name}) + // this.getNetworks(i.code_dial); + // } + // } + // if (!found) { + // Alert.alert("Impossible de recupérer vos informations", "Nous n'avons pas pu recuperer les informations de votre pays veuillez contacter les administrateurs", [{text: "OK"}]); + + // } + // }) + + + // } + + // } + // }).catch((e) => { + + // }); + // this.setState({myPosition: myPosition}); + // if (this.mapRef !== undefined && this.mapRef !== null) { + // this.mapRef.animateToCoordinate({ + // latitude: myPosition.latitude, + // longitude: myPosition.longitude + // }, 1000); + // this.mapRef.animateToRegion({ + // latitude: myPosition.latitude, + // longitude: myPosition.longitude, + // latitudeDelta: 0.03, + // longitudeDelta: 0.01, + // }, 1000) + // } + // } + + // } + treatPosition = async (position) => { const myPosition = position.coords; - - if (!isEqual(myPosition, myLastPosition)) { - getPositionInformation(myPosition).then((response) => { - if (response.results !== undefined) { - if (response.results.length > 0) { - let most = response.results[0] - let {address_components, formatted_address, place_id} = most - this.setState({address: address_components, textadress: formatted_address, place: place_id}) - - let results = response.results; - let shortcountry; - let mcountry; - for (let i = 0; i < results[0].address_components.length; i++) { - for (let j = 0; j < results[0].address_components[i].types.length; j++) { - if (results[0].address_components[i].types[j] === "country") { - mcountry = results[0].address_components[i]; - shortcountry = mcountry.short_name; - this.setState({shortCountry: mcountry.short_name, longCountry: mcountry.long_name}) - } else if (results[0].address_components[i].types[j] === "locality") { - const name = results[0].address_components[i].short_name; - this.setState({townName: name}); - getTownInformationName(name).then((result) => { - let town = null; - if (result instanceof Array) { - town = result[0]; - } else - town = result; - this.setState({town: town}); - }) - } else { - getDefaultTown().then(result => { - getTownInformationName(result.default_locality).then((resultTowwn) => { - let town = null; - if (resultTowwn instanceof Array) { - town = resultTowwn[0]; - } else - town = resultTowwn; - this.setState({town: town}); - }); - }) - } - } - } - getListCountriesActive().then((cnt) => { - this.setState({countries: cnt}) - console.debug(cnt, shortcountry); - var found = false - for (let i of cnt) { - if (i.code_country === shortcountry) { - found = true - this.setState({indicatif: i.code_dial, country: i.name}) - this.getNetworks(i.code_dial); - } - } - if (!found) { - Alert.alert("Impossible de recupérer vos informations", "Nous n'avons pas pu recuperer les informations de votre pays veuillez contacter les administrateurs", [{text: "OK"}]); - - } - }) - - - } - - } - }).catch((e) => { - this.showErrorDialog() - }); - this.setState({myPosition: myPosition}); - if (this.mapRef !== undefined && this.mapRef !== null) { - this.mapRef.animateToCoordinate({ - latitude: myPosition.latitude, - longitude: myPosition.longitude - }, 1000); - this.mapRef.animateToRegion({ - latitude: myPosition.latitude, - longitude: myPosition.longitude, - latitudeDelta: 0.03, - longitudeDelta: 0.01, - }, 1000) - } + if (isEqual(myPosition, this.state.myPosition)) return; + + try { + const { results = [] } = await getPositionInformation(myPosition); + if (!results.length) return; + + const comps = results[0].address_components; + const country = comps.find(c => c.types.includes("country")); + const locality = comps.find(c => c.types.includes("locality")); + + if (country) { + this.setState({ shortCountry: country.short_name, + longCountry : country.long_name }); + } + + let townName, townInfo; + + if (locality) { + townName = locality.short_name; + townInfo = await getTownInformationName(townName); + } else { + const def = await getDefaultTown(); + townName = def.default_locality; + townInfo = await getTownInformationName(townName); + } + + this.setState({ + townName, + town: Array.isArray(townInfo) ? townInfo[0] : townInfo + }); + + // autres mises à jour (carte, myPosition, réseaux, …) + this.setState({ myPosition }); + this.mapRef?.animateToCoordinate(myPosition, 1000); + this.mapRef?.animateToRegion({ ...myPosition, + latitudeDelta: 0.03, longitudeDelta: 0.01 }, 1000); + + } catch (e) { + // gestion d'erreur } - - } + }; + componentWillUnmount() { this.mounted = false; // eslint-disable-next-line no-undef if (this.watchID) Geolocation.clearWatch(this.watchID); } - + renderUserGeoAccount() { - + const { selectedCountry } = this.state; return ( - {this.prepareModal()} + {/* {this.prepareModal()} */} - { + const selectedCountry = this.state.countries[index]; + this.setState({ + countries: selectedCountry.countries, + indicatif: selectedCountry.code_dial, + network: null + }); + this.getNetworks(selectedCountry.code_dial); + }} + + /> + + {console.log("town===>>",this.state.town)} + {/* { return value.name }} - /> + /> */} { this.numanim = comp @@ -767,7 +933,7 @@ export default class CreateUserStep2 extends Component { ({value: row, label: row}))} visible={this.state.networksinglePickerVisible} selectedItem={this.state.singlePickerSelectedItem} @@ -1018,8 +1184,9 @@ export default class CreateUserStep2 extends Component { data['category'] = user.category; data['active'] = '0'; + console.log("data===>>",data); createGeolocatedAccount(data).then((result) => { - console.log(result); + console.log("result===>>",result); if (result.success !== undefined && result.success === 1) { const message = result.category === 'super' ? I18n.t("HYPERVISOR_MUST_VALIDATE_SUPERVISOR") : I18n.t("ACCOUNT_SUCCESSFULL_CREATED") diff --git a/app/webservice/AuthApi.js b/app/webservice/AuthApi.js index 4a559b6c..d0069837 100755 --- a/app/webservice/AuthApi.js +++ b/app/webservice/AuthApi.js @@ -22,9 +22,10 @@ var serializeJSON = function (data) { export const getCodeInformation = (code) => { var data = { - "tag": 'member', "type": "agen_info_code", "code": code, + "tag": 'member', "type": "agen_info_code", "code": "+237", "lang": I18n.currentLocale() }; + console.log("2===>>"); return queryData(data, memberActionUrl) } @@ -41,8 +42,7 @@ function queryAuth(data) { } async function queryData(data, url) { - console.warn("REQUEST URL", url); - console.warn("REQUEST BODY", JSON.stringify(data)); + console.log("3===>>",url); data["lang"] = I18n.currentLocale(); data["isTest"] = isDebugMode /* await axios({ @@ -60,7 +60,7 @@ async function queryData(data, url) { console.warn("SERVER RESPONSE ERROR", error); return {"error": "error", "error_msg": error} });*/ - + console.log("Ici data===>>", data) let response = await fetch(url, { method: 'POST', headers: { @@ -78,6 +78,7 @@ async function queryData(data, url) { console.warn("SERVER RESPONSE ERROR", responseText); return {"error": "error", "error_msg": responseText} } + console.log("responseJson===>> ", responseJson); return responseJson; diff --git a/app/webservice/IlinkConstants.js b/app/webservice/IlinkConstants.js index b247d17e..3c45bb58 100755 --- a/app/webservice/IlinkConstants.js +++ b/app/webservice/IlinkConstants.js @@ -1,24 +1,11 @@ export const isDebugMode = false -//base url test -//export const baseUrl = "https://ilink-app.com/mobilebackendbeta" -//base url production -//export const baseUrl = "https://ilink-app.com/mobilebackend"; -//export const baseUrl = "https://test.ilink-app.com/mobilebackendtest"; -//export const baseUrl = "http://test.ilink-app.com:8080/mobilebackendtest"; -//const baseUrl = "https://ilink-app.com/mobilebackendtest2" export const baseUrl = "https://test.ilink-app.com:8080/mobilebackend"; export const testBaseUrl = "https://test.ilink-app.com"; export const testBaseUrlWithPort = "https://test.ilink-app.com:8086"; -/*export const baseUrl = "https://ilink-app.com/mobilebackend"; -export const testBaseUrl = "https://ilink-app.com:8080";*/ -/* export const baseUrl = "https://preprod.ilink-app.com:8080/mobilebackend"; -export const testBaseUrl = "https://preprod.ilink-app.com"; */ -//base url agent test -//const baseUrl = "https://ilink-app.com/mobilebackendtest"; export const adhesionUrl = baseUrl + '/interacted/LoginAction.php'; export const memberActionUrl = baseUrl + '/interacted/MembersAction.php'; diff --git a/app/webservice/MapService.js b/app/webservice/MapService.js index ac393861..5da5fe39 100755 --- a/app/webservice/MapService.js +++ b/app/webservice/MapService.js @@ -5,7 +5,7 @@ import I18n from 'react-native-i18n' import {readUser} from './AuthApi' import {isDebugMode, MARKER_URL} from "./IlinkConstants"; -let GEOCODDING_URL = "https://ilink-app.com:8080/geocode"; +let GEOCODDING_URL = "https://test.ilink-app.com/geocode"; let API_KEY = "AIzaSyCQY0rwMM9Pn9XWt5F6YLhGoez_bU1IGtc"