1+ /**
2+ * Copyright (c) 2015, Facebook, Inc.
3+ * All rights reserved.
4+ *
5+ * This source code is licensed under the BSD-style license found in the
6+ * LICENSE file in the root directory of this source tree. An additional grant
7+ * of patent rights can be found in the PATENTS file in the same directory.
8+ */
9+
10+ var Comment = require ( './Comment' ) ;
11+ var React = require ( 'react' ) ;
12+
13+ var CommentsBox = React . createClass ( {
14+ propTypes : {
15+ initialComments : React . PropTypes . array . isRequired
16+ } ,
17+ getInitialState ( ) {
18+ return {
19+ comments : this . props . initialComments ,
20+ page : 1 ,
21+ hasMore : true ,
22+ loadingMore : false
23+ } ;
24+ } ,
25+ loadMoreClicked ( evt ) {
26+ var nextPage = this . state . page + 1 ;
27+ this . setState ( {
28+ page : nextPage ,
29+ loadingMore : true
30+ } ) ;
31+
32+ var url = evt . target . href ;
33+ var xhr = new XMLHttpRequest ( ) ;
34+ xhr . open ( 'GET' , url , true ) ;
35+ xhr . onload = ( ) => {
36+ var data = JSON . parse ( xhr . responseText ) ;
37+ this . setState ( {
38+ comments : this . state . comments . concat ( data . comments ) ,
39+ hasMore : data . hasMore ,
40+ loadingMore : false
41+ } ) ;
42+ } ;
43+ xhr . send ( ) ;
44+ return false ;
45+ } ,
46+ render ( ) {
47+ var commentNodes = this . state . comments . map ( comment =>
48+ < Comment author = { comment . Author } > { comment . Text } </ Comment >
49+ ) ;
50+
51+ return (
52+ < div className = "comments" >
53+ < h1 > Comments</ h1 >
54+ < ol className = "commentList" >
55+ { commentNodes }
56+ </ ol >
57+ { this . renderMoreLink ( ) }
58+ </ div >
59+ ) ;
60+ } ,
61+ renderMoreLink ( ) {
62+ if ( this . state . loadingMore ) {
63+ return < em > Loading...</ em > ;
64+ } else if ( this . state . hasMore ) {
65+ return (
66+ < a href = { 'comments/page-' + ( this . state . page + 1 ) } onClick = { this . loadMoreClicked } >
67+ Load More
68+ </ a >
69+ ) ;
70+ } else {
71+ return < em > No more comments</ em > ;
72+ }
73+ }
74+ } ) ;
75+
76+ module . exports = CommentsBox ;
0 commit comments