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

151 lines
5.6 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';
2022-12-28 16:32:30 +01:00
// Redux
2022-10-06 16:25:15 +02:00
import { connect } from 'react-redux';
2023-01-03 12:55:28 +01:00
// SplashScreen
import * as SplashScreen from 'expo-splash-screen';
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 {
2022-12-28 16:32:30 +01:00
app: state.app,
users: state.users.users,
locale: state.locale.locale,
2022-12-30 10:02:07 +01:00
mainColor: state.app.mainColor
2022-10-06 16:25:15 +02:00
};
}
2022-10-05 18:03:52 +02:00
2022-12-28 16:32:30 +01:00
const mapDispatchToProps = dispatch => {
return {
2022-12-30 16:44:51 +01:00
setCurrentUser: () => dispatch({ type: 'SET_CURRENT_USER' }),
2022-12-28 16:32:30 +01:00
};
};
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
};
2022-12-30 16:44:51 +01:00
componentDidMount() {
if (SyncController.isLogged()) {
this.props.setCurrentUser(false);
2022-12-30 16:44:51 +01:00
}
2023-01-03 12:55:28 +01:00
SplashScreen.hideAsync();
2022-12-30 16:44:51 +01:00
}
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)}
2022-12-30 10:02:07 +01:00
color={this.props.mainColor}
2022-10-05 18:03:52 +02:00
/>
</View>
</View>
</ImageBackground >
);
}
2022-10-06 16:25:15 +02:00
2023-01-04 10:31:56 +01:00
async login(login, password) {
2022-10-06 16:25:15 +02:00
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;
}
2023-01-04 10:31:56 +01:00
const isConnected = await SyncController.checkConnection().then(state => {
//console.log(state.isConnected)
return state.isConnected
}).catch(error => {
//console.log(error);
return false;
});
2022-10-06 16:25:15 +02:00
const lastUser = Setting.get('lastUser');
2022-12-28 16:32:30 +01:00
// Si on est sur une installation
2022-12-30 16:44:51 +01:00
if (lastUser === false || lastUser == undefined) {
// Si la tablette est connectée à internet pour forcer la synchro
2023-01-04 10:31:56 +01:00
if (!isConnected) {
Alert.alert(i18n.t('error.error'), i18n.t('error.tablet_not_connected_internet'));
2022-10-06 16:25:15 +02:00
return;
}
return this.loginAction(user);
}
// Si on a deja une install
if (lastUser !== false && lastUser !== undefined) {
// Si on a une précédente connexion
if (lastUser.value === user.id.toString()) {
this.loginAction(user);
} else { // Si le user precedent est different du user courant
// Si user !== last user && last user not synced
if (!SyncController.isSynced()) {
Alert.alert(i18n.t('error.error'), i18n.t('error.tablet_not_synced_last_user'));
return;
}
2022-10-06 16:25:15 +02:00
// Si la tablette est connectée à internet pour forcer la synchro
2023-01-04 10:31:56 +01:00
if (!isConnected) {
2022-10-06 16:25:15 +02:00
Alert.alert(i18n.t('error.error'), i18n.t('error.tablet_not_connected_internet'));
return;
}
SyncController.setInstalled(true);
2022-10-06 16:25:15 +02:00
this.loginAction(user, true);
}
}
}
loginAction(user) {
2022-10-06 16:25:15 +02:00
this.setState({ login: '', password: '' });
LoginController.login(user);
2022-12-30 16:44:51 +01:00
this.props.setCurrentUser();
2022-10-06 16:25:15 +02:00
}
2022-10-05 18:03:52 +02:00
}
2022-12-28 16:32:30 +01:00
export default connect(mapStateToProps, mapDispatchToProps)(LoginScreen);