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: 0, posted: false } // 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, posted} = 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 detail are both set, post the action if (itemType && detail && !posted) { // 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); this.setState({posted: true}); }); } } 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'}); } // 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}); } 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