@@ -11,11 +11,20 @@ import {
11
11
TextField ,
12
12
Typography ,
13
13
} from "@mui/material" ;
14
- import DeleteIcon from ' @mui/icons-material/Delete' ;
14
+ import DeleteIcon from " @mui/icons-material/Delete" ;
15
15
import { useForm } from "react-hook-form" ;
16
16
import { useProfile } from "../../contexts/ProfileContext" ;
17
- import { bioValidator , nameValidator , profilePictureValidator } from "../../utils/validators" ;
18
- import { FAILED_PROFILE_UPDATE_MESSAGE , PROFILE_PIC_MAX_SIZE_ERROR_MESSAGE , USE_AUTH_ERROR_MESSAGE , USE_PROFILE_ERROR_MESSAGE } from "../../utils/constants" ;
17
+ import {
18
+ bioValidator ,
19
+ nameValidator ,
20
+ profilePictureValidator ,
21
+ } from "../../utils/validators" ;
22
+ import {
23
+ FAILED_PROFILE_UPDATE_MESSAGE ,
24
+ PROFILE_PIC_MAX_SIZE_ERROR_MESSAGE ,
25
+ USE_AUTH_ERROR_MESSAGE ,
26
+ USE_PROFILE_ERROR_MESSAGE ,
27
+ } from "../../utils/constants" ;
19
28
import { useRef , useState } from "react" ;
20
29
import { Restore } from "@mui/icons-material" ;
21
30
import { toast } from "react-toastify" ;
@@ -35,7 +44,14 @@ const StyledForm = styled("form")(({ theme }) => ({
35
44
} ) ) ;
36
45
37
46
const EditProfileModal : React . FC < EditProfileModalProps > = ( props ) => {
38
- const { open, onClose, currProfilePictureUrl, currFirstName, currLastName, currBiography } = props ;
47
+ const {
48
+ open,
49
+ onClose,
50
+ currProfilePictureUrl,
51
+ currFirstName,
52
+ currLastName,
53
+ currBiography,
54
+ } = props ;
39
55
40
56
const {
41
57
register,
@@ -77,12 +93,16 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
77
93
const { setUser } = auth ;
78
94
79
95
// profile pic functionality referenced and adapted from https://dreamix.eu/insights/uploading-files-with-react-hook-form/
80
- const [ picPreview , setPicPreview ] = useState < string | null > ( currProfilePictureUrl || null ) ;
96
+ const [ picPreview , setPicPreview ] = useState < string | null > (
97
+ currProfilePictureUrl || null
98
+ ) ;
81
99
const hiddenFileInputRef = useRef < HTMLInputElement | null > ( null ) ;
82
- const { ref : registerRef , ...rest } = register ( "profilePic" , { validate : profilePictureValidator } ) ;
100
+ const { ref : registerRef , ...rest } = register ( "profilePic" , {
101
+ validate : profilePictureValidator ,
102
+ } ) ;
83
103
const onClickUpload = ( ) => {
84
104
hiddenFileInputRef . current ?. click ( ) ;
85
- }
105
+ } ;
86
106
const handleImageChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
87
107
const file = event . target . files ?. [ 0 ] ;
88
108
if ( file ) {
@@ -93,33 +113,35 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
93
113
setValue ( "profilePictureUrl" , "" , { shouldDirty : true } ) ;
94
114
}
95
115
}
96
- }
116
+ } ;
97
117
98
118
const onClickReset = ( ) => {
99
119
if ( getFieldState ( "profilePic" ) . isDirty ) {
100
120
setValue ( "profilePic" , null , { shouldValidate : true , shouldDirty : true } ) ;
101
121
if ( hiddenFileInputRef . current ) {
102
- hiddenFileInputRef . current . value = '' ;
122
+ hiddenFileInputRef . current . value = "" ;
103
123
}
104
124
}
105
125
if ( getFieldState ( "profilePictureUrl" ) . isDirty ) {
106
- setValue ( "profilePictureUrl" , currProfilePictureUrl || "" , { shouldDirty : true } )
126
+ setValue ( "profilePictureUrl" , currProfilePictureUrl || "" , {
127
+ shouldDirty : true ,
128
+ } ) ;
107
129
}
108
130
setPicPreview ( currProfilePictureUrl || "" ) ;
109
- }
131
+ } ;
110
132
111
- const onClickDelete = ( ) => {
133
+ const onClickDelete = ( ) => {
112
134
if ( getFieldState ( "profilePic" ) . isDirty ) {
113
135
setValue ( "profilePic" , null , { shouldValidate : true , shouldDirty : true } ) ;
114
136
if ( hiddenFileInputRef . current ) {
115
- hiddenFileInputRef . current . value = '' ;
137
+ hiddenFileInputRef . current . value = "" ;
116
138
}
117
139
}
118
140
if ( currProfilePictureUrl ) {
119
141
setValue ( "profilePictureUrl" , "" , { shouldDirty : true } ) ;
120
142
}
121
143
setPicPreview ( null ) ;
122
- }
144
+ } ;
123
145
124
146
return (
125
147
< Dialog open = { open } onClose = { onClose } >
@@ -151,7 +173,7 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
151
173
profilePictureUrl : url_data . profilePictureUrl ,
152
174
createdAt : user . createdAt ,
153
175
isAdmin : user . isAdmin ,
154
- }
176
+ } ;
155
177
setUser ( updatedUser ) ;
156
178
}
157
179
} ) ;
@@ -179,24 +201,26 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
179
201
profilePictureUrl : url_data . profilePictureUrl ,
180
202
createdAt : user . createdAt ,
181
203
isAdmin : user . isAdmin ,
182
- }
204
+ } ;
183
205
setUser ( updatedUser ) ;
184
206
}
185
207
} ) ;
186
208
onClose ( ) ;
187
209
}
188
210
} ) }
189
211
>
190
- < Stack
191
- direction = "row"
212
+ < Stack
213
+ direction = "row"
192
214
spacing = { 2 }
193
215
display = "flex"
194
216
alignItems = "center"
195
- sx = { ( theme ) => ( { marginBottom : theme . spacing ( 2 ) } ) } >
196
- { ! picPreview
197
- ? < Avatar sx = { { width : 56 , height : 56 } } />
198
- : < Avatar src = { picPreview } sx = { { width : 56 , height : 56 } } />
199
- }
217
+ sx = { ( theme ) => ( { marginBottom : theme . spacing ( 2 ) } ) }
218
+ >
219
+ { ! picPreview ? (
220
+ < Avatar sx = { { width : 56 , height : 56 } } />
221
+ ) : (
222
+ < Avatar src = { picPreview } sx = { { width : 56 , height : 56 } } />
223
+ ) }
200
224
{ /* input referenced from https://dreamix.eu/insights/uploading-files-with-react-hook-form/ */ }
201
225
< input
202
226
type = "file"
@@ -209,35 +233,32 @@ const EditProfileModal: React.FC<EditProfileModalProps> = (props) => {
209
233
} }
210
234
onChange = { handleImageChange }
211
235
/>
212
- < Button
236
+ < Button
213
237
size = "small"
214
238
variant = "outlined"
215
239
onClick = { onClickUpload }
216
- sx = { { height : 30 } } >
217
- Upload
240
+ sx = { { height : 30 } }
241
+ >
242
+ Upload
218
243
</ Button >
219
- < IconButton
220
- onClick = { onClickReset } >
221
- < Restore color = "success" />
244
+ < IconButton onClick = { onClickReset } >
245
+ < Restore color = "success" />
222
246
</ IconButton >
223
- < IconButton
224
- onClick = { onClickDelete } >
225
- < DeleteIcon color = "error" />
247
+ < IconButton onClick = { onClickDelete } >
248
+ < DeleteIcon color = "error" />
226
249
</ IconButton >
227
250
</ Stack >
228
- { ! ! errors . profilePic
229
- ? < Typography
230
- color = "error"
231
- sx = { { fontSize : 13 ,
232
- marginBottom : 2 } } >
233
- { errors . profilePic . message }
234
- </ Typography >
235
- : < Typography
236
- sx = { { fontSize : 13 ,
237
- marginBottom : 2 ,
238
- color : "#808080" } } >
239
- { PROFILE_PIC_MAX_SIZE_ERROR_MESSAGE }
240
- </ Typography > }
251
+ { errors . profilePic ? (
252
+ < Typography color = "error" sx = { { fontSize : 13 , marginBottom : 2 } } >
253
+ { errors . profilePic . message }
254
+ </ Typography >
255
+ ) : (
256
+ < Typography
257
+ sx = { { fontSize : 13 , marginBottom : 2 , color : "#808080" } }
258
+ >
259
+ { PROFILE_PIC_MAX_SIZE_ERROR_MESSAGE }
260
+ </ Typography >
261
+ ) }
241
262
< TextField
242
263
fullWidth
243
264
required
0 commit comments