@@ -15,10 +15,11 @@ import DeleteIcon from '@mui/icons-material/Delete';
15
15
import { useForm } from "react-hook-form" ;
16
16
import { useProfile } from "../../contexts/ProfileContext" ;
17
17
import { bioValidator , nameValidator , profilePictureValidator } from "../../utils/validators" ;
18
- import { FAILED_PROFILE_UPDATE_MESSAGE , PROFILE_PIC_MAX_SIZE_ERROR_MESSAGE , USE_PROFILE_ERROR_MESSAGE } from "../../utils/constants" ;
18
+ import { FAILED_PROFILE_UPDATE_MESSAGE , PROFILE_PIC_MAX_SIZE_ERROR_MESSAGE , USE_AUTH_ERROR_MESSAGE , USE_PROFILE_ERROR_MESSAGE } from "../../utils/constants" ;
19
19
import { useRef , useState } from "react" ;
20
20
import { Restore } from "@mui/icons-material" ;
21
21
import { toast } from "react-toastify" ;
22
+ import { useAuth } from "../../contexts/AuthContext" ;
22
23
23
24
interface EditProfileModalProps {
24
25
onClose : ( ) => void ;
@@ -44,17 +45,17 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
44
45
getFieldState,
45
46
} = useForm < {
46
47
profilePic : File | null ;
47
- profilePictureUrl : string | null ;
48
+ profilePictureUrl : string ;
48
49
firstName : string ;
49
50
lastName : string ;
50
51
biography : string ;
51
52
} > ( {
52
53
defaultValues : {
53
54
profilePic : null ,
54
- profilePictureUrl : currProfilePictureUrl || null ,
55
+ profilePictureUrl : currProfilePictureUrl || "" ,
55
56
firstName : currFirstName ,
56
57
lastName : currLastName ,
57
- biography : currBiography ,
58
+ biography : currBiography || "" ,
58
59
} ,
59
60
mode : "all" ,
60
61
} ) ;
@@ -65,7 +66,15 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
65
66
throw new Error ( USE_PROFILE_ERROR_MESSAGE ) ;
66
67
}
67
68
68
- const { uploadProfilePicture, updateProfile } = profile ;
69
+ const { user, uploadProfilePicture, updateProfile } = profile ;
70
+
71
+ const auth = useAuth ( ) ;
72
+
73
+ if ( ! auth ) {
74
+ throw new Error ( USE_AUTH_ERROR_MESSAGE ) ;
75
+ }
76
+
77
+ const { setUser } = auth ;
69
78
70
79
// profile pic functionality referenced and adapted from https://dreamix.eu/insights/uploading-files-with-react-hook-form/
71
80
const [ picPreview , setPicPreview ] = useState < string | null > ( currProfilePictureUrl || null ) ;
@@ -81,7 +90,7 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
81
90
setValue ( "profilePic" , file , { shouldValidate : true , shouldDirty : true } ) ;
82
91
83
92
if ( currProfilePictureUrl ) {
84
- setValue ( "profilePictureUrl" , null , { shouldDirty : true } ) ;
93
+ setValue ( "profilePictureUrl" , "" , { shouldDirty : true } ) ;
85
94
}
86
95
}
87
96
}
@@ -94,9 +103,9 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
94
103
}
95
104
}
96
105
if ( getFieldState ( "profilePictureUrl" ) . isDirty ) {
97
- setValue ( "profilePictureUrl" , currProfilePictureUrl || null , { shouldDirty : true } )
106
+ setValue ( "profilePictureUrl" , currProfilePictureUrl || "" , { shouldDirty : true } )
98
107
}
99
- setPicPreview ( currProfilePictureUrl || null ) ;
108
+ setPicPreview ( currProfilePictureUrl || "" ) ;
100
109
}
101
110
102
111
const onClickDelete = ( ) => {
@@ -107,7 +116,7 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
107
116
}
108
117
}
109
118
if ( currProfilePictureUrl ) {
110
- setValue ( "profilePictureUrl" , null , { shouldDirty : true } ) ;
119
+ setValue ( "profilePictureUrl" , "" , { shouldDirty : true } ) ;
111
120
}
112
121
setPicPreview ( null ) ;
113
122
}
@@ -122,7 +131,6 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
122
131
< StyledForm
123
132
onSubmit = { handleSubmit ( ( data ) => {
124
133
if ( data . profilePic ) {
125
- //send to firebase and get the url back
126
134
uploadProfilePicture ( data . profilePic ) . then ( ( res ) => {
127
135
if ( res ) {
128
136
const url_data = {
@@ -131,7 +139,22 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
131
139
biography : data . biography ,
132
140
profilePictureUrl : res . imageUrl ,
133
141
} ;
134
- updateProfile ( url_data ) ;
142
+ updateProfile ( url_data ) . then ( ( res ) => {
143
+ if ( res && user ) {
144
+ const updatedUser = {
145
+ id : user . id ,
146
+ username : user . username ,
147
+ firstName : url_data . firstName ,
148
+ lastName : url_data . lastName ,
149
+ email : user . email ,
150
+ biography : url_data . biography ,
151
+ profilePictureUrl : url_data . profilePictureUrl ,
152
+ createdAt : user . createdAt ,
153
+ isAdmin : user . isAdmin ,
154
+ }
155
+ setUser ( updatedUser ) ;
156
+ }
157
+ } ) ;
135
158
onClose ( ) ;
136
159
} else {
137
160
toast . error ( FAILED_PROFILE_UPDATE_MESSAGE ) ;
@@ -144,7 +167,22 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
144
167
biography : data . biography ,
145
168
profilePictureUrl : data . profilePictureUrl ,
146
169
} ;
147
- updateProfile ( url_data ) ;
170
+ updateProfile ( url_data ) . then ( ( res ) => {
171
+ if ( res && user ) {
172
+ const updatedUser = {
173
+ id : user . id ,
174
+ username : user . username ,
175
+ firstName : url_data . firstName ,
176
+ lastName : url_data . lastName ,
177
+ email : user . email ,
178
+ biography : url_data . biography ,
179
+ profilePictureUrl : url_data . profilePictureUrl ,
180
+ createdAt : user . createdAt ,
181
+ isAdmin : user . isAdmin ,
182
+ }
183
+ setUser ( updatedUser ) ;
184
+ }
185
+ } ) ;
148
186
onClose ( ) ;
149
187
}
150
188
} ) }
0 commit comments