import React, {Component} from 'react'; import {I18n} from '../coral-framework'; import translations from './translations.json'; import {PopupMenu, Button} from 'coral-ui'; import onClickOutside from 'react-onclickoutside'; const name = 'coral-plugin-flags'; class FlagButton extends Component { state = { showMenu: false, itemType: '', reason: '', message: '', step: 0, posted: false, localPost: null, localDelete: false } // When the "report" button is clicked expand the menu onReportClick = () => { const {currentUser, flag, deleteAction} = this.props; const {localPost, localDelete} = this.state; const flagged = (flag && flag.current_user && !localDelete) || localPost; if (!currentUser) { const offset = document.getElementById(`c_${this.props.id}`).getBoundingClientRect().top - 75; this.props.showSignInDialog(offset); return; } if (flagged) { this.setState((prev) => prev.localPost ? {...prev, localPost: null, step: 0} : {...prev, localDelete: true}); deleteAction(localPost || flag.current_user.id); } else { this.setState({showMenu: !this.state.showMenu}); } } onPopupContinue = () => { const {postFlag, postDontAgree, id, author_id} = this.props; const {itemType, reason, step, posted, message} = this.state; // Proceed to the next step or close the menu if we've reached the end if (step + 1 >= this.props.getPopupMenu.length) { this.setState({showMenu: false}); } else { this.setState({step: step + 1}); } // If itemType and reason are both set, post the action if (itemType && reason && !posted) { this.setState({posted: true}); let item_id; switch(itemType) { case 'COMMENTS': item_id = id; break; case 'USERS': item_id = author_id; break; } if (itemType === 'COMMENTS') { this.setState({localPost: 'temp'}); } let action = { item_id, item_type: itemType, reason, message }; if (reason === 'I don\'t agree with this comment') { postDontAgree(action) .then(({data}) => { if (itemType === 'COMMENTS') { this.setState({localPost: data.createDontAgree.dontagree.id}); } }); } else { postFlag(action) .then(({data}) => { if (itemType === 'COMMENTS') { this.setState({localPost: data.createFlag.flag.id}); } }); } } } onPopupOptionClick = (sets) => (e) => { // If flagging a user, indicate that this is referencing the username rather than the bio if(sets === 'itemType' && e.target.value === 'users') { this.setState({field: 'username'}); } // Set itemType and field if they are defined in the popupMenu const currentMenu = this.props.getPopupMenu[this.state.step](); if (currentMenu.itemType) { this.setState({itemType: currentMenu.itemType}); } if (currentMenu.field) { this.setState({field: currentMenu.field}); } this.setState({[sets]: e.target.value}); } onNoteTextChange = (e) => { this.setState({message: e.target.value}); } handleClickOutside () { this.setState({showMenu: false}); } render () { const {flag, getPopupMenu} = this.props; const {localPost, localDelete} = this.state; const flagged = (flag && flag.current_user && !localDelete) || localPost; const popupMenu = getPopupMenu[this.state.step](this.state.itemType); return