Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CommentBox extends React.Component {
- constructor(props) {
- super(props);
- this.state = { data: this.props.initialData };
- this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
- }
- loadCommentsFromServer() {
- const xhr = new XMLHttpRequest();
- xhr.open('get', this.props.url, true);
- xhr.onload = () => {
- const data = JSON.parse(xhr.responseText);
- this.setState({ data: data });
- };
- xhr.send();
- }
- handleCommentSubmit(comment) {
- const comments = this.state.data;
- // Optimistically set an id on the new comment. It will be replaced by an
- // id generated by the server. In a production application you would likely
- // use a more robust system for ID generation.
- comment.Id = comments.length + 1;
- const newComments = comments.concat([comment]);
- this.setState({ data: newComments });
- const data = new FormData();
- data.append('author', comment.author);
- data.append('text', comment.text);
- const xhr = new XMLHttpRequest();
- xhr.open('post', this.props.submitUrl, true);
- xhr.onload = () => this.loadCommentsFromServer();
- xhr.send(data);
- }
- componentDidMount() {
- window.setInterval(() => this.loadCommentsFromServer(), this.props.pollInterval);
- }
- render() {
- return (
- <div className="commentBox">
- <h1>Comments Data</h1>
- <CommentList data={this.state.data} />
- <CommentForm onCommentSubmit={this.handleCommentSubmit} />
- </div>
- );
- }
- }
- class CommentList extends React.Component {
- render() {
- const commentNodes = this.props.data.map(comment => (
- <Comment author={comment.author} key={comment.Id}>
- {comment.text}
- </Comment>
- ));
- return (
- <div className="commentList">
- {commentNodes}
- </div>
- );
- }
- }
- class Comment extends React.Component {
- render() {
- return (
- <div className="comment">
- <h2 className="commentAuthor">
- {this.props.author}
- </h2>
- {this.props.children}
- </div>
- );
- }
- }
- class CommentForm extends React.Component {
- constructor(props) {
- super(props);
- this.state = { author: '', text: '' };
- this.handleAuthorChange = this.handleAuthorChange.bind(this);
- this.handleTextChange = this.handleTextChange.bind(this);
- this.handleSubmit = this.handleSubmit.bind(this);
- }
- handleAuthorChange(e) {
- this.setState({ author: e.target.value });
- }
- handleTextChange(e) {
- this.setState({ text: e.target.value });
- }
- handleSubmit(e) {
- e.preventDefault();
- const author = this.state.author.trim();
- const text = this.state.text.trim();
- if (!text || !author) {
- return;
- }
- this.props.onCommentSubmit({ Author: author, Text: text });
- this.setState({ author: '', text: '' });
- }
- render() {
- return (
- <form className="commentForm" onSubmit={this.handleSubmit}>
- <input
- type="text"
- placeholder="Your name"
- value={this.state.author}
- onChange={this.handleAuthorChange}
- />
- <input
- type="text"
- placeholder="Say something..."
- value={this.state.text}
- onChange={this.handleTextChange}
- />
- <input type="submit" value="Post" />
- </form>
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement