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

179 lines
6.6 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';
2023-01-03 12:55:28 +01:00
// SplashScreen
import * as SplashScreen from 'expo-splash-screen';
// Helpers
2022-09-29 15:03:47 +02:00
import Helpers from '../Lib/Helpers';
2023-01-02 12:09:11 +01:00
// Lodash
import _ from 'lodash';
// 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
2023-01-02 12:09:11 +01:00
import { User, Setting } from '../../Database';
// FileSytem
import * as FileSystem from 'expo-file-system';
function mapStateToProps(state) {
return {
isSetup: state.app.isSetup
};
}
2023-01-02 15:31:47 +01:00
const mapDispatchToProps = dispatch => {
return {
setIsSetup: () => dispatch({ type: 'SET_ISSETUP' }),
2023-01-03 12:55:28 +01:00
init: () => dispatch({ type: 'INIT' })
2023-01-02 15:31:47 +01:00
};
};
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",
}
2023-01-03 12:55:28 +01:00
componentDidMount() {
SplashScreen.hideAsync();
}
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() {
2023-01-02 12:09:11 +01:00
let url = this.state.url;
2023-01-02 15:31:47 +01:00
Setting.upsert({ key: 'bo_url', value: url });
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;
});
if (!isConnected) {
2023-01-02 12:09:11 +01:00
Alert.alert(
i18n.t('global.warning'),
i18n.t('error.must_be_connected_internet_sync')
);
return;
}
2022-10-07 18:27:54 +02:00
this.setState({ processing: true, txtStep: i18n.t("install.get_app_setting") });
2023-01-02 12:09:11 +01:00
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":
2023-01-02 16:48:48 +01:00
case "AppStoreUUID":
2023-01-02 12:09:11 +01:00
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;
}
2022-10-07 18:27:54 +02:00
}
2023-01-02 12:09:11 +01:00
}).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);
});
2023-01-03 12:55:28 +01:00
this.props.init();
2023-01-02 15:31:47 +01:00
this.props.setIsSetup();
2023-01-02 12:09:11 +01:00
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
}
}
2023-01-02 15:31:47 +01:00
export default connect(mapStateToProps, mapDispatchToProps)(InstallScreen);