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

feat(organizations): disable email updates for regular organization members TASK-997 #5233

Merged
merged 11 commits into from
Nov 21, 2024
91 changes: 57 additions & 34 deletions jsapp/js/account/security/email/emailSection.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {formatTime, notify} from 'js/utils';
// Styles
import styles from './emailSection.module.scss';
import securityStyles from 'js/account/security/securityRoute.module.scss';
import {useOrganizationQuery} from 'js/account/stripe.api';

interface EmailState {
emails: EmailResponse[];
Expand All @@ -33,6 +34,8 @@ interface EmailState {
export default function EmailSection() {
const [session] = useState(() => sessionStore);

const orgQuery = useOrganizationQuery();

let initialEmail = '';
if ('email' in session.currentAccount) {
initialEmail = session.currentAccount.email;
Expand All @@ -55,15 +58,20 @@ export default function EmailSection() {
}, []);

function setNewUserEmail(newEmail: string) {
setUserEmail(newEmail).then(() => {
getUserEmails().then((data) => {
setEmail({
...email,
emails: data.results,
newEmail: '',
setUserEmail(newEmail).then(
() => {
getUserEmails().then((data) => {
setEmail({
...email,
emails: data.results,
newEmail: '',
});
});
});
}, () => {/* Avoid crashing app when 500 error happens */});
},
() => {
/* Avoid crashing app when 500 error happens */
}
);
}

function deleteNewUserEmail() {
Expand Down Expand Up @@ -112,6 +120,14 @@ export default function EmailSection() {
}
}

function userCanChangeEmail() {
if (orgQuery.data?.is_mmo) {
return orgQuery.data.request_user_role !== 'member';
} else {
return true;
}
}

const currentAccount = session.currentAccount;
const unverifiedEmail = email.emails.find(
(userEmail) => !userEmail.verified && !userEmail.primary
Expand All @@ -120,20 +136,24 @@ export default function EmailSection() {
return (
<section className={securityStyles.securitySection}>
<div className={securityStyles.securitySectionTitle}>
<h2 className={securityStyles.securitySectionTitleText}>{t('Email address')}</h2>
<h2 className={securityStyles.securitySectionTitleText}>
{t('Email address')}
</h2>
</div>

<div className={cx(securityStyles.securitySectionBody, styles.body)}>
{!session.isPending &&
session.isInitialLoadComplete &&
'email' in currentAccount && (
<TextBox
value={email.newEmail}
placeholder={t('Type new email address')}
onChange={onTextFieldChange.bind(onTextFieldChange)}
type='email'
/>
)}
session.isInitialLoadComplete &&
'email' in currentAccount &&
userCanChangeEmail() ? (
<TextBox
value={email.newEmail}
placeholder={t('Type new email address')}
onChange={onTextFieldChange.bind(onTextFieldChange)}
type='email'
/>
) : (
<div className={styles.emailText}>{email.newEmail}</div>
)}

{unverifiedEmail?.email &&
!session.isPending &&
Expand Down Expand Up @@ -185,21 +205,24 @@ export default function EmailSection() {
</>
)}
</div>

<form
className={styles.options}
onSubmit={(e) => {
e.preventDefault();
handleSubmit();
}}
>
<Button
label='Change'
size='m'
type='primary'
onClick={handleSubmit}
/>
</form>
<div className={styles.options}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this container div inside the userCanChangeEmail check?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a hack, if the container div is inside of the check then the rest of the row CSS gets unaligned. The only way I saw to fix this was to add conditional styling which I thought looked worse than this. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I wondered if that might be the reason. I'd say we should go with conditional CSS. You're right that it will add some more complexity, but I think it will make it easier for other coders working on this section in the future to know what's going on.

{userCanChangeEmail() && (
<form
onSubmit={(e) => {
e.preventDefault();
handleSubmit();
}}
>
<Button
label='Change'
size='m'
type='primary'
onClick={handleSubmit}
/>
)
</form>
)}
</div>
</section>
);
}
5 changes: 5 additions & 0 deletions jsapp/js/account/security/email/emailSection.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@
display: flex;
gap: 10px;
}

.emailText {
font-weight: 600;
color: colors.$kobo-gray-500;
}
Loading