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, showOther: false, itemType: '', detail: '', otherText: '', step: 1 } // When the "report" button is clicked expand the menu onReportClick = () => { if (!this.props.currentUser) { const offset = document.getElementById(`c_${this.props.id}`).getBoundingClientRect().top - 75; this.props.showSignInDialog(offset); return; } this.setState({showMenu: !this.state.showMenu}); } onPopupContinue = () => { const {postAction, addItem, updateItem, flag, id, author_id} = this.props; const {itemType, field, detail, step, otherText} = this.state; //Proceed to the next step this.setState({step: step + 1}); // If itemType and detail are both set, post the action if (itemType && detail) { // Set the text from the "other" field if it exists. const updatedDetail = otherText || detail; let item_id; switch(itemType) { case 'comments': item_id = id; break; case 'user': item_id = author_id; break; } const action = { action_type: 'flag', field, detail: updatedDetail }; postAction(item_id, itemType, action) .then((action) => { let id = `${action.action_type}_${action.item_id}`; addItem({id, current_user: action, count: flag ? flag.count + 1 : 1}, 'actions'); updateItem(action.item_id, action.action_type, id, action.item_type); }); } } // When a popup option is clicked, update the state onPopupOptionClick = (sets) => (e) => { // If the "other" option is clicked, show the other textbox if(sets === 'detail' && e.target.value === 'other') { this.setState({showOther: true}); } // If flagging a user, indicate that this is referencing the username rather than the bio if(sets === 'itemType' && e.target.value === 'user') { this.setState({field: 'username'}); } this.setState({[sets]: e.target.value}); } onOtherTextChange = (e) => { this.setState({otherText: e.target.value}); } handleClickOutside () { this.setState({showMenu: false}); } render () { const {flag, getPopupMenu} = this.props; const flagged = flag && flag.current_user; const popupMenu = getPopupMenu(this.state.step, this.state.itemType); return
{ this.state.showMenu &&
{popupMenu.header}
{ popupMenu.text &&
{popupMenu.text}
} { popupMenu.options &&
{ popupMenu.options.map((option) =>

) } { this.state.showOther &&

}
}
{this.state.step} of 3
{ popupMenu.button && }
}
; } } export default onClickOutside(FlagButton); const styles = { flaggedIcon: { color: '#F00' }, unflaggedIcon: { color: 'inherit' } }; const lang = new I18n(translations);