docbrown/app/javascript/contentDisplayPolicy/initHiddenComments.js
Andy Zhao 71157c993e Hide comment feature (#4944)
* Add MVP of hide comment feature

* Slight copy adjustments

* Remove unused file oops

* Fix strange styling issues

* Add hide/unhide comment specs

* Authenticate user for hide/unhide

* Add tests for hide/unhide functionality

* Remove opacity CSS for hidden comments

* Fix hidden comment explanation logic

* Fix some styling issues

* Fix hiding top level comment logic

* Show only hidden comments in permalink and not thread

* Hide subtree properly if hidden comment

* Fix weird CSS issue

* Properly hide comments for permalink view

* Show children comments in permalink view

* Add tests for comment hiding visibility

* Remove superfluous code and adjust copy

* Remove some more logical duplication

* Add dedicated article column for any comments hidden

* Add reload in test
2019-12-10 15:09:47 -05:00

62 lines
No EOL
1.7 KiB
JavaScript

/* eslint-disable no-alert */
export default function initHiddenComments() {
function hide(commentId) {
const confirmMsg = `
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
All child comments in this thread will also be hidden.
For further actions, you may consider blocking this person and/or reporting abuse.
`;
const confirmHide = window.confirm(confirmMsg)
if(confirmHide) {
fetch(`/comments/${commentId}/hide`, {
method: 'PATCH',
headers: {
'X-CSRF-Token': window.csrfToken,
},
})
.then(response => response.json())
.then(response => {
if (response.hidden === 'true') {
/* eslint-disable-next-line no-restricted-globals */
location.reload();
}
});
}
}
function unhide(commentId) {
fetch(`/comments/${commentId}/unhide`, {
method: 'PATCH',
headers: {
'X-CSRF-Token': window.csrfToken,
},
})
.then(response => response.json())
.then(response => {
if (response.hidden === 'false') {
/* eslint-disable-next-line no-restricted-globals */
location.reload();
}
});
}
const hideButtons = Array.from(
document.getElementsByClassName('hide-comment')
)
hideButtons.forEach(butt => {
const { hideType, commentId } = butt.dataset
if (hideType === 'hide') {
butt.addEventListener('click', () => {
hide(commentId)
})
} else if (hideType === 'unhide') {
butt.addEventListener('click', () => {
unhide(commentId);
});
}
})
}
/* eslint-enable no-alert */