/* 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 { 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: {}, }; } async componentDidMount() { let host = await AsyncStorage.getItem(AppStorage.ELECTRUM_HOST); let port = await AsyncStorage.getItem(AppStorage.ELECTRUM_TCP_PORT); this.setState({ isLoading: false, host, port, }); await this.setState({ config: await BlueElectrum.getConfig(), }); } save = () => { this.setState({ isLoading: true }, async () => { this.state.host = this.state.host ? this.state.host : ''; this.state.port = this.state.port ? this.state.port : ''; try { if (!this.state.host && !this.state.port) { await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, ''); await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, ''); alert('Your changes have been saved successfully. Restart may be required for changes to take effect.'); } else if (!(await BlueElectrum.testConnection(this.state.host, this.state.port))) { alert("Can't connect to provided Electrum server"); } else { await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, this.state.host); await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, this.state.port); alert('Your changes have been saved successfully. Restart may be required for changes to take effect.'); } } catch (_) {} this.setState({ isLoading: false }); }); }; render() { return ( {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.state.isLoading ? : } Currently using: Host: {this.state.config.host} Port: {this.state.config.port} Connected: {(this.state.config.status === 1 && 'Yes') || 'No'} ); } } ElectrumSettings.propTypes = { navigation: PropTypes.shape({ navigate: PropTypes.func, goBack: PropTypes.func, }), };