import React from "react"; import marked from "marked"; export class CommentForm extends React.Component { constructor() { this.handleSubmit = this.handleSubmit.bind(this); } handleSubmit(ev) { ev.preventDefault(); let author = this.refs.author.getDOMNode().value.trim(); let text = this.refs.text.getDOMNode().value.trim(); if (!text || !author) { return; } this.props.onCommentSubmit({author, text}); this.refs.author.getDOMNode().value = ''; this.refs.text.getDOMNode().value = ''; } render() { return (
); } } export class CommentList extends React.Component { render() { let commentNodes = this.props.data.map((comment) => { return ( {comment.text} ); }); return (
{commentNodes}
); } } export class CommentBox extends React.Component { constructor(props) { super(props); if (props.initialData) { this.state = {data: props.initialData} } else { this.state = {data: []} } this.handleCommentSubmit = this.handleCommentSubmit.bind(this); } handleCommentSubmit(comment) { let comments = this.state.data; let newComments = comments.concat([comment]); this.setState({data: newComments}); // TODO: Send to server } componentDidMount() { let xhr = new XMLHttpRequest(); xhr.open('GET', this.props.url); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { this.setState({data: JSON.parse(xhr.responseText)}); } }.bind(this); xhr.onerror = function(err) { console.error(this.props.url, xhr.status, err); }.bind(this); xhr.send(); } render() { return (

Comments

); } } export class Comment extends React.Component { render() { let rawMarkup = marked(this.props.children.toString()); return (

{this.props.author}

); } }