ilink-world/app/screens/account/UpdateInformations.js

149 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-12-05 17:14:33 +00:00
import React, {Component} from 'react';
import {Alert, View} from 'react-native';
import {readUser, updatePosition, updateUserData} from './../../webservice/AuthApi';
import {responsiveHeight} from 'react-native-responsive-dimensions';
2019-06-16 13:09:54 +00:00
import Icon from 'react-native-vector-icons/FontAwesome5'
import Button from 'apsl-react-native-button'
import I18n from 'react-native-i18n'
import {IlinkEmitter} from "../../utils/events";
2020-12-05 17:14:33 +00:00
import Geolocation from 'react-native-geolocation-service';
let theme = require('./../../utils/theme.json');
const route = require('./../../route.json')
2019-06-16 13:09:54 +00:00
require('./../../utils/Translations')
2020-12-05 17:14:33 +00:00
const height = responsiveHeight(100) - 250;
export default class UpdateInformations extends Component {
2019-06-16 13:09:54 +00:00
static navigatorStyle = {
navBarHidden: false,
navBarBackgroundColor: theme.primaryDark,
navBarTextColor: 'white',
navBarButtonColor: 'white',
drawUnderStatusBar: false,
statusBarColor: theme.primaryDarkAdvanced,
statusBarTextColorScheme: 'light',
};
2020-12-05 17:14:33 +00:00
static options(passProps) {
2019-06-16 13:09:54 +00:00
return {
2020-12-05 17:14:33 +00:00
statusBar: {
drawBehind: false
2019-06-16 13:09:54 +00:00
},
2020-12-05 17:14:33 +00:00
topBar: {
title: {
text: "Mise à jour des informations",
color: "white"
2019-06-16 13:09:54 +00:00
},
2020-12-05 17:14:33 +00:00
background: {
color: theme.primaryDark
2019-06-16 13:09:54 +00:00
}
}
}
}
2020-12-05 17:14:33 +00:00
static navigationOptions = ({navigation}) => {
2019-06-16 13:09:54 +00:00
return {
2020-12-05 17:14:33 +00:00
headerTitle: I18n.t('CHANGE_INFORMATION'),
drawerIcon: ({tintColor}) => (
<Icon
name={'user'}
size={24}
/>
),
}
};
constructor(props) {
2019-06-16 13:09:54 +00:00
super(props)
2020-12-05 17:14:33 +00:00
this.state = this.initiateItems()
2019-06-16 13:09:54 +00:00
IlinkEmitter.on("langueChange", this.updateLangue.bind(this))
this.showUserState()
}
2020-12-05 17:14:33 +00:00
async showUserState() {
const user = await readUser()
this.setState({user: user})
if (user.longitude <= 0 && user.latitude <= 0) {
Alert.alert(I18n.t('TITLE_NEED_POSITION'), I18n.t('TEXT_NEED_POSITION'), [{text: 'Ok'}])
2019-06-16 13:09:54 +00:00
}
}
2020-12-05 17:14:33 +00:00
updateLangue() {
this.props.navigation.setParams({name: I18n.t('CHANGE_INFORMATION')})
2019-06-16 13:09:54 +00:00
this.forceUpdate()
}
2020-12-05 17:14:33 +00:00
render() {
2019-06-16 13:09:54 +00:00
return (
2020-12-05 17:14:33 +00:00
<View style={{flex: 1, backgroundColor: theme.primary}}>
2019-06-16 13:09:54 +00:00
<Button
2020-12-05 17:14:33 +00:00
isLoading={this.state.positionEnabled}
style={{
backgroundColor: theme.primaryDark,
marginTop: responsiveHeight(5),
height: responsiveHeight(7),
marginRight: 10,
marginLeft: 10,
borderColor: 'transparent'
}} textStyle={{color: "white", fontSize: 20, fontWeight: 'bold'}}
onPress={() => this.onClickUpdatePosition()}>{I18n.t('UPDATE_POSITION_TEXT')}</Button>
</View>);
2019-06-16 13:09:54 +00:00
}
initiateItems() {
2020-12-05 17:14:33 +00:00
return {
positionEnabled: false,
2019-06-16 13:09:54 +00:00
}
}
2020-12-05 17:14:33 +00:00
onClickUpdatePosition() {
2021-05-24 11:11:55 +00:00
this.setState({positionEnabled: true});
2020-12-05 17:14:33 +00:00
Geolocation.getCurrentPosition((position) => {
2021-05-24 11:11:55 +00:00
console.log("CURRENT POSITION", position);
2019-06-16 13:09:54 +00:00
const myPosition = position.coords;
2020-12-05 17:14:33 +00:00
updatePosition(myPosition.longitude, myPosition.latitude).then((response) => {
var title = '';
var message = '';
this.setState({positionEnabled: false})
2019-06-16 13:09:54 +00:00
console.log(response.error)
2020-12-05 17:14:33 +00:00
if (response.error === undefined) {
updateUserData({longitude: myPosition.longitude, latitude: myPosition.latitude})
message = I18n.t('POSITION_UPDATE_SUCCESS_TEXT')
title = I18n.t('UPDATE_SUCCESS');
} else {
title = I18n.t("TITLE_UPDATE_POSITION_FAILED")
switch (response.error) {
case -3:
message = I18n.t('TEXT_UDATE_POSITION_FAILED_1');
break
case -2:
message = I18n.t('TEXT_UDATE_POSITION_FAILED_2');
break;
2019-06-16 13:09:54 +00:00
}
}
2020-12-05 17:14:33 +00:00
Alert.alert(title, message, [{
text: "Ok", onPress: () => {
this.props.navigation.popToTop()
}
2021-05-24 11:11:55 +00:00
}]);
2020-12-05 17:14:33 +00:00
}).catch((e) => {
2021-05-24 11:11:55 +00:00
console.log(e);
2020-12-05 17:14:33 +00:00
this.setState({positionEnabled: false})
2019-06-16 13:09:54 +00:00
})
2021-05-24 11:11:55 +00:00
}, (e) => {
console.log(e);
Alert.alert(I18n.t('ERROR_LABEL'), e.message, [{
text: "Ok", onPress: () => {
this.props.navigation.popToTop();
}
}]);
}, this.props.geolocationOptions);
2019-06-16 13:09:54 +00:00
}
}