import axios from "axios";
const isValidEmail = email => {
console.log(email);
const endpoint = process.env.REACT_APP_EMAIL_VALIDATION;
const url = `${endpoint}&email=${email}`;
return axios.get(url).then(result => {
return result.data.format_valid;
});
};
export default isValidEmail;
import React from "react";
import FormEmailInput from "./FormEmailInput";
import { Field } from "redux-form";
import withDisabled from "components/connectors/withDisabled";
import PropTypes from "prop-types";
import { flatten } from "lodash";
import hasValue from "utilities/hasValue";
import isValidEmail from "utilities/isValidEmail";
export class FormGroupField extends React.Component {
requiredValidation = value => {
const { formDisabled } = this.context;
const { disabled, required } = this.props;
if (formDisabled || disabled) return;
if (!required || hasValue(value)) return;
// if (isValidEmail(value)) {
// return;
// } else {
// return "Invalid Format";
// }
return "This field is required";
};
emailValidation = value => {
return isValidEmail(value);
};
render() {
const { validate } = this.props;
console.log(isValidEmail("mail@gmail.com"));
return (
<FormEmailInput
renderChildren={Field}
{...this.props}
validate={flatten([
this.requiredValidation,
this.emailValidation,
validate
])}
/>
);
}
}
FormGroupField.contextTypes = {
formDisabled: PropTypes.bool
};
FormGroupField.defaultProps = {
required: false,
disabled: false, // only disable the built-in validation
validate() {} // validation still works here
};
export default withDisabled(FormGroupField);
Promise {<pending>}
isValidEmail("mail@gmail.com").then(result => someAction(result));
async someHandler() {
const result = await isValidEmail("mail@gmail.com");
return someAction(result);
}
async someOtherHandler() {
const result = await isValidEmail("mail@gmail.com");
/* вызов someOtherAsyncAction тоже возвращает Promise, поэтому ждем
и возвращаем только когда придет результат, для этого используем
ключевое слово await */
return await someOtherAsyncAction(result);
}
isValidEmail("mail@gmail.com")
.then( (result) => {
console.log(result);
})
.catch( (err) => {
console.log(err);
})