@@ -39,21 +39,53 @@ const EditProfile = () => {
3939 const [ charactersLeft , setCharactersLeft ] = useState ( bioLimit ) ;
4040 const [ updatedProfileImg , setUpdatedProfileImg ] = useState < File | null > ( null ) ;
4141 const [ previewImg , setPreviewImg ] = useState < string | null > ( null ) ;
42+
43+ // Form state
44+ const [ formData , setFormData ] = useState ( {
45+ username : '' ,
46+ bio : '' ,
47+ youtube : '' ,
48+ facebook : '' ,
49+ twitter : '' ,
50+ github : '' ,
51+ instagram : '' ,
52+ website : '' ,
53+ } ) ;
4254
4355 useEffect ( ( ) => {
4456 if ( isAuthenticated ( ) ) {
4557 fetchProfile ( ) . finally ( ( ) => setLoading ( false ) ) ;
4658 }
47- } , [ isAuthenticated , fetchProfile ] ) ;
59+ // eslint-disable-next-line react-hooks/exhaustive-deps
60+ } , [ ] ) ;
4861
4962 useEffect ( ( ) => {
50- if ( profile ?. personal_info ?. bio ) {
51- setCharactersLeft ( bioLimit - profile . personal_info . bio . length ) ;
63+ if ( profile ) {
64+ setFormData ( {
65+ username : profile . personal_info . username || '' ,
66+ bio : profile . personal_info . bio || '' ,
67+ youtube : profile . social_links . youtube || '' ,
68+ facebook : profile . social_links . facebook || '' ,
69+ twitter : profile . social_links . x || '' ,
70+ github : profile . social_links . github || '' ,
71+ instagram : profile . social_links . instagram || '' ,
72+ website : profile . social_links . website || '' ,
73+ } ) ;
74+ if ( profile . personal_info ?. bio ) {
75+ setCharactersLeft ( bioLimit - profile . personal_info . bio . length ) ;
76+ }
5277 }
5378 } , [ profile ] ) ;
5479
5580 const handleCharacterChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
56- setCharactersLeft ( bioLimit - e . currentTarget . value . length ) ;
81+ const value = e . currentTarget . value ;
82+ setFormData ( prev => ( { ...prev , bio : value } ) ) ;
83+ setCharactersLeft ( bioLimit - value . length ) ;
84+ } ;
85+
86+ const handleInputChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
87+ const { name, value } = e . currentTarget ;
88+ setFormData ( prev => ( { ...prev , [ name ] : value } ) ) ;
5789 } ;
5890
5991 const handleImagePreview = ( e : React . ChangeEvent < HTMLInputElement > ) => {
@@ -93,33 +125,16 @@ const EditProfile = () => {
93125 const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
94126 e . preventDefault ( ) ;
95127
96- if ( ! editProfileForm . current ) return ;
97- const form = new FormData ( editProfileForm . current ) ;
98- const formData : { [ key : string ] : FormDataEntryValue } = { } ;
128+ const { username, bio, youtube, facebook, twitter, github, instagram, website } = formData ;
99129
100- for ( const [ key , value ] of form . entries ( ) ) {
101- formData [ key ] = value ;
102- }
103-
104- const {
105- username,
106- bio,
107- youtube,
108- facebook,
109- twitter,
110- github,
111- instagram,
112- website,
113- } = formData ;
114-
115- if ( typeof username !== 'string' || username . length < 3 ) {
130+ if ( username . length < 3 ) {
116131 return addNotification ( {
117132 message : 'Username should be atleast 3 characters long' ,
118133 type : 'error' ,
119134 } ) ;
120135 }
121136
122- if ( typeof bio === 'string' && bio . length > bioLimit ) {
137+ if ( bio . length > bioLimit ) {
123138 return addNotification ( {
124139 message : `Bio should be less than ${ bioLimit } characters` ,
125140 type : 'error' ,
@@ -131,15 +146,15 @@ const EditProfile = () => {
131146 try {
132147 await updateUserProfile ( {
133148 username,
134- bio : ( bio as string ) || '' ,
149+ bio : bio || '' ,
135150 social_links : {
136- youtube : ( youtube as string ) || '' ,
137- facebook : ( facebook as string ) || '' ,
138- x : ( twitter as string ) || '' ,
139- github : ( github as string ) || '' ,
140- instagram : ( instagram as string ) || '' ,
141- linkedin : '' ,
142- website : ( website as string ) || '' ,
151+ youtube : youtube || '' ,
152+ facebook : facebook || '' ,
153+ x : twitter || '' ,
154+ github : github || '' ,
155+ instagram : instagram || '' ,
156+ linkedin : profile ?. social_links . linkedin || '' ,
157+ website : website || '' ,
143158 } ,
144159 } ) ;
145160 addNotification ( {
@@ -278,9 +293,14 @@ const EditProfile = () => {
278293 id = "edit-profile-username"
279294 type = "text"
280295 name = "username"
281- defaultValue = { username }
296+ value = { formData . username }
282297 placeholder = "Username"
283298 icon = { < AlternateEmailIcon /> }
299+ slotProps = { {
300+ htmlInput : {
301+ onChange : handleInputChange ,
302+ } ,
303+ } }
284304 />
285305 < Typography
286306 variant = "caption"
@@ -297,7 +317,7 @@ const EditProfile = () => {
297317 name = "bio"
298318 multiline
299319 rows = { 4 }
300- defaultValue = { bio }
320+ value = { formData . bio }
301321 placeholder = "Bio"
302322 fullWidth
303323 inputProps = { { maxLength : bioLimit } }
@@ -328,24 +348,25 @@ const EditProfile = () => {
328348 gap : 2 ,
329349 } }
330350 >
331- { (
332- Object . keys ( social_links ) as Array <
333- keyof typeof social_links
334- >
335- ) . map ( key => {
336- const link = social_links [ key ] || '' ;
351+ { [ 'youtube' , 'facebook' , 'twitter' , 'github' , 'instagram' , 'website' ] . map ( key => {
352+ const fieldName = key as keyof typeof formData ;
337353 return (
338354 < InputBox
339355 key = { key }
340356 id = { `edit-profile-${ key } ` }
341357 name = { key }
342358 type = "text"
343- defaultValue = { link }
359+ value = { formData [ fieldName ] }
344360 placeholder = "https://"
345361 icon = { socialIcons [ key ] || < LanguageIcon /> }
346362 sx = { {
347363 flex : { xs : '1 1 100%' , sm : '1 1 calc(50% - 8px)' } ,
348364 } }
365+ slotProps = { {
366+ htmlInput : {
367+ onChange : handleInputChange ,
368+ } ,
369+ } }
349370 />
350371 ) ;
351372 } ) }
0 commit comments