import React, {Component} from 'react';
import {
  StyleSheet,
  Text,
  View,
  Alert,
  AsyncStorage,
  TextInput,
  TouchableHighlight,
} from 'react-native';
var Dimensions = require('Dimensions');
import {Actions} from 'react-native-router-flux';
import {BASE_URL} from './common/Constants';

class MainView extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      inputText: '',
    };
  }

  _clean() {
    this.setState(
      {
        inputText: '',
      });
  }

  _preview() {
    if (!this._checkAndAlert()) {
      return;
    }
    Actions.PreviewView({url: this.state.inputText});
  }

  _checkAndAlert() {
    if (!this.state.inputText || 0 === this.state.inputText) {
      Alert.alert(
        '',
        '请输入推送的网址',
        [
          {text: 'OK', onPress: () => console.log('OK Pressed')},
        ],
        {cancelable: false}
      );
      return false;
    }
    return true;
  }

  _goToSetting() {
    Actions.Setting();
  }

  _send() {
    if (!this._checkAndAlert()) {
      return;
    }
    AsyncStorage.getItem("from_email").then((value) => {
      this.setState({from_email: value});
      if (value == null) {
        this._goToSetting();
        return;
      }
    }).done();
    AsyncStorage.getItem("to_email").then((value) => {
      this.setState({receive_email: value});
      if (value == null) {
        this._goToSetting();
        return;
      }
    }).done();

    this.setState({
      isLoading: true,
    });

    var post_data = {
      url: this.state.inputText,
      from_email: this.state.from_email,
      to_email: this.state.receive_email
    }

    var reqUrl = BASE_URL + "send/url";

    fetch(reqUrl, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(post_data)
    }).then((response) => response.json())
      .then((responseText) => {
        console.log(responseText);
        if (responseText.status != 0) {
          ToastAndroid.show(responseText.msg, ToastAndroid.SHORT);
        } else {
          ToastAndroid.show("发送成功", ToastAndroid.SHORT);
        }
        this.setState({
          isLoading: false,
        });
      })
      .catch((error) => {
        this.setState({
          isLoading: false,
        });
      });

  }

  render() {
    return (
      <View style={styles.container}>
        <TextInput
          style={styles.textInput}
          textAlignVertical="top"
          multiline={true}
          onChangeText={(input) => this.setState({inputText:input})}
          value={this.state.inputText}
        />
        <View style={{
                        width:Dimensions.get('window').width,
                        flexDirection:'row',
                        height: 50,
                        }
                    }>
          <TouchableHighlight
            style={styles.touchable}
            underlayColor="#1976D2"
            onPress={this._clean.bind(this)}>
            <Text style={styles.button}>
              清除内容
            </Text>
          </TouchableHighlight>

          <TouchableHighlight
            style={styles.touchable}
            underlayColor="#1976D2"
            onPress={this._preview.bind(this)}>
            <Text style={styles.button}>
              预览内容
            </Text>
          </TouchableHighlight>
        </View>
        <View style={{
          height: 55,
          width:Dimensions.get('window').width,
          marginTop:8,
        }}>
          <TouchableHighlight
            style={styles.touchable}
            underlayColor="#1976D2"
            onPress={this._send.bind(this)}>
            <Text style={styles.button}>
              发送到我的kindle
            </Text>
          </TouchableHighlight>
        </View>
        <View style={{
          flex:1,
        }}></View>
      </View>
    );
  }
}

var styles = StyleSheet.create({
  container: {
    backgroundColor: '#fafafa',
    flex: 1,
  },
  toolbar: {
    backgroundColor: '#03A9F4',
    height: 56,
  },
  textInput: {
    paddingLeft: 4,
    fontSize: 20,
    height: 220,
    borderColor: 'gray',
    borderWidth: 1,
  },
  touchable: {
    flex: 1,
    backgroundColor: '#03A9F4',
    alignSelf: 'stretch',
    margin: 2,
    height: 50,
    alignItems: 'center',
    justifyContent: 'center'
  },
  button: {
    color: '#FFFFFF',
  },
  center: {
    justifyContent: 'center',
    alignItems: 'center',
  },
});


module.exports = MainView;

@@ -1686,301 +1686,202 @@
     
-fetch(reqUrl, %7B%0A      method: 'POST',%0A      headers: %7B%0A        'Accept': 'application/json',%0A        'Content-Type': 'application/json',
+axios.post(reqUrl, post_data)%0A      .then(function (response) %7B%0A        console.log(response);%0A        this.setState(%7B
 %0A      
-%7D,%0A
     
-  body: JSON.stringify(post_data)%0A    %7D).then((response) =%3E response.json())%0A      .then((responseText) =%3E %7B%0A        console.log(responseText);
+isLoading: false,%0A        %7D);%0A%0A        Alert.alert(%0A          '',
 %0A   
@@ -1889,20 +1889,18 @@
     
-if (
+  
 response
 Text
@@ -1899,20 +1899,16 @@
 onse
-Text
 .status 
 != 0
@@ -1907,52 +1907,32 @@
 tus 
-!= 0) %7B%0A          ToastAndroid.show(
+== 0 ? %22%E5%8F%91%E9%80%81%E6%88%90%E5%8A%9F%22 : 
 response
 Text
@@ -1931,38 +1931,13 @@
 onse
-Text
 .msg,
- ToastAndroid.SHORT);
 %0A   
@@ -1945,16 +1945,11 @@
     
-%7D else %7B
+  %5B
 %0A   
@@ -1959,116 +1959,107 @@
     
-ToastAndroid.show(%22%E5%8F%91%E9%80%81%E6%88%90%E5%8A%9F%22, ToastAndroid.SHORT);%0A        %7D%0A        this.setState(%7B%0A          isLoading: false,
+  %7Btext: 'OK', onPress: () =%3E console.log('OK Pressed')%7D,%0A          %5D,%0A          %7Bcancelable: true%7D
 %0A   
@@ -2055,36 +2055,36 @@
 : true%7D%0A        
-%7D
 );%0A
+%0A
       %7D)%0A      .
@@ -2093,20 +2093,54 @@
 tch(
-(error) =%3E %7B
+function (error) %7B%0A        console.log(error);
 %0A   
@@ -2210,17 +2210,16 @@
     %7D);%0A
-%0A
   %7D%0A%0A  r

send logic