Skip to content

Commit

Permalink
[DT-744] Add option to modify email preferences (#2717)
Browse files Browse the repository at this point in the history
  • Loading branch information
fboulnois authored Nov 13, 2024
1 parent 2260bfc commit b4b1f12
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
50 changes: 50 additions & 0 deletions cypress/component/UserProfile/user_profile.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable no-undef */

import {mount} from 'cypress/react';
import React from 'react';
import {Storage} from '../../../src/libs/storage';
import {User} from '../../../src/libs/ajax/User';
import {Institution} from '../../../src/libs/ajax/Institution';
import UserProfile from '../../../src/pages/user_profile/UserProfile';

const duosUser = {
isSigningOfficial: false,
};

describe('User Profile', () => {
// Intercept configuration calls
beforeEach(() => {
cy.intercept({
method: 'GET',
url: '/config.json',
hostname: 'localhost',
}, {'env': 'ci'});
});

it('Renders the user profile page', () => {
cy.stub(Storage, 'getCurrentUser').returns(duosUser);
cy.stub(Institution, 'list').returns([]);
cy.stub(User, 'getMe').returns(duosUser);
cy.stub(User, 'getApprovedDatasets').returns([]);
cy.stub(User, 'getAcknowledgements').returns({});
mount(<UserProfile/>);
cy.get('h2').should('contain', 'Your Profile');
});

it('Updates the user email preferences', () => {
cy.stub(Storage, 'getCurrentUser').returns(duosUser);
cy.stub(Institution, 'list').returns([]);
cy.stub(User, 'getMe').returns(duosUser);
cy.stub(User, 'getApprovedDatasets').returns([]);
cy.stub(User, 'getAcknowledgements').returns({});
cy.intercept(
{method: 'PUT', url: '**/user'},
{statusCode: 200, body: duosUser}
).as('updateSelf');
mount(<UserProfile/>);
cy.get('input[id="profileEmailEnabled_yes"]').check();
cy.wait('@updateSelf').then(() => {
cy.get('div').contains('Email preference updated successfully!');
});
});
});
32 changes: 31 additions & 1 deletion src/pages/user_profile/UserProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function UserProfile(props) {
const [profile, setProfile] = useState({
profileName: '',
email: undefined,
emailPreference: undefined,
id: undefined
});

Expand Down Expand Up @@ -54,6 +55,19 @@ export default function UserProfile(props) {
}
};

const updateEmailPreference = (value) => {
const payload = {
emailPreference: value
};

User.updateSelf(payload).then((response) => {
setUserRoleStatuses(response, Storage);
Notifications.showSuccess({ text: 'Email preference updated successfully!' });
}, () => {
Notifications.showError({ text: 'Some errors occurred, the user\'s email preference was not updated.' });
});
}


useEffect(() => {
const init = async () => {
Expand All @@ -64,6 +78,7 @@ export default function UserProfile(props) {
setProfile({
profileName: user.displayName,
email: user.email,
emailPreference: user.emailPreference,
id: user.userId
});
setName(user.displayName);
Expand Down Expand Up @@ -179,7 +194,22 @@ export default function UserProfile(props) {
defaultValue={profile.email}
disabled={true}
/>
<div style={{ marginTop: '60px' }} />
<div style={{ marginTop: '10px' }} />
<p
style={{
color: '#000',
fontSize: '16px',
fontWeight: '400',
}}
>
Send me email notifications
</p>
<FormField
type={FormFieldTypes.YESNORADIOGROUP}
id='profileEmailEnabled'
defaultValue={profile.emailPreference}
onChange={(field) => updateEmailPreference(field.value)}
/>
<div style={{ 'marginTop': '60px' }} />
<ControlledAccessGrants />
<div style={{ 'marginTop': '60px' }} />
Expand Down

0 comments on commit b4b1f12

Please sign in to comment.