import { h, Component } from 'preact'; import api from 'common/api'; import { url, id } from 'common/settings'; import store from 'common/store'; import Input from 'components/input'; import Thread from 'components/thread'; export default class Root extends Component { constructor(props) { super(props); this.state = { loaded: false, }; this.addComment = this.addComment.bind(this); } componentDidMount() { api.getUser() .then(data => store.set({ user: data })) .catch(() => store.set({ user: {} })) .finally(() => { api.find({ url }) .then(({ comments } = {}) => this.setState({ comments })) .finally(() => this.setState({ loaded: true })); }); } addComment({ text, id, pid }) { const { comments } = this.state; const newComment = { comment: { id, text, user: store.get('user'), time: new Date(), ...(pid ? { pid } : {}), }, }; const newComments = pid ? Root.pasteReply({ comments, newComment }) : [newComment].concat(comments); this.setState({ comments: newComments }); api.getComment({ id }).then(comment => { this.setState({ comments: Root.replaceComment({ comments: newComments, newComment: comment }), }); }); } render({}, { comments = [], user = {}, loaded }) { if (!loaded) { return (