docbrown/app/javascript/packs/notificationSubscriptionHandler.js
Suzanne Aitchison 5bed8f56d4
improve keyboard accessibility of modals 12427 10610 (#12511)
* adds focusTrap wrapper to preact Modal component

* add view specific code for focus trap in admin add nav link

* add script to return a focustrap toggle, use in add nav link modal partial

* add trap to edit nav link modal

* add handlers for sign up modal

* update modal controller for admin section, update nav link modals to use

* update other admin modals with new data values for trap

* remove unneeded erb script file

* remove unneeded target

* refactor to remove extra unneeded param

* remove duplicate code, store getFocusTrapToggle in window

* trap focus in comment and bookmark showModal instances for not logged in user

* remove need for activator id

* clean up id refs no longer needed

* remove custom code and re-use focsu-trap lib

* update storybook docs

* update default export in focusTrap

* prevent close button click triggering a modal toggle twice

* ensure if user navigates from a modal the trap is deactivated

* add jsdoc comments and add dynamic import

* ensure admin controller modal traps are cleaned up on disconnect

* update sign up modal to use crayons

* update modal controller and admin nav links modals to use preact modal

* update profile fields modals for new controller

* tweak styling of sign up and admin modals to match previous

* update listings modal to use crayons modal, adapt focus trap to work with click outside

* memoize deactivate callback to ensure modal can be presented on first page load

* add missed focustrap changes

* fix focus trap issues in onboarding flow

* refactor onboarding focus trap, remove getFocusTrapToggle

* tweaks for styling and article modal toggle

* add click outside tests to modal

* add cypress tests for the login modal

* update liquid tag tests affected by change

* refactors to address review comments

* fix issue with login modal presented twice on comment add

* change ids to selectors in admin modals

* small pr comment refactors

* add listings e2e tests

* add nav link modal tests

* fix issue with help modal

* tweak to fix ui bug from merge

* remove context from showLoginModal

* rename toggleModal

* rename state property for clarity

Co-authored-by: Nick Taylor <nick@dev.to>
2021-02-24 16:01:10 +00:00

100 lines
2.9 KiB
JavaScript

/* global showLoginModal */
function loadFunctionality() {
if (!document.getElementById('notification-subscriptions-area')) {
return;
}
const { notifiableId } = document.getElementById(
'notification-subscriptions-area',
).dataset;
const { notifiableType } = document.getElementById(
'notification-subscriptions-area',
).dataset;
const userStatus = document.body.getAttribute('data-user-status');
if (userStatus === 'logged-in') {
fetch(`/notification_subscriptions/${notifiableType}/${notifiableId}`, {
headers: {
Accept: 'application/json',
'X-CSRF-Token': window.csrfToken,
'Content-Type': 'application/json',
},
credentials: 'same-origin',
})
.then((response) => response.json())
.then((result) => {
document
.getElementById(`notification-subscription-label_${result.config}`)
.classList.add('selected');
// checkbox.checked = result;
});
}
let updateStatus = () => {};
if (userStatus === 'logged-out') {
updateStatus = () => {
showLoginModal();
};
} else {
updateStatus = (target) => {
let payload = '';
const shouldUnsubscribeToNotifications =
target.classList.contains('selected') ||
target.classList.contains('selected-emoji');
const allButtons = document.getElementsByClassName(
'notification-subscription-label',
);
for (let i = 0; i < allButtons.length; i += 1) {
allButtons[i].classList.remove('selected');
}
if (shouldUnsubscribeToNotifications) {
const unsubscribeButton = allButtons.namedItem('unsubscribe');
unsubscribeButton.classList.add('selected');
({ payload } = unsubscribeButton.dataset);
} else {
target.classList.add('selected');
({ payload } = target.dataset);
}
fetch(`/notification_subscriptions/${notifiableType}/${notifiableId}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'X-CSRF-Token': window.csrfToken,
'Content-Type': 'application/json',
},
credentials: 'same-origin',
body: JSON.stringify({
config: payload,
// notifiable params are passed via URL
}),
});
};
}
const subscriptionButtons = document.getElementsByClassName(
'notification-subscription-label',
);
for (let i = 0; i < subscriptionButtons.length; i += 1) {
subscriptionButtons[i].addEventListener('click', (e) => {
e.preventDefault();
updateStatus(e.target);
if (typeof window.sendHapticMessage !== 'undefined') {
window.sendHapticMessage('medium');
}
});
subscriptionButtons[i].addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
updateStatus(e.target);
}
});
}
}
window.InstantClick.on('change', () => {
loadFunctionality();
});
loadFunctionality();