Skip to content

Commit

Permalink
Improve UX and, input validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
prajwalkulkarni committed Mar 30, 2022
1 parent 0e6e86b commit b383aeb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
10 changes: 7 additions & 3 deletions src/components/EditableLabel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const EditableLabel = ({
value,
type,
handleInputChange,
assignCurrField,
updateProfile,
children,
allowEditing,
Expand All @@ -24,7 +25,7 @@ export const EditableLabel = ({
handleInputChange(e)

if (e.key === 'Enter') {
updateProfile()
updateProfile(name)
setEditMode(!editMode)
}
}}
Expand Down Expand Up @@ -58,7 +59,10 @@ export const EditableLabel = ({
<button
data-cy={`edit-${name}-btn`}
className='editable-label-edit-btn'
onClick={() => setEditMode(!editMode)}
onClick={() => {
setEditMode(!editMode)
assignCurrField(value, name)
}}
>
<EOS_MODE_EDIT className='eos-icons' />
</button>
Expand All @@ -72,7 +76,7 @@ export const EditableLabel = ({
data-cy='save-changes-btn'
onClick={() => {
setEditMode(!editMode)
updateProfile()
updateProfile(name)
}}
>
<EOS_SAVE className='eos-icons' />
Expand Down
13 changes: 11 additions & 2 deletions src/components/UserProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const UserProfile = ({
allowEditing,
user,
handleInputChange,
updateProfile
updateProfile,
assignCurrField
}) => {
return (
<>
Expand All @@ -19,6 +20,7 @@ export const UserProfile = ({
handleInputChange={handleInputChange}
updateProfile={updateProfile}
allowEditing={allowEditing}
assignCurrField={assignCurrField}
/>
</div>
{allowEditing && <UserSettings user={user} />}
Expand Down Expand Up @@ -90,7 +92,8 @@ export const UserDetails = ({
user,
handleInputChange,
allowEditing,
updateProfile
updateProfile,
assignCurrField
}) => {
return (
<div className='user-profile-details'>
Expand All @@ -104,6 +107,7 @@ export const UserDetails = ({
value={user.Name ?? user.username}
className='input-default'
handleInputChange={handleInputChange}
assignCurrField={assignCurrField}
allowEditing={allowEditing}
updateProfile={updateProfile}
placeholder='Your name'
Expand All @@ -118,6 +122,7 @@ export const UserDetails = ({
name={'Bio'}
value={user.Bio}
handleInputChange={handleInputChange}
assignCurrField={assignCurrField}
allowEditing={allowEditing}
updateProfile={updateProfile}
placeholder='Say something about yourself'
Expand All @@ -144,6 +149,7 @@ export const UserDetails = ({
name={'Profession'}
value={user.Profession}
handleInputChange={handleInputChange}
assignCurrField={assignCurrField}
allowEditing={allowEditing}
updateProfile={updateProfile}
placeholder='Your job title'
Expand All @@ -163,6 +169,7 @@ export const UserDetails = ({
name={'Company'}
value={user.Company}
handleInputChange={handleInputChange}
assignCurrField={assignCurrField}
allowEditing={allowEditing}
updateProfile={updateProfile}
placeholder='Your company name'
Expand All @@ -182,6 +189,7 @@ export const UserDetails = ({
name={'LinkedIn'}
value={user.LinkedIn}
handleInputChange={handleInputChange}
assignCurrField={assignCurrField}
allowEditing={allowEditing}
updateProfile={updateProfile}
placeholder='Your LinkedIn username'
Expand Down Expand Up @@ -212,6 +220,7 @@ export const UserDetails = ({
name={'Twitter'}
value={user.Twitter}
handleInputChange={handleInputChange}
assignCurrField={assignCurrField}
allowEditing={allowEditing}
updateProfile={updateProfile}
placeholder='Your Twitter handle'
Expand Down
33 changes: 23 additions & 10 deletions src/pages/MyProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,39 @@ const MyProfile = () => {

const [, setUpdated] = useState(false)

const [updatingField, setUpdatingField] = useState('')
const [updatingField, setUpdatingField] = useState({})

const [currFieldDefaultValue, setCurrFieldDefaultValue] = useState({})

const assignCurrField = (val, name) => {
setCurrFieldDefaultValue((p) => ({
...p,
[name]: val
}))
setUpdatingField((p) => ({ ...p, [name]: val }))
}

const handleInputChange = (event) => {
setUpdatingField(event.target.value)
setUpdatingField((p) => ({ ...p, [event.target.name]: event.target.value }))
setUser({
...user,
[event.target.name]: event.target.value
})
}

const updateProfile = async () => {
const updateProfile = async (name) => {
if (currFieldDefaultValue[name] === updatingField[name]) {
toast.success('Nothing changed here')
return
}
try {
if (!updatingField) {
toast.error('Input cannot be empty')
return
}
const response = await userStory.updateUser({ id: userId, ...user })
if (response) {
toast.success('Profile updated successfully')
setUpdated(true)
if (response && updatingField[name].length === 0) {
toast.success('Your data has been removed')
} else if (response && updatingField[name].length > 0) {
toast.success('Your changes have been saved')
}
setUpdated(true)
} catch (err) {
console.error(err.message)
toast.error(err.message)
Expand Down Expand Up @@ -68,6 +80,7 @@ const MyProfile = () => {
user={user === '' ? '' : Object.assign(user, { id: userId })}
handleInputChange={handleInputChange}
updateProfile={updateProfile}
assignCurrField={assignCurrField}
allowEditing
/>
</div>
Expand Down

0 comments on commit b383aeb

Please sign in to comment.