docbrown/app/javascript/profileDropdown/blockButton.js
Andy Zhao 73caa9a864 New Feature: Block Users (#4411)
* Prevent need for eager loading

* Add initial implementation of user block

* Remove debugger oops

* Add index and foreign keys for user_block table

* Use private method instead of exists

* Move channel handling logic to service object

* Update styling a bit

* Use profileDropdown file for future proofing

* Remove commented out code

* Render json: { result } for all endpoints

* Add statuses for sad paths

* Add better sad path handling in the JS

* Remove accidentally committed spec file

* Don't wait for DOM to load block button

* Use equality for accuracy in slug query

* Add status code for unauthorized requests

* Add missing comma sigh
2019-10-23 17:14:28 -04:00

90 lines
2.9 KiB
JavaScript

export default function initBlock() {
const blockButton = document.getElementById(
'user-profile-dropdownmenu-block-button',
);
const { profileUserId } = blockButton.dataset;
function unblock() {
fetch(`/user_blocks/${profileUserId}`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'X-CSRF-Token': window.csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_block: {
blocked_id: profileUserId,
},
}),
})
.then(response => response.json())
.then(response => {
if (response.result === 'unblocked') {
blockButton.innerText = 'Block';
blockButton.addEventListener('click', block, { once: true });
} else if (response.status === 422) {
window.alert(`Something went wrong: ${e} -- Please refresh the page to try again.`)
}
})
.catch(e => {
window.alert(`Something went wrong: ${e}. -- Please refresh the page to try again.`);
});
}
function block() {
const confirmBlock = window.confirm(
`Are you sure you want to block this person? This will:
- prevent them from commenting on your posts
- block all notifications from them
- prevent them from messaging you via DEV Connect`,
);
if (confirmBlock) {
fetch(`/user_blocks/${profileUserId}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'X-CSRF-Token': window.csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_block: {
blocked_id: profileUserId,
},
}),
})
.then(response => response.json())
.then(response => {
if (response.result === 'blocked') {
blockButton.innerText = 'Unblock';
blockButton.addEventListener('click', unblock, { once: true });
} else if (response.status === 422) {
window.alert(`Something went wrong: ${e}. -- Please refresh the page to try again.`)
}
})
.catch(e => {
window.alert(`Something went wrong: ${e}. -- Please refresh the page to try again.`);
});
} else {
blockButton.addEventListener('click', block, { once: true });
}
}
// userData() is a global function
const currentUserId = userData().id;
if (currentUserId === parseInt(profileUserId, 10)) {
blockButton.style.display = 'none';
} else {
fetch(`/user_blocks/${profileUserId}`)
.then(response => response.json())
.then(response => {
if (response.result === 'blocking') {
blockButton.innerText = 'Unblock';
blockButton.addEventListener('click', unblock, { once: true });
} else {
blockButton.addEventListener('click', block, { once: true });
}
});
}
}