File size: 3,253 Bytes
e1d589b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React from 'react';
import {Link} from 'react-router';
import 'highlight.js/styles/github-gist.css';
import {getTopicDetail, addComment, deleteComment,deleteTopic} from '../lib/client';
import {renderMarkdown,redirectURL} from '../lib/utils';
import CommentEditor from './CommentEditor';

export default class TopicDetail extends React.Component{

  constructor(props){
    super(props);
    this.state={};
  }

  componentDidMount() {
    this.refresh();
  }

  refresh() {
    getTopicDetail(this.props.params.id)
      .then(topic => {
        topic.html = renderMarkdown(topic.content);
        if (topic.comments) {
          for (const item of topic.comments) {
            item.html = renderMarkdown(item.content);
          }
        }
        this.setState({topic});
      })
      .catch(err => console.error(err));
  }

  handleDeleteComment(cid) {
    if (!confirm('是否删除评论?')) return;
    deleteComment(this.state.topic._id, cid)
      .then(comment => {
        this.refresh();
      })
      .catch(err => {
        alert(err);
      });
  }

  handleDeleteTopic() {
  if (!confirm('是否删除主题?')) return;
  deleteTopic(this.state.topic._id)
    .then(() => {
      redirectURL('/');
    })
    .catch(err => {
      alert(err);
    });
}

  render () {
    const topic=this.state.topic;

    if (!topic){
      return (
        <div>正在加载。。。</div>
      );
    }
    return (
      <div>
       <h2>{topic.title}</h2>
       <p>{topic.author.nickname} 发表于 {topic.createdAt}</p>
       <p>标签:{topic.tags.join(', ')}</p>
       <p>
          {!topic.permission.edit ? null :
            <Link to={`/topic/${topic._id}/edit`} className="btn btn-xs btn-primary">
              <i className="glyphicon glyphicon-edit"></i> 编辑
            </Link>
          }
          &nbsp;&nbsp;
          {!topic.permission.delete ? null :
          <button className="btn btn-xs btn-danger" onClick={this.handleDeleteTopic.bind(this)}>
            <i className="glyphicon glyphicon-trash"></i> 删除
          </button>
          }
      </p>
       <br/>
       <section dangerouslySetInnerHTML={{__html: topic.html}}></section>
       <CommentEditor
         title="发表评论"
         onSave={(comment, done) => {
           addComment(this.state.topic._id, comment.content)
             .then(comment => {
               done();
               this.refresh();
             })
             .catch(err => {
               done();
               alert(err);
             });
         }}
       />
       <ul className="list-group">
         {topic.comments.map((item,i)=>{
           return (
             <li className="list-group-item" key={i}>
             <span className="pull-right">
              {!item.permission.delete ? null :
               <button className="btn btn-xs btn-danger" onClick={this.handleDeleteComment.bind(this, item._id)}>
                 <i className="glyphicon glyphicon-trash"></i>
               </button>
              }
             </span>
               {item.author.nickname} 于{item.createAt}说:
               <p dangerouslySetInnerHTML={{__html: item.html}}></p>
             </li>
           )
         })}
       </ul>
      </div>
    );
  }
}