Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * This file provided by Facebook is for non-commercial testing and evaluation
  3.  * purposes only. Facebook reserves all rights not expressly granted.
  4.  *
  5.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  6.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  7.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  8.  * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  9.  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  10.  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11.  */
  12.  
  13. var Comment = React.createClass({
  14.   rawMarkup: function() {
  15.     var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
  16.     return { __html: rawMarkup };
  17.   },
  18.  
  19.   render: function() {
  20.     return (
  21.       <div className="comment">
  22.         <h2 className="commentAuthor">
  23.           {this.props.author}
  24.         </h2>
  25.         <span dangerouslySetInnerHTML={this.rawMarkup()} />
  26.         <button onClick={this.props.handleRemoveComment}><u><b>Remove Comment</b></u></button>
  27.       </div>
  28.     );
  29.   }
  30. });
  31.  
  32. var CommentBox = React.createClass({
  33.  
  34.   handleRemoveComment: function() {
  35.     var comments = this.state.data;
  36.     var url = this.props.url + '?' + $.param({"id": this.props.id});
  37.     $.ajax({
  38.       url: url,
  39.       dataType: 'json',
  40.       type: 'DELETE',
  41.       success: function(data) {
  42.         this.setState({data: data});
  43.       }.bind(this),
  44.       error: function(xhr, status, err) {
  45.         this.setState({data: comments});
  46.         console.error(this.props.url, status, err.toString());
  47.       }
  48.     });
  49.   },
  50.  
  51.   loadCommentsFromServer: function() {
  52.     $.ajax({
  53.       url: this.props.url,
  54.       dataType: 'json',
  55.       cache: false,
  56.       success: function(data) {
  57.         this.setState({data: data});
  58.       }.bind(this),
  59.       error: function(xhr, status, err) {
  60.         console.error(this.props.url, status, err.toString());
  61.       }.bind(this)
  62.     });
  63.   },
  64.   handleCommentSubmit: function(comment) {
  65.     var comments = this.state.data;
  66.     // Optimistically set an id on the new comment. It will be replaced by an
  67.     // id generated by the server. In a production application you would likely
  68.     // not use Date.now() for this and would have a more robust system in place.
  69.     comment.id = Date.now();
  70.     var newComments = comments.concat([comment]);
  71.     this.setState({data: newComments});
  72.     $.ajax({
  73.       url: this.props.url,
  74.       dataType: 'json',
  75.       type: 'POST',
  76.       data: comment,
  77.       success: function(data) {
  78.         this.setState({data: data});
  79.       }.bind(this),
  80.       error: function(xhr, status, err) {
  81.         this.setState({data: comments});
  82.         console.error(this.props.url, status, err.toString());
  83.       }.bind(this)
  84.     });
  85.   },
  86.  
  87.   getInitialState: function() {
  88.     return {data: []};
  89.   },
  90.   componentDidMount: function() {
  91.     this.loadCommentsFromServer();
  92.     setInterval(this.loadCommentsFromServer, this.props.pollInterval);
  93.   },
  94.   render: function() {
  95.     return (
  96.       <div className="commentBox">
  97.         <h1>Comments</h1>
  98.         <CommentList data={this.state.data} />
  99.         <CommentForm onCommentSubmit={this.handleCommentSubmit} />
  100.       </div>
  101.     );
  102.   }
  103. });
  104.  
  105. var CommentList = React.createClass({
  106.   render: function() {
  107.     console.log(this.props.data);
  108.     var commentNodes = this.props.data.map(function(comment) {
  109.       return (
  110.         <Comment author={comment.author} id={comment.id} url="/api/comments" key={comment.id}  >
  111.           {comment.text}
  112.         </Comment>
  113.       );
  114.     });
  115.  
  116.     return (
  117.       <div className="commentList">
  118.         {commentNodes}
  119.       </div>
  120.     );
  121.   }
  122. });
  123.  
  124. var CommentForm = React.createClass({
  125.   getInitialState: function() {
  126.     return {author: '', text: ''};
  127.   },
  128.   handleAuthorChange: function(e) {
  129.     this.setState({author: e.target.value});
  130.   },
  131.   handleTextChange: function(e) {
  132.     this.setState({text: e.target.value});
  133.   },
  134.   handleSubmit: function(e) {
  135.     e.preventDefault();
  136.     var author = this.state.author.trim();
  137.     var text = this.state.text.trim();
  138.     if (!text || !author) {
  139.       return;
  140.     }
  141.     this.props.onCommentSubmit({author: author, text: text});
  142.     this.setState({author: '', text: ''});
  143.   },
  144.   render: function() {
  145.     return (
  146.       <form className="commentForm" onSubmit={this.handleSubmit}>
  147.         <input
  148.           type="text"
  149.           placeholder="Your name"
  150.           value={this.state.author}
  151.           onChange={this.handleAuthorChange}
  152.         />
  153.         <input
  154.           type="text"
  155.           placeholder="Say something..."
  156.           value={this.state.text}
  157.           onChange={this.handleTextChange}
  158.         />
  159.         <input type="submit" value="Post" />
  160.       </form>
  161.     );
  162.   }
  163. });
  164.  
  165. ReactDOM.render(
  166.   <CommentBox url="/api/comments" pollInterval={2000} />,
  167.   document.getElementById('content')
  168. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement