Sync users if possible on login screen (prevent reinstalling app to sync new users)
This commit is contained in:
parent
d418f1312f
commit
64c324d30a
2 changed files with 38 additions and 5 deletions
|
|
@ -22,7 +22,8 @@ import * as FileSystem from 'expo-file-system';
|
|||
// Controller
|
||||
import LoginController from '../Controllers/LoginController';
|
||||
// Database
|
||||
import { Setting } from '../../Database';
|
||||
import { Setting, User } from '../../Database';
|
||||
import InstallController from '../Controllers/InstallController';
|
||||
import SyncController from '../Controllers/SyncController';
|
||||
|
||||
function mapStateToProps(state) {
|
||||
|
|
@ -42,6 +43,7 @@ const mapDispatchToProps = dispatch => {
|
|||
|
||||
class LoginScreen extends React.Component {
|
||||
state = {
|
||||
syncWorking: false,
|
||||
login: '',
|
||||
password: '',
|
||||
};
|
||||
|
|
@ -51,6 +53,7 @@ class LoginScreen extends React.Component {
|
|||
this.props.setCurrentUser(false);
|
||||
}
|
||||
SplashScreen.hideAsync();
|
||||
this.syncUsersIfPossibleAsync();
|
||||
}
|
||||
|
||||
render() {
|
||||
|
|
@ -83,11 +86,12 @@ class LoginScreen extends React.Component {
|
|||
autoCorrect={false}
|
||||
/>
|
||||
<Button
|
||||
disabled={this.state.syncWorking}
|
||||
title={i18n.t('global.login')}
|
||||
onPress={() =>
|
||||
this.login(this.state.login.toLowerCase(), this.state.password)
|
||||
}
|
||||
color={this.props.mainColor}
|
||||
color={!this.state.syncWorking ? this.props.mainColor : 'gray'}
|
||||
/>
|
||||
</View>
|
||||
</View>
|
||||
|
|
@ -123,11 +127,11 @@ class LoginScreen extends React.Component {
|
|||
}
|
||||
const isConnected = await SyncController.checkConnection()
|
||||
.then(state => {
|
||||
//console.log(state.isConnected)
|
||||
console.debug(state.isConnected);
|
||||
return state.isConnected;
|
||||
})
|
||||
.catch(error => {
|
||||
//console.log(error);
|
||||
console.debug(error);
|
||||
return false;
|
||||
});
|
||||
const lastUser = Setting.get('lastUser');
|
||||
|
|
@ -172,6 +176,35 @@ class LoginScreen extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
async syncUsersIfPossibleAsync() {
|
||||
const isConnected = await SyncController.checkConnection();
|
||||
if (!isConnected.isInternetReachable) {
|
||||
return;
|
||||
}
|
||||
this.setState({ syncWorking: true });
|
||||
if (InstallController.getAppConfig(SyncController.getUrlBo())) {
|
||||
// getUsers
|
||||
const resUsers = await SyncController.getUsers()
|
||||
.then(res => {
|
||||
User.clear();
|
||||
return res.data;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
Alert.alert(
|
||||
i18n.t('error.error'),
|
||||
i18n.t('error.error_has_occurred'),
|
||||
);
|
||||
this.setState({ syncWorking: false });
|
||||
return;
|
||||
});
|
||||
resUsers.forEach(user => {
|
||||
User.add(user);
|
||||
});
|
||||
this.setState({ syncWorking: false });
|
||||
}
|
||||
}
|
||||
|
||||
loginAction(user) {
|
||||
this.setState({ login: '', password: '' });
|
||||
LoginController.login(user);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import React from 'react';
|
||||
import { Alert, Button, Text, View } from 'react-native';
|
||||
import { Alert, Button, Linking, Text, View } from 'react-native';
|
||||
// Redux
|
||||
import { connect } from 'react-redux';
|
||||
import { initContact } from '../Store/Actions/contacts';
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue