Skip to content

Commit b383aeb

Browse files
Improve UX and, input validation.
1 parent 0e6e86b commit b383aeb

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

src/components/EditableLabel.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const EditableLabel = ({
66
value,
77
type,
88
handleInputChange,
9+
assignCurrField,
910
updateProfile,
1011
children,
1112
allowEditing,
@@ -24,7 +25,7 @@ export const EditableLabel = ({
2425
handleInputChange(e)
2526

2627
if (e.key === 'Enter') {
27-
updateProfile()
28+
updateProfile(name)
2829
setEditMode(!editMode)
2930
}
3031
}}
@@ -58,7 +59,10 @@ export const EditableLabel = ({
5859
<button
5960
data-cy={`edit-${name}-btn`}
6061
className='editable-label-edit-btn'
61-
onClick={() => setEditMode(!editMode)}
62+
onClick={() => {
63+
setEditMode(!editMode)
64+
assignCurrField(value, name)
65+
}}
6266
>
6367
<EOS_MODE_EDIT className='eos-icons' />
6468
</button>
@@ -72,7 +76,7 @@ export const EditableLabel = ({
7276
data-cy='save-changes-btn'
7377
onClick={() => {
7478
setEditMode(!editMode)
75-
updateProfile()
79+
updateProfile(name)
7680
}}
7781
>
7882
<EOS_SAVE className='eos-icons' />

src/components/UserProfile.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export const UserProfile = ({
88
allowEditing,
99
user,
1010
handleInputChange,
11-
updateProfile
11+
updateProfile,
12+
assignCurrField
1213
}) => {
1314
return (
1415
<>
@@ -19,6 +20,7 @@ export const UserProfile = ({
1920
handleInputChange={handleInputChange}
2021
updateProfile={updateProfile}
2122
allowEditing={allowEditing}
23+
assignCurrField={assignCurrField}
2224
/>
2325
</div>
2426
{allowEditing && <UserSettings user={user} />}
@@ -90,7 +92,8 @@ export const UserDetails = ({
9092
user,
9193
handleInputChange,
9294
allowEditing,
93-
updateProfile
95+
updateProfile,
96+
assignCurrField
9497
}) => {
9598
return (
9699
<div className='user-profile-details'>
@@ -104,6 +107,7 @@ export const UserDetails = ({
104107
value={user.Name ?? user.username}
105108
className='input-default'
106109
handleInputChange={handleInputChange}
110+
assignCurrField={assignCurrField}
107111
allowEditing={allowEditing}
108112
updateProfile={updateProfile}
109113
placeholder='Your name'
@@ -118,6 +122,7 @@ export const UserDetails = ({
118122
name={'Bio'}
119123
value={user.Bio}
120124
handleInputChange={handleInputChange}
125+
assignCurrField={assignCurrField}
121126
allowEditing={allowEditing}
122127
updateProfile={updateProfile}
123128
placeholder='Say something about yourself'
@@ -144,6 +149,7 @@ export const UserDetails = ({
144149
name={'Profession'}
145150
value={user.Profession}
146151
handleInputChange={handleInputChange}
152+
assignCurrField={assignCurrField}
147153
allowEditing={allowEditing}
148154
updateProfile={updateProfile}
149155
placeholder='Your job title'
@@ -163,6 +169,7 @@ export const UserDetails = ({
163169
name={'Company'}
164170
value={user.Company}
165171
handleInputChange={handleInputChange}
172+
assignCurrField={assignCurrField}
166173
allowEditing={allowEditing}
167174
updateProfile={updateProfile}
168175
placeholder='Your company name'
@@ -182,6 +189,7 @@ export const UserDetails = ({
182189
name={'LinkedIn'}
183190
value={user.LinkedIn}
184191
handleInputChange={handleInputChange}
192+
assignCurrField={assignCurrField}
185193
allowEditing={allowEditing}
186194
updateProfile={updateProfile}
187195
placeholder='Your LinkedIn username'
@@ -212,6 +220,7 @@ export const UserDetails = ({
212220
name={'Twitter'}
213221
value={user.Twitter}
214222
handleInputChange={handleInputChange}
223+
assignCurrField={assignCurrField}
215224
allowEditing={allowEditing}
216225
updateProfile={updateProfile}
217226
placeholder='Your Twitter handle'

src/pages/MyProfile.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,39 @@ const MyProfile = () => {
1616

1717
const [, setUpdated] = useState(false)
1818

19-
const [updatingField, setUpdatingField] = useState('')
19+
const [updatingField, setUpdatingField] = useState({})
20+
21+
const [currFieldDefaultValue, setCurrFieldDefaultValue] = useState({})
22+
23+
const assignCurrField = (val, name) => {
24+
setCurrFieldDefaultValue((p) => ({
25+
...p,
26+
[name]: val
27+
}))
28+
setUpdatingField((p) => ({ ...p, [name]: val }))
29+
}
2030

2131
const handleInputChange = (event) => {
22-
setUpdatingField(event.target.value)
32+
setUpdatingField((p) => ({ ...p, [event.target.name]: event.target.value }))
2333
setUser({
2434
...user,
2535
[event.target.name]: event.target.value
2636
})
2737
}
2838

29-
const updateProfile = async () => {
39+
const updateProfile = async (name) => {
40+
if (currFieldDefaultValue[name] === updatingField[name]) {
41+
toast.success('Nothing changed here')
42+
return
43+
}
3044
try {
31-
if (!updatingField) {
32-
toast.error('Input cannot be empty')
33-
return
34-
}
3545
const response = await userStory.updateUser({ id: userId, ...user })
36-
if (response) {
37-
toast.success('Profile updated successfully')
38-
setUpdated(true)
46+
if (response && updatingField[name].length === 0) {
47+
toast.success('Your data has been removed')
48+
} else if (response && updatingField[name].length > 0) {
49+
toast.success('Your changes have been saved')
3950
}
51+
setUpdated(true)
4052
} catch (err) {
4153
console.error(err.message)
4254
toast.error(err.message)
@@ -68,6 +80,7 @@ const MyProfile = () => {
6880
user={user === '' ? '' : Object.assign(user, { id: userId })}
6981
handleInputChange={handleInputChange}
7082
updateProfile={updateProfile}
83+
assignCurrField={assignCurrField}
7184
allowEditing
7285
/>
7386
</div>

0 commit comments

Comments
 (0)