import React from 'react';
import { Link } from 'react-router-dom';
import moment from 'moment';
import Message from './Message';
import MessageInput from './MessageInput';
import Spinner from './Spinner';
import Avatar from './Avatar';
import Icon from './Icon';
import Button from './Button';
import { format } from '../utils/embed';
import { getMessages, comment, markReadTracker } from '../api';
export default class Thread extends React.Component {
constructor(props) {
super(props);
const { msg } = (this.props.location.state || {});
this.state = {
msg: msg || {},
replies: [],
active: 0
};
}
componentDidMount() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
this.loadReplies();
}
loadReplies() {
this.setState({ replies: [] })
const { mid } = this.props.match.params;
let params = {
mid: mid
}
if (this.props.visitor && this.props.visitor.hash) {
params.hash = this.props.visitor.hash
};
getMessages('/thread', params)
.then(response => {
let msg = response.data.shift();
this.setState({
msg: msg,
replies: response.data,
active: 0
})
}
).catch(ex => {
console.log(ex);
});
}
loaded = () => {
return (this.state.replies && this.state.replies.length > 0) || ('mid' in this.state.msg && !('replies' in this.state.msg));
}
setActive(msg, event) {
this.setState({
active: msg.rid || 0
})
}
postComment = (template) => {
const { mid, rid, body, attach } = template;
comment(mid, rid, body, attach).then(res => {
this.loadReplies()
})
.catch(console.log)
}
render() {
const msg = this.state.msg;
return (
= 0) }}>
{
{msg.replies && `Replies (${msg.replies})`}
{
this.state.replies.map((msg) => (
) : (