ldc-com-app/ComApp/src/App/Views/LoginScreen.js

122 lines
4.7 KiB
JavaScript
Raw Normal View History

2022-10-05 18:03:52 +02:00
import React from 'react';
2022-10-06 16:25:15 +02:00
import { View, ImageBackground, TextInput, Button, Image, Alert } from 'react-native';
import { connect } from 'react-redux';
2022-10-05 18:03:52 +02:00
// Common Styles
import commonStyles from '../../Resources/CommonStyles';
// Style
import styles from '../../Resources/Styles/LoginStyle';
// Translations
import i18n from "../../I18n";
// FileSytem
import * as FileSystem from 'expo-file-system';
2022-10-06 16:25:15 +02:00
// Controller
import LoginController from '../Controllers/LoginController';
// Database
import { Setting } from '../../Database';
import SyncController from '../Controllers/SyncController';
function mapStateToProps(state) {
return {
users: state.app.users,
locale: state.app.locale,
};
}
2022-10-05 18:03:52 +02:00
class LoginScreen extends React.Component {
state = {
2022-10-06 16:25:15 +02:00
login: 'v@b.t',
password: 'test',
2022-10-05 18:03:52 +02:00
};
render() {
return (
<ImageBackground source={{ uri: FileSystem.documentDirectory + 'BackgroundImage.png' }} style={commonStyles.background}>
<View style={styles.container}>
<Image source={{ uri: FileSystem.documentDirectory + 'SquaredLogo.png' }} style={styles.logo} />
<View>
<TextInput
style={styles.text}
placeholder={i18n.t('login_s.username')}
value={this.state.login}
onChangeText={text => this.setState({ login: text })}
autoCapitalize={'none'}
autoCorrect={false}
keyboardType={'email-address'}
/>
<TextInput
style={styles.text}
placeholder={i18n.t('login_s.password')}
value={this.state.password}
onChangeText={text => this.setState({ password: text })}
secureTextEntry={true}
autoCapitalize={'none'}
autoCorrect={false}
/>
<Button
title={i18n.t('global.login')}
onPress={() => this.login(this.state.login, this.state.password)}
color={'#ce3029'}
/>
</View>
</View>
</ImageBackground >
);
}
2022-10-06 16:25:15 +02:00
login(login, password) {
if (login === '' || password === '') {
Alert.alert(i18n.t("global.warning"), i18n.t('error.must_fill_login_fields'));
return;
}
var usersSize = Object.keys(this.props.users).length;
if (usersSize === 0) {
Alert.alert(i18n.t("global.warning"), i18n.t('login_s.missing_users'));
return;
}
let user = this.props.users.filter(tmp => tmp.email === login);
if (user.length === 0) {
Alert.alert(i18n.t('error.error'), i18n.t('error.incorrect_credentials'));
return;
}
user = user[0];
const verified = LoginController.verify(user, password).then(verified => verified);
if (!verified) {
Alert.alert(i18n.t('error.error'), i18n.t('error.incorrect_credentials'));
return;
}
const lastUser = Setting.get('lastUser');
if (lastUser === false || lastUser === undefined) {
this.loginAction(user);
} else {
// Si user !== last user && last user not synced
if (lastUser.value !== user.id.toString() && !SyncController.isSynced()) {
Alert.alert(i18n.t('error.error'), i18n.t('error.tablet_not_synced_last_user'));
return;
}
// Si user !== last user
if (lastUser.value !== user.id.toString()) {
// Si la tablette est connectée à internet pour forcer la synchro
if (!SyncController.checkConnection()) {
Alert.alert(i18n.t('error.error'), i18n.t('error.tablet_not_connected_internet'));
return;
}
this.loginAction(user, true);
}
}
}
loginAction(user, forceSync = false) {
this.setState({ login: '', password: '' });
LoginController.login(user);
this.props.dispatch({
type: 'SET_CURRENT_USER',
data: JSON.stringify(user),
});
this.props.dispatch({ type: 'SET_LOGGED', data: true });
if (forceSync === true) {
this.props.navigation.navigate('SyncScreen', { isInstalled: true, forceSync: true });
}
}
2022-10-05 18:03:52 +02:00
}
2022-10-06 16:25:15 +02:00
export default connect(mapStateToProps)(LoginScreen);