-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ReportUserModal #4051
feat: ReportUserModal #4051
Changes from all commits
5a17ffe
0540f79
8be4262
eef302c
63e5977
aa74858
9b01e41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import type { ReactElement } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { ReasonSelectionModal } from './ReasonSelectionModal'; | ||
import type { ReportReason } from '../../../report'; | ||
import { ReportEntity, SEND_REPORT_MUTATION } from '../../../report'; | ||
import { Checkbox } from '../../fields/Checkbox'; | ||
import type { UserShortProfile } from '../../../lib/user'; | ||
import { gqlClient } from '../../../graphql/common'; | ||
import { | ||
CONTENT_PREFERENCE_BLOCK_MUTATION, | ||
ContentPreferenceType, | ||
} from '../../../graphql/contentPreference'; | ||
import { useAuthContext } from '../../../contexts/AuthContext'; | ||
import { useToastNotification } from '../../../hooks'; | ||
import { useLazyModal } from '../../../hooks/useLazyModal'; | ||
|
||
const reportReasons: { value: string; label: string }[] = [ | ||
{ value: 'INAPPROPRIATE', label: 'Inappropriate or NSFW Content' }, | ||
{ value: 'TROLLING', label: 'Trolling or Disruptive Behavior' }, | ||
{ value: 'HARASSMENT', label: 'Harassment or Bullying' }, | ||
{ value: 'IMPERSONATION', label: 'Impersonation or False Identity' }, | ||
{ value: 'SPAM', label: 'Spam or Unsolicited Advertising' }, | ||
{ value: 'MISINFORMATION', label: 'Misinformation or False Claims' }, | ||
{ value: 'HATE_SPEECH', label: 'Hate Speech or Discrimination' }, | ||
{ value: 'PRIVACY', label: 'Privacy or Copyright Violation' }, | ||
{ value: 'PLAGIARISM', label: 'Plagiarism or Content Theft' }, | ||
{ value: 'OTHER', label: 'Other' }, | ||
]; | ||
|
||
type ReportUserModalProps = { | ||
offendingUser: Pick<UserShortProfile, 'id' | 'username'>; | ||
defaultBlockUser?: boolean; | ||
}; | ||
|
||
export const ReportUserModal = ({ | ||
offendingUser, | ||
defaultBlockUser, | ||
}: ReportUserModalProps): ReactElement => { | ||
const { closeModal: onClose } = useLazyModal(); | ||
const { displayToast } = useToastNotification(); | ||
const { user } = useAuthContext(); | ||
const [blockUser, setBlockUser] = useState(defaultBlockUser); | ||
const { isPending: isBlockPending, mutateAsync: blockUserMutation } = | ||
useMutation({ | ||
mutationFn: () => | ||
gqlClient.request(CONTENT_PREFERENCE_BLOCK_MUTATION, { | ||
id: offendingUser.id, | ||
entity: ContentPreferenceType.User, | ||
feedId: user?.id, | ||
}), | ||
onSuccess: () => { | ||
displayToast(`🚫 ${offendingUser.username} has been blocked`); | ||
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a question: Do we need to invalidate some query (like the current feed) on success? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. Not entirely sure. IIRC we don't invalidate the current feed when blocking sources, tags etc, just the selected post. I guess adding optional callbacks wouldn't be a bad idea. I can check with product There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normally we do refresh/remove the post from the feed relevant to the action, but we can maybe check with the product team on this one. |
||
onClose(); | ||
}, | ||
onError: () => { | ||
displayToast(`❌ Failed to block ${offendingUser.username}`); | ||
}, | ||
}); | ||
const { isPending: isReportPending, mutateAsync: reportUserMutation } = | ||
useMutation({ | ||
mutationFn: ({ reason, text }: { reason: ReportReason; text: string }) => | ||
gqlClient.request(SEND_REPORT_MUTATION, { | ||
id: offendingUser.id, | ||
type: ReportEntity.User, | ||
reason, | ||
comment: text, | ||
}), | ||
onSuccess: () => { | ||
if (!blockUser) { | ||
displayToast(`🗒️ ${offendingUser.username} has been reported`); | ||
onClose(); | ||
} | ||
}, | ||
onError: () => { | ||
displayToast(`❌ Failed to report ${offendingUser.username}`); | ||
}, | ||
}); | ||
|
||
const onReportUser = ( | ||
e: React.MouseEvent, | ||
reason: ReportReason, | ||
text: string, | ||
) => { | ||
e.preventDefault(); | ||
reportUserMutation({ reason, text }); | ||
if (blockUser) { | ||
blockUserMutation(); | ||
} | ||
}; | ||
|
||
const isPending = isBlockPending || isReportPending; | ||
const checkboxDisabled = defaultBlockUser || isPending; | ||
return ( | ||
<ReasonSelectionModal | ||
isOpen | ||
onReport={onReportUser} | ||
disabled={isPending} | ||
reasons={reportReasons} | ||
onRequestClose={onClose} | ||
heading="Select reason for reporting" | ||
footer={ | ||
<Checkbox | ||
name="blockUser" | ||
className="font-normal" | ||
disabled={checkboxDisabled} | ||
onChange={(e) => setBlockUser(e.target.checked)} | ||
checked={blockUser} | ||
> | ||
Block {offendingUser.username} | ||
</Checkbox> | ||
} | ||
/> | ||
); | ||
}; | ||
|
||
export default ReportUserModal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with Nimrod to change the message to this