/* global alert */ import React, { Component } from 'react'; import { View, TextInput } from 'react-native'; import { AppStorage } from '../../class'; import AsyncStorage from '@react-native-community/async-storage'; import { ScrollView } from 'react-native-gesture-handler'; import { BlueLoading, BlueSpacing20, BlueButton, SafeBlueArea, BlueCard, BlueNavigationStyle, BlueText } from '../../BlueComponents'; import PropTypes from 'prop-types'; let loc = require('../../loc'); let BlueElectrum = require('../../BlueElectrum'); export default class ElectrumSettings extends Component { static navigationOptions = () => ({ ...BlueNavigationStyle(), title: loc.settings.electrum_settings, }); constructor(props) { super(props); this.state = { isLoading: true, config: {}, }; } componentWillUnmount() { clearInterval(this.state.inverval); } async componentDidMount() { let host = await AsyncStorage.getItem(AppStorage.ELECTRUM_HOST); let port = await AsyncStorage.getItem(AppStorage.ELECTRUM_TCP_PORT); let sslPort = await AsyncStorage.getItem(AppStorage.ELECTRUM_SSL_PORT); this.setState({ isLoading: false, host, port, sslPort, }); const inverval = setInterval(async () => { this.setState({ config: await BlueElectrum.getConfig(), }); }, 1000); this.setState({ config: await BlueElectrum.getConfig(), inverval, }); } checkServer = async () => { this.setState({ isLoading: true }, async () => { const features = await BlueElectrum.serverFeatures(); alert(JSON.stringify(features, null, 2)); this.setState({ isLoading: false }); }); }; save = () => { const host = this.state.host ? this.state.host : ''; const port = this.state.port ? this.state.port : ''; const sslPort = this.state.sslPort ? this.state.sslPort : ''; this.setState({ isLoading: true }, async () => { try { if (!host && !port && !sslPort) { await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, ''); await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, ''); await AsyncStorage.setItem(AppStorage.ELECTRUM_SSL_PORT, ''); alert('Your changes have been saved successfully. Restart may be required for changes to take effect.'); } else if (!(await BlueElectrum.testConnection(host, port, sslPort))) { alert("Can't connect to provided Electrum server"); } else { await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, host); await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, port); await AsyncStorage.setItem(AppStorage.ELECTRUM_SSL_PORT, sslPort); alert('Your changes have been saved successfully. Restart may be required for changes to take effect.'); } } catch (_) {} this.setState({ isLoading: false }); }); }; render() { return ( Status {(this.state.config.status === 1 && 'Connected') || 'Not Connected'} {this.state.config.host}:{this.state.config.port} {loc.settings.electrum_settings_explain} this.setState({ host: text })} numberOfLines={1} style={{ flex: 1, marginHorizontal: 8, minHeight: 36, height: 36 }} editable={!this.state.isLoading} underlineColorAndroid="transparent" /> this.setState({ port: text })} numberOfLines={1} style={{ flex: 1, marginHorizontal: 8, minHeight: 36, height: 36 }} editable={!this.state.isLoading} underlineColorAndroid="transparent" /> this.setState({ sslPort: text })} numberOfLines={1} style={{ flex: 1, marginHorizontal: 8, minHeight: 36, height: 36 }} editable={!this.state.isLoading} underlineColorAndroid="transparent" /> {this.state.isLoading ? : } ); } } ElectrumSettings.propTypes = { navigation: PropTypes.shape({ navigate: PropTypes.func, goBack: PropTypes.func, }), };