From b4b1f12badd2541bb17a12575c4c50ac610f0806 Mon Sep 17 00:00:00 2001 From: fboulnois Date: Wed, 13 Nov 2024 15:11:42 -0500 Subject: [PATCH] [DT-744] Add option to modify email preferences (#2717) --- .../UserProfile/user_profile.spec.js | 50 +++++++++++++++++++ src/pages/user_profile/UserProfile.jsx | 32 +++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 cypress/component/UserProfile/user_profile.spec.js diff --git a/cypress/component/UserProfile/user_profile.spec.js b/cypress/component/UserProfile/user_profile.spec.js new file mode 100644 index 000000000..e50436dad --- /dev/null +++ b/cypress/component/UserProfile/user_profile.spec.js @@ -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(); + 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(); + cy.get('input[id="profileEmailEnabled_yes"]').check(); + cy.wait('@updateSelf').then(() => { + cy.get('div').contains('Email preference updated successfully!'); + }); + }); +}); diff --git a/src/pages/user_profile/UserProfile.jsx b/src/pages/user_profile/UserProfile.jsx index f247e1a9b..7135616b7 100644 --- a/src/pages/user_profile/UserProfile.jsx +++ b/src/pages/user_profile/UserProfile.jsx @@ -27,6 +27,7 @@ export default function UserProfile(props) { const [profile, setProfile] = useState({ profileName: '', email: undefined, + emailPreference: undefined, id: undefined }); @@ -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 () => { @@ -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); @@ -179,7 +194,22 @@ export default function UserProfile(props) { defaultValue={profile.email} disabled={true} /> -
+
+

+ Send me email notifications +

+ updateEmailPreference(field.value)} + />