import React, { useEffect } from 'react' import PropTypes from 'prop-types' import { motion, AnimatePresence } from "framer-motion" import { Formik, Form } from 'formik' import { Persist } from 'formik-persist' import * as Yup from 'yup' import ReCAPTCHA from "react-google-recaptcha"; import FormikControl from '../FormikControls' import ButtonPrimary from '../controls/ButtonPrimary' import { SubmitFormResult } from '../SubmitFormResult'; export const FormFeedback = (props) => { const { setPopupTitle, setPopupContent, setPopupOpened } = props useEffect(() => { if (setPopupTitle) { setPopupTitle('Some feedback form example') } }) const sections = [ { label: 'User support', value: 'users' }, { label: 'Partnership', value: 'partners' }, ] const support_types = [ { label: 'Consulting on product', value: 'product' }, { label: 'Technical support', value: 'technical' }, { label: 'Legal support', value: 'legal' }, ] const support_types_values = support_types.map(val => val.value) const supportTypesMotionVariants = { hidden: { opacity: 0, scaleY: 0, transition: { type: "linear", duration: .2 } }, visible: { opacity: 1, scaleY: 1, transition: { type: "linear", duration: .2 } }, } const initialValues = { user_name: '', email: '', section: '', support_type: [], message: '', } const validationSchema = Yup.object({ user_name: Yup.string() .required('Required'), email: Yup.string() .required('Required') .email('Must be a valid email address'), section: Yup.mixed() .required('Required') .oneOf(['users', 'partners'], 'Invalid section'), support_type: Yup.array(Yup.string()).ensure().when('section', { is: 'users', then: Yup.array().required('Required').test('is-valid-support-type', 'Invalid support type', val => { const sb = val.filter(n => !support_types_values.includes(n)) return 0 === sb.length }), }), message: Yup.string() .required('Required') .max(500, val => `Maximum ${val.max} symbols`), }) const recaptchaRef = React.useRef(); const onSubmitWithReCAPTCHA = async (values, { resetForm }) => { setPopupOpened(false) if (recaptchaRef.current && recaptchaRef.current.executeAsync) { let token try { await Promise.race([ new Promise((_, reject) => setTimeout(() => { reject(new Error('timeout')) }, 5000)), recaptchaRef.current.executeAsync(), ]) .then((v) => { token = v }) } catch (error) { console.error('app error - catch in try around Promise.race', error) token = false } if (token) { values.recaptcha = token // /* // <-- Stub code begin! console.log('values', values) resetForm() resetForm() //api bug workaround - need to call reset twice setPopupContent() setPopupOpened(true) // */ // <-- Stub code end! /* // <-- Example code begin! const res = await someApiCall(values) if ('success' === res.state) { resetForm() resetForm() //api bug workaround - need to call reset twice setFormState('success') return } else if ('error' === res.state) { setFormMessage(res.payload) } else { setFormMessage('Incorrect server response') } // */ // <-- Example code end! } else { console.error('app error - reCaptcha token is empty') alert('reCaptcha token is empty') } } else { console.error('app error - reCaptcha is not rendered') alert('reCaptcha is not rendered') } } return ( {formik => { return (
{'users' === formik.values.section && }
{(!formik.isValid || !formik.dirty) ?
Please fill all necessary form fields
:
This site is protected by reCAPTCHA and the Google
}
) }}
) } export default FormFeedback FormFeedback.propTypes = { setPopupTitle: PropTypes.func, setPopupContent: PropTypes.func, setPopupOpened: PropTypes.func, close: PropTypes.func, }