text: import React,{useState}from 'react';
  import {StyleSheet,View} from "react-native";
  import {Input, Button} from "react-native-elements";
  import * as firebase from "firebase";
  import { validateEmail } from "../../utils/validations";
  import {reauthenticate} from "../../utils/api";
  
  
  export default function ChangeEmailForm(props){
      const {email,setShowModal,toastRef,setReloadUserInfo} = props;
      const [formData, setFormData] = useState(defaultValue());
      const [showPassword, setShowPassword] = useState(false);
      const [errors, setErrors] = useState({});
      const [isloading, setIsloading] = useState(false);
      const onChange = (e,type) =>{
          setFormData({...formData, [type]: e.nativeEvent.text})
      }
      const onSubmit = () => {
          setErrors({});
          if(!formData.email || email === formData.email){
              setErrors({
                  email:"El Email no ha cambiado"
              });
          }else if (!validateEmail(formData.email)){
              setErrors({
                  email:"El Email Es Incorrecto"
              });
          } else if(!formData.password){
              setErrors({
                  password:"La contraseña no puede estar vacia"
              });
              
          }else{
              setIsloading(true);
              reauthenticate(formData.password).then(() =>{
                  firebase.auth().currentUser.updateEmail(formData.email).then(()=>{
                      setIsloading(false);
                      setReloadUserInfo(true);
                      toastRef.current.show("Email Actualizado Correctamente");
                      setShowModal(false);
                  }).catch(()=>{
                      setErrors({email:"Error al actualizar Email."});
                      setIsloading(false);
                  })
              }).catch(()=>{
                  setIsloading(false);
                  setErrors({password:"La Contraseña no es correcta."})
              })
          }
      };
      return(
          <View style={styles.view}>
              <Input 
              placeholder="Nuevo Correo Electronico"
              containerStyle={styles.input}
              //defaultValue={email || ""}
              rightIcon={{
                  type: "material-community",
                  name:"at",
                  color:"#c2c2c2",
              }}
              onChange={(e)=>onChange(e,"email")}
              errorMessage={errors.email}
              />
              <Input
              placeholder="Confirmar con Contraseña"
              containerStyle={styles.input}
              password={true}
              secureTextEntry={showPassword ? false : true}
              rightIcon={{
                  type:"material-community",
                  name:showPassword ? "eye-off-outline": "eye-outline",
                  color:"#c2c2c2",
                  onPress:()=> setShowPassword(!showPassword)
              }}
              onChange={(e)=>onChange(e,"password")}
              errorMessage={errors.password}
              />
              <Button
                  title="Cambiar Email"
                  containerStyle={styles.btnContainer}
                  buttonStyle={styles.btn}
                  onPress={onSubmit}
                  loading={isloading}
              />
  
          </View>
      )
  
  }
  function defaultValue(){
      return{
          email:"",
          password:""
      }
  }
  const styles= StyleSheet.create({
  
      view:{
          alignItems:"center",
          paddingTop: 10,
          paddingBottom: 10,
      },
      input:{
          marginBottom: 10,
      },
      btnContainer:{
          marginTop: 20 ,
          width:"95%",
      },
      btn:{
          backgroundColor:"#9acd32"
      }
  })
  