docbrown/app/javascript/packs/notificationSubscriptionHandler.js
Andy Zhao b953f17d0c New Feature: Subscribe to posts! (#3149)
* Add MVP of notification subscriptions

* Add rate limiting for notification subscriptions

* Show modal for logged out users on subscribe click

* Add enter key functionality for checkbox

* Add relationships for notification subscriptions

* Add model test for notification subscription

* Deprecated article mutes in favor of notification subscriptions

* Deprecated comment mutes in favor of notification subscriptions

* Move comment muting tests and logic over

* Merge comment and article muting into subscriptions

* Remove redundant check

* Use database to check for rate limit instead of cache

* Test for subscriptions handling notifications properly

* Fix notification model spec for subscriptions

* Fix tests for subscriptions

* Remove rate limiting

* Properly handle logged out users getting subscription status

* Fix test for new pattern

* Update reserved words

* Add config column to notification subscriptions

* Add logic for top level config and move tests

* Test for top level subscribers getting notified

* Fix logic mistake oops

* Add index and refactor comment_user_ids
2019-06-14 12:03:43 -04:00

86 lines
2.4 KiB
JavaScript

const label = document.getElementById('notification-subscription-label');
const checkbox = document.getElementById('notification-subcription-checkbox');
const subscriptionStatusInput = document.getElementById(
'notification-subscription-status',
);
const notifiableId = document.getElementById(
'notification-subscription-notifiable-id',
).value;
const notifiableType = document.getElementById(
'notification-subscription-notifiable-type',
).value;
const userStatus = document
.getElementsByTagName('body')[0]
.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 => {
subscriptionStatusInput.value = result;
checkbox.checked = result;
});
}
let updateStatus = () => {};
if (userStatus === 'logged-out') {
updateStatus = () => {
// Disabled because showModal() is globally defined in asset pipeline
// eslint-disable-next-line no-undef
showModal('notification-subscription');
};
} else {
updateStatus = () => {
checkbox.checked = !checkbox.checked;
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({
currently_subscribed: subscriptionStatusInput.value,
// notifiable params are passed via URL
}),
})
.then(response => response.json())
.then(result => {
subscriptionStatusInput.value = result;
checkbox.checked = result;
label.classList.remove('enabled');
label.classList.add('disabled');
setTimeout(() => {
label.classList.remove('disabled');
label.classList.add('enabled');
}, 1500);
});
};
}
label.addEventListener('click', e => {
e.preventDefault();
updateStatus();
});
checkbox.addEventListener('click', e => {
e.preventDefault();
updateStatus();
});
checkbox.addEventListener('keydown', e => {
if (e.key === 'Enter') {
updateStatus();
}
});