[d7b7f00] | 1 | import React, { useState, useEffect } from 'react';
|
---|
| 2 | import { useNavigate } from 'react-router-dom';
|
---|
| 3 | import styles from '../css/ApplicationFormCss/ApplicationForm.module.css';
|
---|
| 4 | import modalStyles from '../css/ApplicationFormCss/AlertModal.module.css';
|
---|
| 5 |
|
---|
| 6 | function ApplicationForm() {
|
---|
| 7 | const [cv, setCv] = useState(null);
|
---|
| 8 | const [motivationalLetter, setMotivationalLetter] = useState('');
|
---|
| 9 | const [phoneNumber, setPhoneNumber] = useState('');
|
---|
| 10 | const [email, setEmail] = useState('');
|
---|
| 11 | const [showModal, setShowModal] = useState(false);
|
---|
| 12 | const [isLoggedIn, setIsLoggedIn] = useState(false);
|
---|
| 13 | const navigate = useNavigate();
|
---|
| 14 |
|
---|
| 15 | useEffect(() => {
|
---|
| 16 | const token = localStorage.getItem('token');
|
---|
| 17 | setIsLoggedIn(!!token);
|
---|
| 18 |
|
---|
| 19 | const storedEmail = localStorage.getItem('email');
|
---|
| 20 | const storedPhoneNumber = localStorage.getItem('phoneNumber');
|
---|
| 21 |
|
---|
| 22 | setEmail(storedEmail || '');
|
---|
| 23 | setPhoneNumber(storedPhoneNumber);
|
---|
| 24 | }, []);
|
---|
| 25 |
|
---|
| 26 | const handleCvChange = (e) => {
|
---|
| 27 | setCv(e.target.files[0]);
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | const handleMotivationalLetterChange = (e) => {
|
---|
| 31 | setMotivationalLetter(e.target.value);
|
---|
| 32 | };
|
---|
| 33 |
|
---|
| 34 | const handlePhoneNumberChange = (e) => {
|
---|
| 35 | setPhoneNumber(e.target.value);
|
---|
| 36 | };
|
---|
| 37 |
|
---|
| 38 | const handleEmailChange = (e) => {
|
---|
| 39 | setEmail(e.target.value);
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | const handleSubmit = async (e) => {
|
---|
| 43 | e.preventDefault();
|
---|
| 44 | try {
|
---|
| 45 | const token = localStorage.getItem('token');
|
---|
| 46 |
|
---|
| 47 | const formData = new FormData();
|
---|
| 48 | formData.append('cv', cv);
|
---|
| 49 | formData.append('motivational_letter', motivationalLetter);
|
---|
| 50 | formData.append('phone_number', phoneNumber);
|
---|
| 51 | formData.append('email',email);
|
---|
| 52 |
|
---|
| 53 | const response = await fetch('http://localhost:8080/api/apply', {
|
---|
| 54 | method: 'POST',
|
---|
| 55 | headers: {
|
---|
| 56 | 'Authorization': `Bearer ${token}`
|
---|
| 57 | },
|
---|
| 58 | body: formData,
|
---|
| 59 | });
|
---|
| 60 |
|
---|
| 61 | if (response.ok) {
|
---|
| 62 | setShowModal(true);
|
---|
| 63 | setTimeout(() => {
|
---|
| 64 | setShowModal(false);
|
---|
| 65 | setCv(null);
|
---|
| 66 | setMotivationalLetter('');
|
---|
| 67 | setPhoneNumber('');
|
---|
| 68 | setEmail('');
|
---|
| 69 | navigate('/');
|
---|
| 70 | }, 2500);
|
---|
| 71 | } else {
|
---|
| 72 | alert('Failed to submit application');
|
---|
| 73 | }
|
---|
| 74 | } catch (error) {
|
---|
| 75 | console.error('Error submitting application:', error);
|
---|
| 76 | alert('An error occurred while submitting the application.');
|
---|
| 77 | }
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | if (!isLoggedIn) {
|
---|
| 81 | return (
|
---|
| 82 | <div className={styles.formContainer}>
|
---|
| 83 | <div className={styles.messageContainer}>
|
---|
| 84 | <h2 className={styles.formTitle}>Please log in to apply for the delivery position.</h2>
|
---|
| 85 | <button onClick={() => navigate('/login')} className={styles.loginButton}>
|
---|
| 86 | Go to Login
|
---|
| 87 | </button>
|
---|
| 88 | </div>
|
---|
| 89 | </div>
|
---|
| 90 | );
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return (
|
---|
| 94 | <div className={styles.formContainer}>
|
---|
| 95 | <form onSubmit={handleSubmit} className={styles.applicationForm}>
|
---|
| 96 | <h2 className={styles.formTitle}>Apply for Delivery Position</h2>
|
---|
| 97 |
|
---|
| 98 | <div className={styles.formGroup}>
|
---|
| 99 | <label htmlFor="phoneNumber">Phone Number:</label>
|
---|
| 100 | <input
|
---|
| 101 | type="tel"
|
---|
| 102 | id="phoneNumber"
|
---|
| 103 | value={phoneNumber || ''}
|
---|
| 104 | onChange={handlePhoneNumberChange}
|
---|
| 105 | placeholder="Enter your phone number"
|
---|
| 106 | readOnly={!!localStorage.getItem("phoneNumber")}
|
---|
| 107 | />
|
---|
| 108 | </div>
|
---|
| 109 |
|
---|
| 110 | <div className={styles.formGroup}>
|
---|
| 111 | <label htmlFor="email">Email Address:</label>
|
---|
| 112 | <input
|
---|
| 113 | type="email"
|
---|
| 114 | id="email"
|
---|
| 115 | value={email}
|
---|
| 116 | onChange={handleEmailChange}
|
---|
| 117 | placeholder="Enter your email address"
|
---|
| 118 | readOnly
|
---|
| 119 | required
|
---|
| 120 | />
|
---|
| 121 | </div>
|
---|
| 122 |
|
---|
| 123 | <div className={styles.formGroup}>
|
---|
| 124 | <label htmlFor="cv">Upload CV:</label>
|
---|
| 125 | <input
|
---|
| 126 | type="file"
|
---|
| 127 | id="cv"
|
---|
| 128 | accept=".pdf,.doc,.docx"
|
---|
| 129 | onChange={handleCvChange}
|
---|
| 130 | />
|
---|
| 131 | </div>
|
---|
| 132 |
|
---|
| 133 | <div className={styles.formGroup}>
|
---|
| 134 | <label htmlFor="motivationalLetter">Motivational Letter:</label>
|
---|
| 135 | <textarea
|
---|
| 136 | id="motivationalLetter"
|
---|
| 137 | value={motivationalLetter}
|
---|
| 138 | onChange={handleMotivationalLetterChange}
|
---|
| 139 | rows="6"
|
---|
| 140 | placeholder="Write your motivational letter"
|
---|
| 141 | required
|
---|
| 142 | />
|
---|
| 143 | </div>
|
---|
| 144 |
|
---|
| 145 | <div className={styles.buttonContainer}>
|
---|
| 146 | <button type="submit" className={styles.submitButton}>
|
---|
| 147 | Submit Application
|
---|
| 148 | </button>
|
---|
| 149 | </div>
|
---|
| 150 | </form>
|
---|
| 151 |
|
---|
| 152 | {showModal && (
|
---|
| 153 | <>
|
---|
| 154 | <div className={`${modalStyles.modalOverlay} ${modalStyles.show}`}></div>
|
---|
| 155 | <div className={`${modalStyles.alertModal} ${modalStyles.show}`}>
|
---|
| 156 | <h3>Application Submitted!</h3>
|
---|
| 157 | <p>Your application has been submitted successfully. You will be redirected to the homepage shortly.</p>
|
---|
| 158 | <button className={modalStyles.closeButton} onClick={() => setShowModal(false)}>
|
---|
| 159 | Close
|
---|
| 160 | </button>
|
---|
| 161 | </div>
|
---|
| 162 | </>
|
---|
| 163 | )}
|
---|
| 164 | </div>
|
---|
| 165 | );
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | export default ApplicationForm;
|
---|