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 (
正在加载。。。
); } return (

{topic.title}

{topic.author.nickname} 发表于 {topic.createdAt}

标签:{topic.tags.join(', ')}

{!topic.permission.edit ? null : 编辑 }    {!topic.permission.delete ? null : }


{ addComment(this.state.topic._id, comment.content) .then(comment => { done(); this.refresh(); }) .catch(err => { done(); alert(err); }); }} />
); } }