ldc-com-app/ComApp/src/App/Views/InstallScreen.js
2023-01-02 15:31:47 +01:00

163 lines
No EOL
6.1 KiB
JavaScript

import React from 'react';
import { ImageBackground, View, Image, Text, TextInput, Button, Alert } from 'react-native';
// Redux
import { connect } from 'react-redux';
// Helpers
import Helpers from '../Lib/Helpers';
// Lodash
import _ from 'lodash';
// Common Styles
import commonStyles from '../../Resources/CommonStyles';
// Style
import styles from '../../Resources/Styles/InstallStyle';
// Translations
import i18n from "../../I18n";
// Controller
import InstallController from '../Controllers/InstallController';
import SyncController from '../Controllers/SyncController';
// Database
import { User, Setting } from '../../Database';
// FileSytem
import * as FileSystem from 'expo-file-system';
function mapStateToProps(state) {
return {
isSetup: state.app.isSetup
};
}
const mapDispatchToProps = dispatch => {
return {
setIsSetup: () => dispatch({ type: 'SET_ISSETUP' }),
};
};
class InstallScreen extends React.Component {
state = {
processing: false,
txtStep: '',
url: "https://dev-app.rhd-ldc.fr",
}
render() {
return (
<ImageBackground source={require('../../Resources/assets/img/background_ldc.png')} style={commonStyles.background}>
<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
disabled={this.state.processing}
color={'#D4021D'}
title={ i18n.t('install.install')}
onPress={() => {
if (this.state.url.length === 0) {
Alert.alert(i18n.t("alert.error"), i18n.t("install.enter_url"));
} else {
if (Helpers.isUrlValid(this.state.url)) {
this.install();
} else {
Alert.alert(i18n.t("alert.error"), i18n.t("install.url_invalid"));
}
}
}}
/>
</View>
<View style={styles.actions}>
<Text>
{this.state.txtStep}
</Text>
</View>
</View>
</ImageBackground>
);
}
async install() {
let url = this.state.url;
Setting.upsert({ key: 'bo_url', value: url });
if (!SyncController.checkConnection()) {
Alert.alert(
i18n.t('global.warning'),
i18n.t('error.must_be_connected_internet_sync')
);
return;
}
this.setState({ processing: true, txtStep: i18n.t("install.get_app_setting") });
const toDL = await InstallController.getConfig(url).then(response => {
let toDL = [];
if (response.data) {
if (response.data.status === "OK") {
_.forEach(response.data.datas, function (value, key) {
switch (key) {
case "MainColor":
Setting.upsert({ key: key, value: value });
break;
case "SquaredLogo":
case "RectangleLogo":
case "BackgroundImage":
case "HomeButtonImage":
toDL.push({
source: url + "/" + value,
dest: FileSystem.documentDirectory + key + '.png'
});
break;
}
});
return toDL;
} else {
Alert.alert(i18n.t("alert.error"), i18n.t("error.error_has_occurred"));
return false;
}
}
}).catch(function (error) {
console.log(error)
Alert.alert(i18n.t("alert.error"), i18n.t("error.error_has_occurred"));
return false;
});
for await (const file of toDL) {
await FileSystem.downloadAsync(
file.source,
file.dest
)
.then(({ uri }) => {
console.log('Finished downloading to ', uri);
})
.catch(error => {
Alert.alert(i18n.t("alert.error"), i18n.t("install.resource_dl_error") + file.dest);
console.error(error);
return false;
});
}
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.setIsSetup();
this.props.navigation.navigate('LoginScreen');
}
this.setState({ processing: false, txtStep: "" });
}
}
export default connect(mapStateToProps, mapDispatchToProps)(InstallScreen);