import React, { useState, useRef } from 'react'; import { FaMapPin, FaCalendarAlt, FaBriefcase } from 'react-icons/fa'; import styled from 'styled-components'; import { createGlobalStyle } from 'styled-components'; import IntlTelInput from 'intl-tel-input/reactWithUtils'; import 'intl-tel-input/styles'; import axios from 'axios'; import privacyPolicyData from "../../data/privacyPolicyData"; import { showCustomAlert, showCustomError, showCustomSuccess } from '../../utils/showalerts'; const GlobalStyles = createGlobalStyle` .iti__country { color: #333; background-color: #f8f9fa; } .iti__country:hover { background-color: #3f7ad3; color: white; } .iti__country.iti__active { background-color: #3f7ad3; color: white; } .flexer { display: grid; grid-template-columns: 1fr; } @media (min-width: 768px) { .flexer { grid-template-columns: 60% 40%; gap: 2rem; } } ul li::before { content: "\f00c"; font-family: "Font Awesome 5 Free"; font-weight: 900; position: absolute; left: 0; top: 50%; transform: translateY(-50%); color: green; } `; const FormContainer = styled.div` display: flex; flex-direction: column; align-items: center; max-width: 900px; margin-top: 2rem; width: 100%; `; const Form = styled.form` display: flex; flex-direction: column; gap: 1rem; width: 80%; max-height: fit-content !important; align-self: center; justify-self: center; background-color: #2d3748; padding: 24px; border-radius: 0.375rem; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); `; const Input = styled.input` width: 100%; padding: 0.75rem 1rem; font-size: 1rem; border: 3px solid ${(props) => (props.invalid ? '#de493e' : '#3267B9')}; border-radius: 0.375rem; background-origin: border-box; color: black; &:focus { outline: none; box-shadow: 0 0 8px rgba(63, 122, 211, 0.5); } `; const IntlTelInputWrapper = styled.div` width: 100%; .iti { width: 100%; color: black; } input { width: 100%; padding: 0.75rem 1rem; font-size: 1rem; border: 3px solid ${(props) => (props.invalid ? '#de493e' : '#3267B9')}; border-radius: 0.375rem; background-origin: border-box; color: black; &:focus { outline: none; box-shadow: 0 0 8px rgba(63, 122, 211, 0.5); } } `; const Textarea = styled.textarea` width: 100%; padding: 0.75rem 1rem; font-size: 1rem; border: 3px solid #3267B9; border-radius: 0.375rem; color: black; &:focus { outline: none; box-shadow: 0 0 8px rgba(63, 122, 211, 0.8); } `; const Select = styled.select` width: 100%; padding: 0.75rem 1rem; font-size: 1rem; border: 3px solid #3267B9; border-radius: 0.375rem; color: black; &:focus { outline: none; box-shadow: 0 0 8px rgba(63, 122, 211, 0.5); } `; const SubmitButton = styled.button` width: 100%; padding: 0.75rem 1.5rem; background: #3267B9; color: white; font-weight: 600; border-radius: 0.375rem; transition: background 0.3s; &:hover { background: #3f7ad3; } `; const FileInputWrapper = styled.div` position: relative; width: 100%; display: flex; flex-direction: column; align-items: center; `; const FileInputButton = styled.label` display: inline-block; padding: 0.75rem 1.5rem; background-color: #3f7ad3; color: white; border-radius: 30px; cursor: pointer; text-align: center; width: 100%; max-width: 250px; text-transform: uppercase; font-weight: 500; transition: background-color 0.3s ease; &:hover { background-color: #669df0; } `; const FileInput = styled.input` display: none; `; const FileNameDisplay = styled.span` margin-top: 0.5rem; font-size: 0.875rem; color: #A0AEC0; `; const ConsentWrapper = styled.div` display: grid; grid-template-columns: auto 1fr; gap: 0.5rem; color: #d5d5d5; `; const JobPageSection = ({ job }) => { const { title, location, status, postedDate, description, responsibilities = [], skillsRequired = [], desirableSkills = [], perks = [] } = job; const [phone, setPhone] = useState(''); const [isValid, setIsValid] = useState(false); const [fileName, setFileName] = useState(''); const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [experience, setExperience] = useState(''); const [role, setRole] = useState(title); // Set initial role as the job title const [linkedin, setLinkedin] = useState(''); const [consent, setConsent] = useState(false); // State to track consent const [errors, setErrors] = useState({}); // State for storing error messages const fieldRefs = useRef({ name: React.createRef(), email: React.createRef(), phone: React.createRef(), linkedin: React.createRef(), experience: React.createRef(), }); const validateName = (name) => name.length >= 3; const validateEmail = (email) => /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(email); const validatePhone = () => isValid; const validateLinkedin = (linkedin) => /^https?:\/\/(www\.)?linkedin\.com\//.test(linkedin); const formatStatus = (status) => status === "OPENED" ? "Open" : "Closed"; const formatDate = (date) => { const options = { year: 'numeric', month: 'long', day: 'numeric' }; return new Date(date).toLocaleDateString(undefined, options); }; const handleInputChange = (e, setter, fieldName) => { setter(e.target.value); setErrors({}); }; const handleBlur = (fieldName) => { const validationErrors = {}; if (fieldName === 'name' && !validateName(name)) { validationErrors.name = 'Name should be at least 3 characters long.'; } if (fieldName === 'email' && !validateEmail(email)) { validationErrors.email = 'Invalid email format.'; } if (fieldName === 'phone' && !validatePhone()) { validationErrors.phone = 'Please enter a valid phone number.'; } if (fieldName === 'linkedin' && !validateLinkedin(linkedin)) { validationErrors.linkedin = "LinkedIn URL should start with 'https://www.linkedin.com'."; } setErrors(validationErrors); console.log(isValid); if (Object.keys(validationErrors).length > 0) { console.log(fieldRefs.current[fieldName]); fieldRefs.current[fieldName].current.focus(); } }; const handleFileChange = (e) => { const file = e.target.files[0]; const validExtensions = ['pdf', 'docx', 'doc']; if (file) { const fileExtension = file.name.split('.').pop().toLowerCase(); if (!validExtensions.includes(fileExtension)) { showCustomError("Invalid file type. Please upload a .pdf, .docx, or .doc file."); setFileName(''); e.target.value = ''; return; } setFileName(file.name); } }; const handleSubmit = async (e) => { e.preventDefault(); if (!name || !email || !phone || !experience || !fileName || !linkedin) { showCustomError("Please fill out all required fields."); return; } const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; if (!emailRegex.test(email)) { showCustomError("Please enter a valid email address."); return; } if (!isValid) { showCustomError("Please enter a valid phone number."); return; } //Set the policy version number const policyVersion = privacyPolicyData.version; try { const formData = new FormData(); formData.append('name', name); formData.append('email', email); formData.append('phone', phone); formData.append('experience', experience); formData.append('linkedin', linkedin); formData.append('consent', consent); formData.append('policyVersion', policyVersion); formData.append('role', role); // Add role to the form data formData.append('resume', document.getElementById('fileInput').files[0]); showCustomAlert("Please wait.... Your submission is in progress"); const response = await axios.post('/api/submit-job-application', formData, { headers: { 'Content-Type': 'multipart/form-data' } }); if (response.status >= 200 && response.status < 300) { // Assuming the backend sends a success message in the response data const successMessage = response.data.message || "Your application has been submitted successfully!"; showCustomSuccess(successMessage); // Optionally clear the form fields here // setYourState(''); } else { // Handle cases where the response status is not successful const errorMessage = response.data?.message || "An unexpected error occurred. Please try again."; showCustomError(errorMessage); } // Reset form fields setName(''); setEmail(''); setPhone(''); setExperience(''); setFileName(''); setLinkedin(''); setConsent(false); document.getElementById("fileInput").value = ''; } catch (error) { console.error('Error:', error); // Reset form fields setName(''); setEmail(''); setPhone(''); setExperience(''); setFileName(''); setLinkedin(''); setConsent(false); if (error.response.data.error ==="Unable to send email for the confirmation of submission, but the application was received suvvessfully!"){ showCustomAlert("Unable to send email for the confirmation of submission, but the application was received suvvessfully!"); }else{ showCustomError(error.response.data.error || "An unexpected error occurred. Please try again."); } } }; return ( <>

{title}

{location}

{formatStatus(status)}

Posted on {formatDate(postedDate)}

Job Description

{description}

Responsibilities

    {responsibilities.map((item, index) => (
  • {item}
  • ))}

Skills Required

    {skillsRequired.map((item, index) => (
  • {item}
  • ))}

Desirable Skills

    {desirableSkills.map((item, index) => (
  • {item}
  • ))}

What We Offer

    {perks.map((item, index) => (
  • {item}
  • ))}
{status === "OPENED" && (

Apply for This Position

Fill in your details and upload your resume.

{ // Only allow letters (including spaces and accented characters) const regex = /^[A-Za-z\s]*$/; if (regex.test(e.target.value) || e.target.value === '') { handleInputChange(e, setName, 'name'); } }} invalid={errors.name} onBlur={() => handleBlur('name')} required /> {errors.name && {errors.name}} handleInputChange(e, setEmail, 'email')} invalid={errors.email} onBlur={() => handleBlur('email')} placeholder="Your Email Address" required /> {errors.email && {errors.email}} handleBlur('phone')} /> {errors.phone && {errors.phone}} {" "} {/*
*
*/} {/* Role Selection Dropdown */}
{/* strategy Field */} handleInputChange(e, setLinkedin, 'linkedin')} invalid={errors.linkedin} onBlur={() => handleBlur('linkedin')} placeholder="Provide us your LinkedIn to know you more" required > {errors.linkedin && {errors.linkedin}}
Choose File {fileName && ( {fileName} )}
{/* Consent Checkbox */} setConsent(!consent)} required /> Submit Application
)}
); }; export default JobPageSection;