import React, { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import styles from '../css/ApplicationFormCss/ApplicationForm.module.css'; import modalStyles from '../css/ApplicationFormCss/AlertModal.module.css'; function ApplicationForm() { const [cv, setCv] = useState(null); const [motivationalLetter, setMotivationalLetter] = useState(''); const [phoneNumber, setPhoneNumber] = useState(''); const [email, setEmail] = useState(''); const [showModal, setShowModal] = useState(false); const [isLoggedIn, setIsLoggedIn] = useState(false); const navigate = useNavigate(); useEffect(() => { const token = localStorage.getItem('token'); setIsLoggedIn(!!token); const storedEmail = localStorage.getItem('email'); const storedPhoneNumber = localStorage.getItem('phoneNumber'); setEmail(storedEmail || ''); setPhoneNumber(storedPhoneNumber); }, []); const handleCvChange = (e) => { setCv(e.target.files[0]); }; const handleMotivationalLetterChange = (e) => { setMotivationalLetter(e.target.value); }; const handlePhoneNumberChange = (e) => { setPhoneNumber(e.target.value); }; const handleEmailChange = (e) => { setEmail(e.target.value); }; const handleSubmit = async (e) => { e.preventDefault(); try { const token = localStorage.getItem('token'); const formData = new FormData(); formData.append('cv', cv); formData.append('motivational_letter', motivationalLetter); formData.append('phone_number', phoneNumber); formData.append('email',email); const response = await fetch('http://localhost:8080/api/apply', { method: 'POST', headers: { 'Authorization': `Bearer ${token}` }, body: formData, }); if (response.ok) { setShowModal(true); setTimeout(() => { setShowModal(false); setCv(null); setMotivationalLetter(''); setPhoneNumber(''); setEmail(''); navigate('/'); }, 2500); } else { alert('Failed to submit application'); } } catch (error) { console.error('Error submitting application:', error); alert('An error occurred while submitting the application.'); } }; if (!isLoggedIn) { return (
Your application has been submitted successfully. You will be redirected to the homepage shortly.