docbrown/app/javascript/admin/controllers/config_controller.js
Arit Amana 1912ffdc46
[Team Email Login] Refactor the Email Authentication flow (#11090)
* Hooked "Enable" button to hidden checkbox

* Hooked "Close" button to close email settings and show "Enable/Edit" button

* Additional hookups

* Start building generalized Modal blocks

* Everything hooked up except styling and a few Qs

* last of the hookups; ensure logic flow

* clean up

* specs to cover email auth refactor

* Fix bug surfaced by Vaidehi

* Incorporate PR feedback

* prevent email auth disable if invite-only-mode

* adjust emailAuthModal body text

* Sundry improvements

* Last-mile tweaks

* Trying to get 3rd party auth deselect to work

* delete unnecssary function

* remove superfluous comment

* Move inline styling into CSS file

* Incorporate PR feedback

* Incorporate more PR feedback

* Make Confirm btn intent clearer

* Add TODO comment
2020-10-28 13:37:11 -04:00

139 lines
4.6 KiB
JavaScript

import { Controller } from 'stimulus';
const recaptchaFields = document.querySelector('#recaptchaContainer');
const emailSigninAndLoginCheckbox = document.querySelector(
'#email-signup-and-login-checkbox',
);
const emailAuthSettingsSection = document.querySelector(
'#email-auth-settings-section',
);
const modalAnchor = document.querySelector('.admin-config-modal-anchor');
const emailAuthModalTitle = 'Disable email address registration';
// TODO: Remove the sentence "You must update site config to save this action!"
// once we build more robust flow for Admin/Config
const emailAuthModalBody =
'<p>If you disable email address as a registration option, people cannot create an account with their email address.</p><br /><p>However, people who have already created an account using their email address can continue to login.</p><br /><p><strong>You must update site config to save this action!</strong></p>';
const adminConfigModal = (
title,
body,
confirmBtnText,
confirmBtnAction,
cancelBtnText,
cancelBtnAction,
) => `
<div class="crayons-modal crayons-modal--s absolute">
<div class="crayons-modal__box">
<header class="crayons-modal__box__header">
<p class="fw-bold fs-l">${title}</p>
<button type="button" class="crayons-btn crayons-btn--icon crayons-btn--ghost" data-action="click->config#closeAdminConfigModal">
<svg width="24" height="24" viewBox="0 0 24 24" class="crayons-icon" xmlns="http://www.w3.org/2000/svg">
<path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636l4.95 4.95z" />
</svg>
</button>
</header>
<div class="crayons-modal__box__body">
${body}
<div class="mt-6">
<button
class="crayons-btn crayons-btn--danger"
data-action="click->config#${confirmBtnAction}">
${confirmBtnText}
</button>
<button
class="crayons-btn crayons-btn--secondary"
data-action="click->config#${cancelBtnAction}">
${cancelBtnText}
</button>
</div>
</div>
</div>
<div class="crayons-modal__overlay"></div>
</div>
`;
export default class ConfigController extends Controller {
static targets = [
'authenticationProviders',
'collectiveNoun',
'emailAuthSettingsBtn',
'inviteOnlyMode',
'requireCaptchaForEmailPasswordRegistration',
];
disableTargetField(event) {
const targetElementName = event.target.dataset.disableTarget;
const targetElement = this[`${targetElementName}Target`];
const newValue = event.target.checked;
targetElement.disabled = newValue;
// Disable the button generated by ERB for select tags
if (targetElement.nodeName === 'SELECT') {
const snakeCaseName = targetElementName.replace(
/[A-Z]/g,
(letter) => `_${letter.toLowerCase()}`,
);
document.querySelector(
`button[data-id=site_config_${snakeCaseName}]`,
).disabled = newValue;
}
}
toggleGoogleRecaptchaFields() {
if (this.requireCaptchaForEmailPasswordRegistrationTarget.checked) {
recaptchaFields.classList.remove('hidden');
} else {
recaptchaFields.classList.add('hidden');
}
}
enableOrEditEmailAuthSettings() {
event.preventDefault();
if (this.emailAuthSettingsBtnTarget.dataset.buttonText === 'enable') {
emailSigninAndLoginCheckbox.checked = true;
}
this.emailAuthSettingsBtnTarget.classList.add('hidden');
emailAuthSettingsSection.classList.remove('hidden');
}
hideEmailAuthSettings() {
event.preventDefault();
this.emailAuthSettingsBtnTarget.classList.remove('hidden');
emailAuthSettingsSection.classList.add('hidden');
}
activateEmailAuthModal() {
event.preventDefault();
modalAnchor.innerHTML = adminConfigModal(
emailAuthModalTitle,
emailAuthModalBody,
'Confirm',
'disableEmailAuthFromModal',
'Cancel',
'closeAdminConfigModal',
);
if (document.querySelector('.crayons-modal')) {
window.scrollTo(0, 0);
document.body.style.height = '100vh';
document.body.style.overflowY = 'hidden';
}
}
closeAdminConfigModal() {
modalAnchor.innerHTML = '';
document.body.style.height = 'inherit';
document.body.style.overflowY = 'inherit';
}
disableEmailAuthFromModal() {
event.preventDefault();
emailSigninAndLoginCheckbox.checked = false;
this.closeAdminConfigModal();
}
disableEmailAuth() {
event.preventDefault();
emailSigninAndLoginCheckbox.checked = false;
this.hideEmailAuthSettings();
}
}