@@ -8,7 +8,9 @@ import { toast } from "react-toastify";
8
8
import { getAuth } from "firebase/auth" ;
9
9
import "react-toastify/dist/ReactToastify.css" ;
10
10
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';
12
14
13
15
const ProfilePage = ( ) => {
14
16
const [ userData , setUserData ] = useState ( null ) ;
@@ -46,8 +48,27 @@ const ProfilePage = () => {
46
48
return unsubscribe ;
47
49
} , [ ] ) ;
48
50
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
51
72
const storageRef = ref (
52
73
storage ,
53
74
`profile_pictures/${ user . uid } /${ profilePic . name } `
@@ -56,35 +77,59 @@ const ProfilePage = () => {
56
77
const uploadTask = uploadBytesResumable ( storageRef , profilePic ) ;
57
78
58
79
uploadTask . on (
59
- " state_changed" ,
80
+ ' state_changed' ,
60
81
( snapshot ) => {
61
82
const progress =
62
83
( snapshot . bytesTransferred / snapshot . totalBytes ) * 100 ;
63
- console . log ( " Upload is " + progress + " % done" ) ;
84
+ console . log ( ' Upload is ' + progress + ' % done' ) ;
64
85
switch ( snapshot . state ) {
65
- case " paused" :
66
- console . log ( " Upload is paused" ) ;
86
+ case ' paused' :
87
+ console . log ( ' Upload is paused' ) ;
67
88
break ;
68
- case " running" :
69
- console . log ( " Upload is running" ) ;
89
+ case ' running' :
90
+ console . log ( ' Upload is running' ) ;
70
91
break ;
71
92
default :
72
93
break ;
73
94
}
74
95
} ,
75
96
( error ) => {
76
- console . error ( "error" , error ) ;
97
+ console . error ( 'Error uploading profile picture:' , error ) ;
77
98
} ,
78
99
( ) => {
79
100
getDownloadURL ( uploadTask . snapshot . ref ) . then ( ( downloadURL ) => {
80
101
setProfilePic ( downloadURL ) ;
81
102
} ) ;
82
103
}
83
104
) ;
84
- } ;
85
- profilePic && uploadFile ( ) ;
86
- } , [ profilePic ] ) ;
105
+ }
106
+ } ;
87
107
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
88
133
const handleLogout = ( ) => {
89
134
auth
90
135
. signOut ( )
@@ -99,34 +144,7 @@ const ProfilePage = () => {
99
144
} ) ;
100
145
} ;
101
146
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
+
130
148
131
149
return (
132
150
< div className = "flex flex-row ml-10 mt-10" >
@@ -145,30 +163,17 @@ const ProfilePage = () => {
145
163
Welcome, { user . userName }
146
164
</ h1 >
147
165
{ /* 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 }
155
175
/>
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 >
172
177
</ div >
173
178
) : (
174
179
< p > </ p >
@@ -182,7 +187,6 @@ const ProfilePage = () => {
182
187
Welcome, {userData.userName}
183
188
</h1> */ }
184
189
{ /* set profile picture */ }
185
-
186
190
</ div >
187
191
< hr className = "mx-auto border-dashed rounded-md w-[1000%] lg:w-[1000px] mt-12 mb-5" />
188
192
< div >
0 commit comments