Skip to content

Commit 7c80694

Browse files
profile
1 parent 2adc9b2 commit 7c80694

File tree

3 files changed

+80
-65
lines changed

3 files changed

+80
-65
lines changed

frontend/package-lock.json

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"react": "^18.2.0",
1717
"react-dom": "^18.2.0",
1818
"react-icons": "^4.12.0",
19+
"react-image-filler": "^1.0.1",
1920
"react-router-dom": "^6.21.1",
2021
"react-scripts": "5.0.1",
2122
"react-toastify": "^10.0.4",

frontend/src/pages/Private/Profile.js

+69-65
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { toast } from "react-toastify";
88
import { getAuth } from "firebase/auth";
99
import "react-toastify/dist/ReactToastify.css";
1010
import { CgProfile } from "react-icons/cg";
11-
import { ref, uploadBytesResumable, getDownloadURL } from "firebase/storage";
11+
import { ref, uploadBytesResumable, getDownloadURL,deleteObject } from "firebase/storage";
12+
import ImageFiller from "react-image-filler";
13+
// import firebase from 'firebase/app';
1214

1315
const ProfilePage = () => {
1416
const [userData, setUserData] = useState(null);
@@ -46,8 +48,27 @@ const ProfilePage = () => {
4648
return unsubscribe;
4749
}, []);
4850

49-
useEffect(() => {
50-
const uploadFile = () => {
51+
//#################profile picture
52+
53+
54+
55+
useEffect(() => {
56+
const uploadFile = () => {
57+
if (profilePic) {
58+
// Check if there is an existing profile picture
59+
if (user.profilePicURL) {
60+
// Delete the existing profile picture
61+
const existingPicRef = ref(storage, user.profilePicURL);
62+
deleteObject(existingPicRef)
63+
.then(() => {
64+
console.log('Existing profile picture deleted successfully');
65+
})
66+
.catch((error) => {
67+
console.error('Error deleting existing profile picture:', error);
68+
});
69+
}
70+
71+
// Upload the new profile picture
5172
const storageRef = ref(
5273
storage,
5374
`profile_pictures/${user.uid}/${profilePic.name}`
@@ -56,35 +77,59 @@ const ProfilePage = () => {
5677
const uploadTask = uploadBytesResumable(storageRef, profilePic);
5778

5879
uploadTask.on(
59-
"state_changed",
80+
'state_changed',
6081
(snapshot) => {
6182
const progress =
6283
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
63-
console.log("Upload is " + progress + "% done");
84+
console.log('Upload is ' + progress + '% done');
6485
switch (snapshot.state) {
65-
case "paused":
66-
console.log("Upload is paused");
86+
case 'paused':
87+
console.log('Upload is paused');
6788
break;
68-
case "running":
69-
console.log("Upload is running");
89+
case 'running':
90+
console.log('Upload is running');
7091
break;
7192
default:
7293
break;
7394
}
7495
},
7596
(error) => {
76-
console.error("error", error);
97+
console.error('Error uploading profile picture:', error);
7798
},
7899
() => {
79100
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
80101
setProfilePic(downloadURL);
81102
});
82103
}
83104
);
84-
};
85-
profilePic && uploadFile();
86-
}, [profilePic]);
105+
}
106+
};
87107

108+
uploadFile();
109+
}, [profilePic]);
110+
111+
//input field changes save and store
112+
const onChangeHandler = (e) => {
113+
const { name, value } = e.target;
114+
setUserData({ ...userData, [name]: value });
115+
console.log(userData);
116+
};
117+
//update new details to the database
118+
const userDetailsUpdate = async (uid) => {
119+
const docRef = doc(firestore, "users", uid);
120+
try {
121+
await updateDoc(docRef, userData);
122+
window.location.reload();
123+
toast.success("User details updated successfully");
124+
} catch (error) {
125+
console.error("Error updating document: ", error);
126+
toast.error("Error updating user details");
127+
}
128+
};
129+
130+
131+
132+
//#################log out
88133
const handleLogout = () => {
89134
auth
90135
.signOut()
@@ -99,34 +144,7 @@ const ProfilePage = () => {
99144
});
100145
};
101146

102-
const onChangeHandler = (e) => {
103-
const { name, value } = e.target;
104-
setUserData({ ...userData, [name]: value });
105-
console.log(userData);
106-
};
107-
108-
const userDetailsUpdate = async (uid) => {
109-
const docRef = doc(firestore, "users", uid);
110-
try {
111-
await updateDoc(docRef, userData);
112-
window.location.reload();
113-
toast.success("User details updated successfully");
114-
} catch (error) {
115-
console.error("Error updating document: ", error);
116-
toast.error("Error updating user details");
117-
}
118-
};
119-
120-
const handleProfilePic = (e) => {
121-
document.getElementById("profileImageInput").click();
122-
try {
123-
const profileImage = e.target.files[0];
124-
setProfilePic(profileImage);
125-
console.log("profile pic uploaded successfully!");
126-
} catch (error) {
127-
console.log(error.message);
128-
}
129-
};
147+
130148

131149
return (
132150
<div className="flex flex-row ml-10 mt-10">
@@ -145,30 +163,17 @@ const ProfilePage = () => {
145163
Welcome, {user.userName}
146164
</h1>
147165
{/* Profile Picture */}
148-
149-
{/* {profilePic ? (
150-
<img
151-
src={profilePic}
152-
alt="profile"
153-
className="w-32 h-32 rounded-full cursor-pointer"
154-
onClick={handleProfilePic}
166+
<div
167+
style={{
168+
borderRadius: "50%",
169+
overflow: "hidden",
170+
width: 200,
171+
height: 200,
172+
}}
173+
>
174+
<ImageFiller width={200} height={200}
155175
/>
156-
) : (
157-
<div
158-
className="w-32 h-32 rounded-full bg-gray-200/50 flex items-center justify-center cursor-pointer
159-
hover:bg-slate-200 `{ddfjv}`"
160-
onClick={handleProfilePic}
161-
>
162-
<CgProfile className="text-[#828282] w-32 h-32 text-6xl lg:text-8xl" />
163-
</div>
164-
)}
165-
<input
166-
type="file"
167-
id="profileImageInput"
168-
accept="image/*"
169-
style={{ display: "none" }}
170-
onChange={handleProfilePic}
171-
/> */}
176+
</div>
172177
</div>
173178
) : (
174179
<p></p>
@@ -182,7 +187,6 @@ const ProfilePage = () => {
182187
Welcome, {userData.userName}
183188
</h1> */}
184189
{/* set profile picture */}
185-
186190
</div>
187191
<hr className="mx-auto border-dashed rounded-md w-[1000%] lg:w-[1000px] mt-12 mb-5" />
188192
<div>

0 commit comments

Comments
 (0)