import React from 'react'; import { OverlayTrigger, Tooltip } from 'react-bootstrap'; import { Profile, Fa, Card } from '../../components'; import moment from 'moment'; import { app } from '../../core/app'; import IssueFollowUpsBox from './issue-followups-box'; import CRUD from '../../commons/crud'; const crud = new CRUD('issuefollowup'); export default class IssueCard extends React.Component { constructor(props) { super(props); this.onFollowupEvent = this.onFollowupEvent.bind(this); this.addAnswer = this.addAnswer.bind(this); this.removeAnswer = this.removeAnswer.bind(this); this.editAnswer = this.editAnswer.bind(this); this.editIssueClick = this.editIssueClick.bind(this); this.confirmRemove = this.confirmRemove.bind(this); this.closeIssueClick = this.closeIssueClick.bind(this); this.reopenIssueClick = this.reopenIssueClick.bind(this); this.state = { doc: {}, showForm: false, answerBtnDisabled: true }; } onFollowupEvent(evt, data, txt) { switch (evt) { case 'add': return this.addAnswer(data); case 'remove': return this.removeAnswer(data); case 'edit': return this.editAnswer(data, txt); default: throw new Error('Invalid ' + evt); } } addAnswer(txt) { if (!this.props.issue.followups) { this.props.issue.followups = []; } // add the new comment on UI const newAnswer = { id: 'fakeid-' + this.props.issue.followups.length, issueId: this.props.issue.id, text: txt, user: { id: app.getState().session.userId, name: app.getState().session.userName }, unit: { id: app.getState().session.unitId, name: app.getState().session.unitName }, followupDate: new Date() }; this.props.issue.followups.push(newAnswer); this.forceUpdate(); // create the new issue on database const prom = crud.create(newAnswer) .then(id => { // updates new comment id, so edit and delete should work. newAnswer.id = id; this.forceUpdate(); }) .catch(() => { newAnswer.id = 'error-' + this.props.issue.followups.length; this.forceUpdate(); }); return prom; } removeAnswer(item) { // removes the comment from UI const lst = this.props.issue.followups; const index = lst.indexOf(item); this.props.issue.followups.splice(index, 1); this.forceUpdate(); // delete the new comment on database crud.delete(item.id); } editAnswer(item, txt) { // refresh the comment on UI item.text = txt; this.forceUpdate(); crud.update(item.id, item); } editIssueClick() { this.props.onIssueEvent('openedt', this.props.issue); } confirmRemove() { const self = this; app.messageDlg({ message: __('form.confirm_remove'), style: 'warning', type: 'YesNo' }).then(res => { if (res === 'yes') { self.props.onIssueEvent('remove', this.props.issue); } }); } closeIssueClick() { this.props.onIssueEvent('close', this.props.issue); } reopenIssueClick() { this.props.onIssueEvent('reopen', this.props.issue); } render() { const issue = this.props.issue; return (
{issue.id.indexOf('fakeid') < 0 && issue.id.indexOf('error') < 0 && { issue.closed ? {__('action.reopen')} : {__('action.close')} } {__('action.edit')} {__('action.delete')}}> } {issue.id.indexOf('fakeid') >= 0 && {__('global.saving')} } {issue.id.indexOf('error') >= 0 && {__('global.errorsaving')} }
{issue.closed ? {__('Issue.closed')} : {__('Issue.open')}}
{issue.title}
{issue.user.name}{' ' + __('global.wrotein') + ' '}{moment(issue.creationDate).format('lll')}
{issue.unit.name}
{issue.description.split('\n').map((item, i) => {item}
) }
); } } IssueCard.propTypes = { issue: React.PropTypes.object.isRequired, onEditEvent: React.PropTypes.func, onRemoveEvent: React.PropTypes.func, /** * Possible events: openedt, remove, reopen, close */ onIssueEvent: React.PropTypes.func };