|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import Navbar from '@/components/global/navbar/Navbar'; |
| 3 | +import TextField from '@mui/material/TextField'; |
| 4 | +import MoreHorizIcon from '@mui/icons-material/MoreHoriz'; |
| 5 | +import ProfileSection from './profileSection'; |
| 6 | +import Visibilitypassword from './visibilityPassword'; |
| 7 | +import UserInfo from './userInfo' |
| 8 | +import { isValidEmail, validateEmail, validatePassword } from '@/components/common/validation/validation'; |
| 9 | +import { Divider } from '@mui/material'; |
| 10 | +import Footer from '@/components/global/footer/Footer'; |
| 11 | + |
| 12 | +const Profile = () => { |
| 13 | + const validationOdj = { |
| 14 | + 'current password': { error:false, fb: false, msg:'' }, |
| 15 | + 'email': { error:false, fb: false, msg:''}, |
| 16 | + 'new password' : { error:false, fb: false, msg:'' }, |
| 17 | + 'password' : { error:false, fb: false, msg:'' } |
| 18 | + } |
| 19 | + const [newEmail, setNewEmail] = useState(''); |
| 20 | + const [password, setPassword] = useState(''); |
| 21 | + const [nameError, setNameError] = useState<any>(''); |
| 22 | + const [emailError, setEmailError] = useState(''); |
| 23 | + const [currentPassword, setCurrentPassword] = useState(''); |
| 24 | + const [newPassword, setNewPassword] = useState(''); |
| 25 | + const [passwordError, setPasswordError] = useState(''); |
| 26 | + const [nameSuccess, setNameSuccess] = useState(''); |
| 27 | + const [emailSuccess, setEmailSuccess] = useState(''); |
| 28 | + const [passwordSuccess, setPasswordSuccess] = useState(''); |
| 29 | + const [isValid, setValidates] = useState(validationOdj); |
| 30 | + |
| 31 | + const handleValidation = ({target}:any ) => { |
| 32 | + const { value, name, type } = target; |
| 33 | + let error = value === '' |
| 34 | + let msg ='' |
| 35 | + if(!value) { |
| 36 | + msg =`${name} is required` |
| 37 | + } else if (name == 'email') { |
| 38 | + msg = validateEmail(value); |
| 39 | + error = !isValidEmail(value); |
| 40 | + } else if (type === 'password') { |
| 41 | + msg = validatePassword(value); |
| 42 | + } |
| 43 | + setValidates({...isValid, [name]:{error, msg}}) |
| 44 | + } |
| 45 | + |
| 46 | + const nameValue = ({validates}: any) => { |
| 47 | + if (validates['name'].error) { |
| 48 | + setNameError(validates['name'].msg); |
| 49 | + } else { |
| 50 | + setNameError(''); |
| 51 | + //todo: need create submit method |
| 52 | + setNameSuccess(' Display name purely for a decorative purpose'); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + const emailValue = () => { |
| 57 | + setEmailError(''); |
| 58 | + setEmailSuccess('You will receive a confirmation message to your current email'); |
| 59 | + }; |
| 60 | + |
| 61 | + const passwordValue = () => { |
| 62 | + setPasswordError(''); |
| 63 | + setPasswordSuccess('New Password save successfully'); |
| 64 | + }; |
| 65 | + |
| 66 | + function ValidationMsg({ error, success }: any) { |
| 67 | + return ( |
| 68 | + <div className="flex ml-4"> |
| 69 | + <span className={` ${error ? 'text-red-500' : 'text-green-700'} flex items-end`}> |
| 70 | + { error || success} |
| 71 | + </span> |
| 72 | + </div> |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className=""> |
| 78 | + <Navbar searchbar /> |
| 79 | + <section className="px-3"> |
| 80 | + <div className="container"> |
| 81 | + <div className="flex flex-col pt-2 pb-48"> |
| 82 | + <div> |
| 83 | + <h1 className="text-3xl font-bold"> |
| 84 | + My Profile |
| 85 | + </h1> |
| 86 | + </div> |
| 87 | + <ProfileSection title="Account" buttonText="SAVE CHANGES" onClick={nameValue}> |
| 88 | + <div className="flex-col w-2/4"> |
| 89 | + <div className="xl:w-11/12 md:w-5/6 lg:w-4/5 w-11/12"> |
| 90 | + <UserInfo handleInfo={nameValue} /> |
| 91 | + </div> |
| 92 | + </div> |
| 93 | + <Divider orientation="vertical" flexItem /> |
| 94 | + <ValidationMsg error={nameError} success={nameSuccess} /> |
| 95 | + </ProfileSection> |
| 96 | + <ProfileSection title="Change Email" buttonText="SAVE CHANGES" onClick={emailValue}> |
| 97 | + <div className="flex-col w-2/4 pr-6"> |
| 98 | + <TextField |
| 99 | + label="New Email Address" |
| 100 | + id="emailAddress" |
| 101 | + size="small" |
| 102 | + name='email' |
| 103 | + variant="standard" |
| 104 | + className="w-full mb-6" |
| 105 | + value={newEmail} |
| 106 | + error={isValid['email']?.error} |
| 107 | + helperText={isValid['email']?.msg} |
| 108 | + onChange={(e) => setNewEmail(e.target.value)} |
| 109 | + onBlur={handleValidation} |
| 110 | + /> |
| 111 | + <Visibilitypassword |
| 112 | + value = {password} |
| 113 | + className="mr-6" |
| 114 | + name = "password" |
| 115 | + label='Current Password' |
| 116 | + error={isValid['password']?.error} |
| 117 | + helperText={isValid['password']?.msg} |
| 118 | + onChange={(e: any) => setPassword(e.target.value)} |
| 119 | + onBlur={handleValidation} |
| 120 | + /> |
| 121 | + </div> |
| 122 | + <Divider orientation="vertical" flexItem /> |
| 123 | + <ValidationMsg success={emailSuccess} /> |
| 124 | + </ProfileSection> |
| 125 | + <ProfileSection title="Change Password" buttonText="SAVE CHANGES" onClick={passwordValue}> |
| 126 | + <div className="flex-col w-2/4 pr-6"> |
| 127 | + <TextField |
| 128 | + label="Current Password" |
| 129 | + id="standard2" |
| 130 | + name="current password" |
| 131 | + size="small" |
| 132 | + variant="standard" |
| 133 | + className="w-full mr-6 mb-6" |
| 134 | + type="password" |
| 135 | + error={isValid['current password']?.error} |
| 136 | + helperText={isValid['current password']?.msg} |
| 137 | + value={currentPassword} |
| 138 | + onChange={(e) => setCurrentPassword(e.target.value)} |
| 139 | + onBlur={handleValidation} |
| 140 | + /> |
| 141 | + <TextField |
| 142 | + label="New Password" |
| 143 | + id="standard3" |
| 144 | + name="new password" |
| 145 | + size="small" |
| 146 | + variant="standard" |
| 147 | + type="password" |
| 148 | + className="w-full mb-6" |
| 149 | + error={isValid['new password']?.error} |
| 150 | + helperText={isValid['new password']?.msg} |
| 151 | + value={newPassword} |
| 152 | + onChange={(e) => setNewPassword(e.target.value)} |
| 153 | + onBlur={handleValidation} |
| 154 | + /> |
| 155 | + </div> |
| 156 | + <Divider orientation="vertical" flexItem /> |
| 157 | + <ValidationMsg error={isValid['current password']?.msg || isValid['new password']?.msg} success={passwordSuccess} /> |
| 158 | + </ProfileSection> |
| 159 | + <div className='mt-6'> |
| 160 | + <button className="rounded-md border py-3 px-4 tracking-wider text-md"> |
| 161 | + MORE <MoreHorizIcon className="ml-2 mb-1 text-sm" /> |
| 162 | + </button> |
| 163 | + </div> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + </section> |
| 167 | + <Footer /> |
| 168 | + </div> |
| 169 | + ); |
| 170 | +}; |
| 171 | + |
| 172 | +export default Profile; |
| 173 | + |
| 174 | + |
0 commit comments