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

99 lines
3.9 KiB
JavaScript
Raw Normal View History

import React from 'react';
2022-10-07 18:27:54 +02:00
import { ImageBackground, View, Image, Text, TextInput, Button, Alert } from 'react-native';
// Redux
import { connect } from 'react-redux';
// Helpers
2022-09-29 15:03:47 +02:00
import Helpers from '../Lib/Helpers';
// Common Styles
import commonStyles from '../../Resources/CommonStyles';
2022-09-30 17:40:03 +02:00
// Style
import styles from '../../Resources/Styles/InstallStyle';
// Translations
import i18n from "../../I18n";
// Controller
import InstallController from '../Controllers/InstallController';
2022-10-07 18:27:54 +02:00
import SyncController from '../Controllers/SyncController';
// Database
import { User } from '../../Database';
function mapStateToProps(state) {
return {
isSetup: state.app.isSetup
};
}
class InstallScreen extends React.Component {
state = {
2022-10-07 18:27:54 +02:00
processing: false,
txtStep: '',
2022-09-29 15:03:47 +02:00
url: "https://dev-app.rhd-ldc.fr",
}
render() {
return (
<ImageBackground source={require('../../Resources/assets/img/background_ldc.png')} style={commonStyles.background}>
2022-09-30 17:40:03 +02:00
<View style={styles.container}>
<Image source={require('../../Resources/assets/img/logo-square-ldc.png')} style={styles.logo} />
<View style={styles.actions}>
<TextInput
style={commonStyles.textInput}
placeholder={i18n.t("install.enter_url")}
onChangeText={(newText) => this.setState({ url: newText })}
value={this.state.url}
/>
<Button
2022-10-07 18:27:54 +02:00
disabled={this.state.processing}
color={'#D4021D'}
title={ i18n.t('install.install')}
onPress={() => {
if (this.state.url.length === 0) {
2022-09-30 17:13:43 +02:00
Alert.alert(i18n.t("alert.error"), i18n.t("install.enter_url"));
} else {
if (Helpers.isUrlValid(this.state.url)) {
2022-10-07 18:27:54 +02:00
this.install();
} else {
2022-09-30 17:13:43 +02:00
Alert.alert(i18n.t("alert.error"), i18n.t("install.url_invalid"));
}
}
}}
/>
</View>
2022-10-07 18:27:54 +02:00
<View style={styles.actions}>
<Text>
{this.state.txtStep}
</Text>
</View>
</View>
</ImageBackground>
);
2022-10-07 18:27:54 +02:00
}
async install() {
2022-10-07 18:27:54 +02:00
this.setState({ processing: true, txtStep: i18n.t("install.get_app_setting") });
if (InstallController.getConfig(this.state.url)) {
this.setState({ txtStep: i18n.t("install.get_content_setting") });
if (InstallController.getAppConfig(this.state.url)) {
// getUsers
this.setState({ txtStep: i18n.t('sync_s.updating_users') + '...' });
const resUsers = await SyncController.getUsers().then(res => {
User.clear();
return res.data;
}).catch(error => {
console.log(error);
Alert.alert(
i18n.t('error.error'),
i18n.t('error.error_has_occurred')
);
return
});
resUsers.forEach(user => {
User.add(user);
});
this.props.dispatch({ type: 'SET_ISSETUP' });
this.props.navigation.navigate('LoginScreen');
2022-10-07 18:27:54 +02:00
}
}
this.setState({ processing: false, txtStep: "" });
2022-10-07 18:27:54 +02:00
}
}
export default connect(mapStateToProps)(InstallScreen);