|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { useForm, SubmitHandler } from 'react-hook-form'; |
| 3 | +import { useGetUserByIdQuery, useUpdateUserMutation } from '../../services/userApi'; |
| 4 | +import Input from '../common/Input'; |
| 5 | +import { useSelector } from 'react-redux'; |
| 6 | + |
| 7 | +export interface UserFormValues { |
| 8 | + firstName: string; |
| 9 | + lastName: string; |
| 10 | + phoneNumber: string; |
| 11 | + email: string; |
| 12 | + photoUrl?: any; |
| 13 | +} |
| 14 | + |
| 15 | +const Profile: React.FC = () => { |
| 16 | + const [isEditing, setIsEditing] = useState(false); |
| 17 | + const [showSuccessMessage, setShowSuccessMessage] = useState(false); |
| 18 | + const id = useSelector((state: any) => state.user.userId); |
| 19 | + |
| 20 | + const { data } = useGetUserByIdQuery(id); |
| 21 | + const [userData, setUserData] = useState(data?.message); |
| 22 | + |
| 23 | + const { |
| 24 | + register, |
| 25 | + handleSubmit, |
| 26 | + setValue, |
| 27 | + formState: { errors, isSubmitting }, |
| 28 | + } = useForm<UserFormValues>(); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + if (data?.message) { |
| 32 | + setUserData(data.message); |
| 33 | + } |
| 34 | + }, [data]); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (userData) { |
| 38 | + setValue('firstName', userData.firstName); |
| 39 | + setValue('lastName', userData.lastName); |
| 40 | + setValue('email', userData.email); |
| 41 | + setValue('phoneNumber', userData.phoneNumber); |
| 42 | + } |
| 43 | + }, [userData, setValue]); |
| 44 | + |
| 45 | + const [updateUser, { isLoading, isError, error }] = useUpdateUserMutation(); |
| 46 | + |
| 47 | + const onSubmit: SubmitHandler<UserFormValues> = async data => { |
| 48 | + const formData = new FormData(); |
| 49 | + formData.append('firstName', data.firstName); |
| 50 | + formData.append('lastName', data.lastName); |
| 51 | + formData.append('phoneNumber', data.phoneNumber); |
| 52 | + formData.append('email', data.email); |
| 53 | + if (data.photoUrl && data.photoUrl.length > 0) { |
| 54 | + formData.append('profileImage', data.photoUrl[0]); |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + const response = await updateUser(formData).unwrap(); |
| 59 | + setUserData(response.message); |
| 60 | + setIsEditing(false); |
| 61 | + setShowSuccessMessage(true); |
| 62 | + setTimeout(() => setShowSuccessMessage(false), 3000); |
| 63 | + } catch (err) { |
| 64 | + console.error('Failed to update profile:', err); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + const getErrorMessage = (error: any) => { |
| 69 | + if ('data' in error) { |
| 70 | + return error.data?.message || 'Error updating profile'; |
| 71 | + } else { |
| 72 | + return 'An unexpected error occurred'; |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className='container mx-auto px-4'> |
| 78 | + <div className='flex flex-col items-center'> |
| 79 | + {showSuccessMessage && ( |
| 80 | + <div className='w-full md:w-8/12 lg:w-6/12 xl:w-5/12 bg-[#38a169] text-[#ffffff] p-4 rounded-lg mb-6 text-center'> |
| 81 | + Profile updated successfully! |
| 82 | + </div> |
| 83 | + )} |
| 84 | + <h1 className='text-3xl font-bold my-6 text-[#000000]'>My Profile</h1> |
| 85 | + <div className='w-full md:w-8/12 lg:w-6/12 xl:w-5/12 border border-[#d1d5db] p-6 rounded-lg shadow-md mb-6'> |
| 86 | + <div className='flex items-center justify-between'> |
| 87 | + <div className='flex items-center'> |
| 88 | + <img |
| 89 | + className='h-24 w-24 rounded-full' |
| 90 | + src={userData?.photoUrl || '/default-profile.png'} |
| 91 | + alt={userData?.firstName} |
| 92 | + /> |
| 93 | + <div className='ml-4'> |
| 94 | + <div className='text-2xl font-medium text-[#000000]'> |
| 95 | + {userData?.firstName} {userData?.lastName} |
| 96 | + </div> |
| 97 | + <p className='text-[#6b7280] font-light mt-1'>{userData?.role}</p> |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + <button |
| 101 | + type='button' |
| 102 | + className='p-3 rounded-lg bg-[#d1d5db] hover:bg-[#9ca3af] transition-all text-[#000000] font-bold' |
| 103 | + onClick={() => setIsEditing(true)} |
| 104 | + > |
| 105 | + Edit |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + {isEditing && ( |
| 110 | + <div className='w-full md:w-8/12 lg:w-6/12 xl:w-5/12 border border-[#d1d5db] rounded-xl p-8 shadow-md'> |
| 111 | + <h2 className='text-xl font-semibold mb-4 text-[#000000]'>Personal Information</h2> |
| 112 | + {isError && error && ( |
| 113 | + <p className='text-lg bg-[#f56565] text-[#ffffff] mt-4 py-2 rounded-lg px-3'>{getErrorMessage(error)}</p> |
| 114 | + )} |
| 115 | + <form onSubmit={handleSubmit(onSubmit)} className='space-y-6'> |
| 116 | + <div> |
| 117 | + <label className='block text-sm font-medium text-[#000000]'>Profile Picture</label> |
| 118 | + <input type='file' accept='image/*' className='mt-2' {...register('photoUrl')} /> |
| 119 | + </div> |
| 120 | + <div className='grid grid-cols-1 sm:grid-cols-2 gap-4'> |
| 121 | + <div className='text-[#000000]'> |
| 122 | + <Input |
| 123 | + id='firstName' |
| 124 | + label='First Name' |
| 125 | + type='text' |
| 126 | + placeholder='Enter first name' |
| 127 | + {...register('firstName')} |
| 128 | + error={errors?.firstName?.message} |
| 129 | + /> |
| 130 | + </div> |
| 131 | + <div className='text-[#000000]'> |
| 132 | + <Input |
| 133 | + id='lastName' |
| 134 | + label='Last Name' |
| 135 | + type='text' |
| 136 | + placeholder='Enter last name' |
| 137 | + {...register('lastName')} |
| 138 | + error={errors?.lastName?.message} |
| 139 | + /> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + <div className='text-[#000000]'> |
| 143 | + <Input |
| 144 | + id='phoneNumber' |
| 145 | + label='Contact Number' |
| 146 | + type='tel' |
| 147 | + placeholder='Enter your contact number' |
| 148 | + {...register('phoneNumber')} |
| 149 | + error={errors?.phoneNumber?.message} |
| 150 | + /> |
| 151 | + </div> |
| 152 | + <div className='text-[#000000]'> |
| 153 | + <Input |
| 154 | + id='email' |
| 155 | + label='Email' |
| 156 | + type='email' |
| 157 | + disabled |
| 158 | + placeholder='Enter your email' |
| 159 | + {...register('email')} |
| 160 | + error={errors?.email?.message} |
| 161 | + /> |
| 162 | + </div> |
| 163 | + <div className='flex justify-between gap-4'> |
| 164 | + <button |
| 165 | + type='submit' |
| 166 | + className='p-3 rounded-lg bg-[#38a169] hover:bg-[#2f855a] transition-all text-[#ffffff] font-bold' |
| 167 | + disabled={isSubmitting || isLoading} |
| 168 | + > |
| 169 | + {isSubmitting || isLoading ? 'Loading...' : 'Save'} |
| 170 | + </button> |
| 171 | + <button |
| 172 | + type='button' |
| 173 | + onClick={() => setIsEditing(false)} |
| 174 | + className='p-3 rounded-lg bg-[#d1d5db] hover:bg-[#9ca3af] transition-all text-[#000000]' |
| 175 | + > |
| 176 | + Cancel |
| 177 | + </button> |
| 178 | + </div> |
| 179 | + </form> |
| 180 | + </div> |
| 181 | + )} |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + ); |
| 185 | +}; |
| 186 | + |
| 187 | +export default Profile; |
0 commit comments