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

148 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-10-05 18:03:52 +02:00
import React from 'react';
import { View, Button, Alert, Text } from 'react-native';
// Redux
import { connect } from 'react-redux';
// Style
import styles from '../../Resources/Styles/SyncStyle';
// Translations
import i18n from "../../I18n";
// Controller
import SyncController from '../Controllers/SyncController';
// Progress
import * as Progress from 'react-native-progress';
// Database
import { User, Contact, Directory } from '../../Database';
2022-10-05 18:03:52 +02:00
function mapStateToProps(state) {
return {
documents: state.app.documents,
syncVersion: state.app.syncVersion,
currentUser: state.app.currentUser,
2022-10-05 18:03:52 +02:00
};
}
class SyncScreen extends React.Component {
state = {
progress: 0,
txtStep: '',
downloadProgress: 0,
downloadingFile: false,
processing: false,
newApp: false
};
render() {
const fileProgress = this.fileProgress();
return (
<View style={styles.container}>
<Progress.Circle
size={250}
progress={this.state.progress}
showsText={true}
color={'#ce3029'}
/>
<View style={styles.btnWrapper}>
<Button
disabled={this.state.processing}
color={'#ce3029'}
title={i18n.t('sync_s.start_sync')}
onPress={() => this.sync()}
/>
</View>
<View style={styles.btnWrapper}>
<Text>
{this.state.txtStep} {fileProgress}
</Text>
</View>
</View>
);
}
async sync() {
// if (!SyncController.checkConnection()) {
// Alert.alert(
// i18n.t('global.warning'),
// i18n.t('error.must_be_connected_internet_sync')
// );
// return;
// }
// this.setState({ processing: true });
// // getVersion
// const resVersion = await SyncController.getVersion().then(res => {
// return res.data;
// }).catch(error => {
// console.log(error);
// Alert.alert(
// i18n.t('error.error'),
// i18n.t('error.error_has_occurred')
// );
// });
// this.props.dispatch({
// type: 'UPDATE_VERSION',
// data: resVersion.version,
// });
// this.props.dispatch({
// type: 'UPDATE_SHOW_IS_ACTIVE',
// data: resVersion.ShowIsActive,
// });
// this.setState({ newApp: resVersion.newApp });
// // getUsers
// this.setState({ txtStep: i18n.t('sync_s.updating_users') + '...' });
2022-10-05 18:03:52 +02:00
// 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')
// );
// });
// resUsers.forEach(user => {
// User.add(user);
// });
// // getContacts
// this.setState({ progress: 0.1, txtStep: i18n.t('sync_s.updating_contacts') + '...' });
// const resContacts = await SyncController.getContacts().then(res => {
// Contact.clear();
2022-10-05 18:03:52 +02:00
// return res.data;
// }).catch(error => {
// console.log(error);
// Alert.alert(
// i18n.t('error.error'),
// i18n.t('error.error_has_occurred')
// );
// });
// resContacts.forEach(contact => {
// Contact.add(contact);
// });
// sendBrochures
// this.setState({ progress: 0.2, txtStep: i18n.t('sync_s.sending_flyers') + '...' });
// await SyncController.sendBrochures();
// getDirectories
this.setState({ progress: 0.3, txtStep: i18n.t('sync_s.updating_folders') + '...' });
const resDirectories = await SyncController.getDirectories().then(res => {
Directory.clear();
return res.data;
}).catch(error => {
console.log(error);
Alert.alert(
i18n.t('error.error'),
i18n.t('error.error_has_occurred')
);
});
resDirectories.forEach(dir => {
Directory.add(dir);
});
2022-10-05 18:03:52 +02:00
// End
this.setState({ txtStep: i18n.t('sync_s.update_completed') + '.', processing: false });
}
fileProgress() {
if (this.state.downloadingFile === true) {
return this.state.downloadProgress + ' %';
}
}
}
export default connect(mapStateToProps)(SyncScreen);