docbrown/app/javascript/profileDropdown/flagButton.js
Michael Kohl 09828853f6
✂✂✂ Remove Connect (#14734)
* Remove Connect

* Remove more Connect specs

* Remove a lot more Connect code

* 🚮

* It all has to go

* Explicitly add httpclient

* Update application layout

* Remove messages association from User

* Start fixing specs

* reintroduce util function and refactor references

* Remove Connect Cypress test

* Fix more specs

* Remove Connect from listings

* Ignore contact_via_connect column on listings

* Remove contact_via_connect usages

* Ignore mod_chat_channel_id on tags

* Drop Connect tables

* Remove email_connect_messages from user notification settings

* Re-add httpclient 2.8.3

This was mistakenly removed as a merge conflict

* Don't need to exclude removed chat channel file

* Remove unneeded style for chat channels

* Remove unneeded channel list prop type

* Remove chat channels index/connect-link from getPageEntries

* Re-add comment from httpclient in Gemfile

* Remove connect references from mailers

Tag Moderators no longer have a chat channel

No longer will users be notified about new messages (there won't be
any)

No longer will users be notified about channel invites (you can't
invite anyone anymore)

* Don't configure Pusher and remove PUSHER_* from .env_sample

since it's removed from gemfile, the Pusher constant will not resolve, if this is
configured in the environment variables we'll fail to boot.

Co-authored-by: Suzanne Aitchison <suzanne@forem.com>
Co-authored-by: Dan Uber <dan@forem.com>
2021-11-18 08:21:00 -06:00

82 lines
2.3 KiB
JavaScript

/* global userData */
/* eslint-disable no-alert, import/order */
import { request } from '@utilities/http';
import { getUserDataAndCsrfToken } from '@utilities/getUserDataAndCsrfToken';
function addFlagUserBehavior(flagButton) {
const { profileUserId, profileUserName } = flagButton.dataset;
let isUserFlagged = flagButton.dataset.isUserFlagged === 'true';
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) {
request('/reactions', {
method: 'POST',
body: {
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) => {
Honeybadger.notify(
isUserFlagged ? 'Unable to unflag user' : 'Unable to flag user',
profileUserId,
);
window.alert(`Something went wrong: ${e}`);
});
}
}
flagButton.addEventListener('click', flag);
}
/**
* 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;
}
getUserDataAndCsrfToken().then(() => {
const user = userData();
if (!user) {
flagButton.remove();
return;
}
const trustedOrAdmin = user.trusted || user.admin;
const { profileUserId } = flagButton.dataset;
if (!trustedOrAdmin || user.id === parseInt(profileUserId, 10)) {
flagButton.remove();
}
addFlagUserBehavior(flagButton);
});
}
/* eslint-enable no-alert */