import propTypes from 'prop-types'; import img from '../../assets/Group 305.png'; import * as Yup from 'yup'; import { Formik, Form, ErrorMessage } from 'formik'; import { JobFormContext } from '../../context'; import { useContext, useState, useRef, useEffect } from 'react'; import * as emailjs from 'emailjs-com'; import { motion } from 'framer-motion'; import ReactGA from 'react-ga4'; import MyDropzone from './MyDropzone'; import HashPositions from './HashPositions'; import ReCAPTCHA from 'react-google-recaptcha'; import axios from 'axios'; export default function JobForm(props) { const [sucMsg, setSucMsg] = useState(false); const captchaRef = useRef(null); const captchaRef2 = useRef(null); const [msgText, setMsgText] = useState(''); const cntCareersJobs = props.cntCareers; let defaultPositionSelection = props.defaultPositionSelection; //search context for prevous entry TODO const { jobForm, setJobForm } = useContext(JobFormContext); const [otherInputState, setOtherInputState] = useState(true); const [selectedPosition, setSelectedPosition] = useState(''); const [errorMsg, setErrorMsg] = useState(''); const fileInput = useRef(); function changeFormHandler(event) { const { name, value } = event.target; setJobForm({ ...jobForm, [name]: value, }); } function dropzoneToFormData(files) { setJobForm({ ...jobForm, file: files, }); } function hashToFormData(selected, other) { setJobForm({ ...jobForm, position: selected, other: other, }); } useEffect(() => { if (jobForm.file !== '') { setErrorMsg(''); } }, [jobForm.file]); const validationSchema = Yup.object({ email: Yup.string().email('Invalid email format').required('Email is Required'), firstName: Yup.string() .min(2, 'First name too short') .max(50, 'First name too long') .required('First Name is Required'), lastName: Yup.string() .min(2, 'Last name too short') .max(50, 'Last name too long') .required('Last name is Required'), coverLetter: Yup.string() .trim() .min(2, 'Cover Letter too short') .required('Cover Letter is Required'), }); return (
Position: ${selectedPosition} ${values.other}
Cover letter: ${values.coverLetter}
Link (optional): ${values.link}
`, // attachment: file, // }); // } else // setMsgText('Please fill reCAPTCHA and try again. Thank you!'); // }) // .catch(error => { // console.log(error); // }); //} //} prepFile(jobForm.file).then(async res => { const newtemplateParams = { Position: values.position, Other: values.other, Firstname: values.firstName, Lastname: values.lastName, Coverletter: values.coverLetter, Email: values.email, Link: values.link, File: res, }; const token = captchaRef.current.getValue() || captchaRef2.current.getValue(); captchaRef.current.reset(); captchaRef2.current.reset(); if (token.length === 0) { setSucMsg(true); setMsgText('Please fill reCAPTCHA and try again. Thank you!'); } else { await axios .post(`${process.env.REACT_APP_CAPTCHA_API}/verify-token`, { token, }) .then(res => { setSucMsg(true); if (res.data.data.success) { setMsgText('Submission Succesful! Thank you!'); emailjs .send( process.env.REACT_APP_SERVICE_ID, process.env.REACT_APP_JOB_TEMPLATE_ID, newtemplateParams, process.env.REACT_APP_USER_ID, ) .then( result => { console.log(result.text); setSucMsg(true); }, error => { console.log(error.text); }, ); } else setMsgText('Please fill reCAPTCHA and try again. Thank you!'); }) .catch(error => { console.log(error); }); } }); } ReactGA.event('contact', { category: 'Contact', action: 'Job application', }); }} > {props => ( )}