[d7b7f00] | 1 | import React, {useState, useEffect} from 'react';
|
---|
| 2 | import styles from '../../css/ProfileCss/profileSection.module.css';
|
---|
| 3 |
|
---|
| 4 | const ProfileSection = () => {
|
---|
| 5 | const [profileData, setProfileData] = useState({
|
---|
| 6 | userName: '',
|
---|
| 7 | userSurname: '',
|
---|
| 8 | email: '',
|
---|
| 9 | phoneNumber: '',
|
---|
| 10 | address: '',
|
---|
| 11 | addressNumber: '',
|
---|
| 12 | addressFloor: ''
|
---|
| 13 | });
|
---|
| 14 |
|
---|
| 15 | useEffect(() => {
|
---|
| 16 | const storedUserName = localStorage.getItem("userName");
|
---|
| 17 | const storedUserSurname = localStorage.getItem("userSurname");
|
---|
| 18 | const storedEmail = localStorage.getItem("email");
|
---|
| 19 | const storedPhoneNumber = localStorage.getItem("phoneNumber");
|
---|
| 20 | const storedAddress = localStorage.getItem("address");
|
---|
| 21 |
|
---|
| 22 | let parts = storedAddress.split(";");
|
---|
| 23 |
|
---|
| 24 | setProfileData({
|
---|
| 25 | userName: storedUserName || '',
|
---|
| 26 | userSurname: storedUserSurname || '',
|
---|
| 27 | email: storedEmail || '',
|
---|
| 28 | phoneNumber: storedPhoneNumber || '',
|
---|
| 29 | address: parts[0] || '',
|
---|
| 30 | addressNumber: parts[1] || '',
|
---|
| 31 | addressFloor: parts[2] || ''
|
---|
| 32 | });
|
---|
| 33 | }, []);
|
---|
| 34 |
|
---|
| 35 | const handleSaveChanges = async (e) => {
|
---|
| 36 | e.preventDefault()
|
---|
| 37 | try {
|
---|
| 38 | const token = localStorage.getItem("token");
|
---|
| 39 |
|
---|
| 40 | const updatedProfileData = {
|
---|
| 41 | ...profileData,
|
---|
| 42 | address: `${profileData.address};${profileData.addressNumber};${profileData.addressFloor}`
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | const response = await fetch("http://localhost:8080/api/profile/update", {
|
---|
| 46 | method: 'POST',
|
---|
| 47 | headers: {
|
---|
| 48 | 'Content-Type': 'application/json',
|
---|
| 49 | 'Authorization': `Bearer ${token}`
|
---|
| 50 | },
|
---|
| 51 | body: JSON.stringify(updatedProfileData)
|
---|
| 52 | })
|
---|
| 53 |
|
---|
| 54 | if (response.ok) {
|
---|
| 55 | updatedProfileData.phoneNumber === '' ? localStorage.setItem("phoneNumber", "") : localStorage.setItem("phoneNumber", updatedProfileData.phoneNumber)
|
---|
| 56 | updatedProfileData.address === ";;" ? localStorage.setItem("address", "") : localStorage.setItem("address", updatedProfileData.address);
|
---|
| 57 | alert("Profile updated successfully.")
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | } catch (error) {
|
---|
| 61 | alert("An error occurred while saving your changes.")
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | const handleInputChange = (event) => {
|
---|
| 66 | const {name, value} = event.target;
|
---|
| 67 | setProfileData({
|
---|
| 68 | ...profileData,
|
---|
| 69 | [name]: value
|
---|
| 70 | })
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | return (
|
---|
| 74 | <div className={styles.profileSection}>
|
---|
| 75 | <form className={styles.profileForm}>
|
---|
| 76 | <div className={styles.formGroup}>
|
---|
| 77 | <label htmlFor="profile-name">Profile name</label>
|
---|
| 78 | <input
|
---|
| 79 | type="text"
|
---|
| 80 | id="profile-name"
|
---|
| 81 | name="userName"
|
---|
| 82 | value={profileData.userName}
|
---|
| 83 | onChange={(e) => handleInputChange(e)}
|
---|
| 84 | readOnly
|
---|
| 85 | />
|
---|
| 86 | </div>
|
---|
| 87 | <div className={styles.formGroup}>
|
---|
| 88 | <label htmlFor="profile-surname">Profile surname</label>
|
---|
| 89 | <input
|
---|
| 90 | type="text"
|
---|
| 91 | id="profile-surname"
|
---|
| 92 | name="userSurname"
|
---|
| 93 | value={profileData.userSurname}
|
---|
| 94 | onChange={(e) => handleInputChange(e)}
|
---|
| 95 | readOnly
|
---|
| 96 | />
|
---|
| 97 | </div>
|
---|
| 98 | <div className={styles.formGroup}>
|
---|
| 99 | <label htmlFor="email">Email</label>
|
---|
| 100 | <input
|
---|
| 101 | type="email"
|
---|
| 102 | id="email"
|
---|
| 103 | name="email"
|
---|
| 104 | value={profileData.email}
|
---|
| 105 | onChange={(e) => handleInputChange(e)}
|
---|
| 106 | readOnly
|
---|
| 107 | />
|
---|
| 108 | </div>
|
---|
| 109 | <div className={styles.formGroup}>
|
---|
| 110 | <label htmlFor="phoneNumber">Phone Number</label>
|
---|
| 111 | <input
|
---|
| 112 | type="tel"
|
---|
| 113 | id="phoneNumber"
|
---|
| 114 | name="phoneNumber"
|
---|
| 115 | value={profileData.phoneNumber}
|
---|
| 116 | onChange={(e) => handleInputChange(e)}
|
---|
| 117 | />
|
---|
| 118 | </div>
|
---|
| 119 | <div className={styles.formGroup}>
|
---|
| 120 | <label htmlFor="address">Address</label>
|
---|
| 121 | <input
|
---|
| 122 | type="text"
|
---|
| 123 | id="address"
|
---|
| 124 | name="address"
|
---|
| 125 | value={profileData.address}
|
---|
| 126 | onChange={(e) => handleInputChange(e)}
|
---|
| 127 | />
|
---|
| 128 | <div className={styles.addressDetails}>
|
---|
| 129 | <div className={styles.addressNumber}>
|
---|
| 130 | <label htmlFor="address-number">Number</label>
|
---|
| 131 | <input
|
---|
| 132 | type="text"
|
---|
| 133 | id="address-number"
|
---|
| 134 | name="addressNumber"
|
---|
| 135 | value={profileData.addressNumber}
|
---|
| 136 | onChange={(e) => handleInputChange(e)}
|
---|
| 137 | />
|
---|
| 138 | </div>
|
---|
| 139 | <div className={styles.addressFloor}>
|
---|
| 140 | <label htmlFor="address-floor">Floor</label>
|
---|
| 141 | <input
|
---|
| 142 | type="text"
|
---|
| 143 | id="address-floor"
|
---|
| 144 | name="addressFloor"
|
---|
| 145 | value={profileData.addressFloor}
|
---|
| 146 | onChange={(e) => handleInputChange(e)}
|
---|
| 147 | />
|
---|
| 148 | </div>
|
---|
| 149 | </div>
|
---|
| 150 | </div>
|
---|
| 151 | <div className={styles.buttonContainer}>
|
---|
| 152 | <button className={styles.saveBtn} onClick={handleSaveChanges}>Save changes</button>
|
---|
| 153 | </div>
|
---|
| 154 | </form>
|
---|
| 155 | </div>
|
---|
| 156 | );
|
---|
| 157 | };
|
---|
| 158 |
|
---|
| 159 | export default ProfileSection;
|
---|