* Add flag link to profile dropdown view * Add JS for flag button on profile * Only show flag button to trusted users * Quick fix for accidental unflagging This will be more properly addressed in a future refactoring. * Update reaction specs * Update spec * Make flagging togglable * Make CodeClimate happy * Revert accidentally changed file * CodeClimate * Update app/javascript/profileDropdown/flagButton.js Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com> * Add JSDoc * Remove second popup * Refactor and add system spec * CodeClimate * Change send to public_send * Address PR feedback by @aitchiss Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>
67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
/* global userData */
|
|
/* eslint-disable no-alert */
|
|
/**
|
|
* Adds a flag button visible only to trusted users on profile pages.
|
|
* @function initFlag
|
|
* @returns {(void|undefined)} This function has no useable return value.
|
|
*/
|
|
export function initFlag() {
|
|
const flagButton = document.getElementById(
|
|
'user-profile-dropdownmenu-flag-button',
|
|
);
|
|
|
|
if (!flagButton) {
|
|
// button not always present when this is called
|
|
return;
|
|
}
|
|
|
|
const user = userData();
|
|
if (!user) {
|
|
return;
|
|
}
|
|
|
|
const { profileUserId, profileUserName } = flagButton.dataset;
|
|
let isUserFlagged = flagButton.dataset.isUserFlagged === 'true';
|
|
const trustedOrAdmin = user.trusted || user.admin;
|
|
|
|
if (!trustedOrAdmin || user.id === parseInt(profileUserId, 10)) {
|
|
flagButton.remove();
|
|
}
|
|
|
|
function flag() {
|
|
const confirmFlag = window.confirm(
|
|
isUserFlagged
|
|
? 'Are you sure you want to unflag this person? This will make all of their posts visible again.'
|
|
: 'Are you sure you want to flag this person? This will make all of their posts less visible.',
|
|
);
|
|
|
|
if (confirmFlag) {
|
|
fetch('/reactions', {
|
|
method: 'POST',
|
|
headers: {
|
|
'X-CSRF-Token': window.csrfToken,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
reactable_type: 'User',
|
|
category: 'vomit',
|
|
reactable_id: profileUserId,
|
|
}),
|
|
})
|
|
.then((response) => response.json())
|
|
.then((response) => {
|
|
if (response.result === 'create') {
|
|
isUserFlagged = true;
|
|
flagButton.innerHTML = `Unflag ${profileUserName}`;
|
|
} else {
|
|
isUserFlagged = false;
|
|
flagButton.innerHTML = `Flag ${profileUserName}`;
|
|
}
|
|
})
|
|
.catch((e) => window.alert(`Something went wrong: ${e}`));
|
|
}
|
|
}
|
|
|
|
flagButton.addEventListener('click', flag);
|
|
}
|
|
/* eslint-enable no-alert */
|