Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyProfile - Validate input string for empty values #204

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/assets/scss/pages/home.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ $subheader-font-size: 18px;
}

.options-bar {
justify-content: center;
background-color: $eos-white;
border-radius: 10px;
justify-content: center;
padding: 8px;
}

Expand Down
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
28 changes: 24 additions & 4 deletions src/pages/MyProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,39 @@ const MyProfile = () => {

const [, setUpdated] = useState(false)

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((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 {
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 @@ -61,6 +80,7 @@ const MyProfile = () => {
user={user === '' ? '' : Object.assign(user, { id: userId })}
handleInputChange={handleInputChange}
updateProfile={updateProfile}
assignCurrField={assignCurrField}
allowEditing
/>
</div>
Expand Down