@@ -4,6 +4,8 @@ import React, { PropTypes } from 'react';
4
4
import CommentForm from './CommentForm/CommentForm' ;
5
5
import CommentList , { CommentPropTypes } from './CommentList/CommentList' ;
6
6
import css from './CommentBox.scss' ;
7
+ import Immutable from 'immutable' ;
8
+ import ActionCable from 'actioncable' ;
7
9
8
10
export default class CommentBox extends BaseComponent {
9
11
static propTypes = {
@@ -19,14 +21,43 @@ export default class CommentBox extends BaseComponent {
19
21
} ) . isRequired ,
20
22
} ;
21
23
24
+ constructor ( ) {
25
+ super ( ) ;
26
+ _ . bindAll ( this , [
27
+ 'refreshComments' ,
28
+ ] ) ;
29
+ }
30
+
31
+ subscribeChannel ( ) {
32
+ const { messageReceived } = this . props . actions ;
33
+ const protocol = window . location . protocol === "https:" ? "wss://" : "ws://"
34
+ const cable = ActionCable . createConsumer ( protocol + window . location . hostname + ":" + window . location . port + "/cable" ) ;
35
+ cable . subscriptions . create ( { channel : "CommentsChannel" } , {
36
+ connected : ( ) => {
37
+ console . log ( "connected" )
38
+ } ,
39
+ disconnected : ( ) => {
40
+ console . log ( "disconnected" )
41
+ } ,
42
+ received : ( comment ) => {
43
+ messageReceived ( Immutable . fromJS ( comment ) ) ;
44
+ }
45
+ } ) ;
46
+ }
47
+
22
48
componentDidMount ( ) {
23
49
const { fetchComments } = this . props . actions ;
24
50
fetchComments ( ) ;
25
- this . intervalId = setInterval ( fetchComments , this . props . pollInterval ) ;
51
+ this . subscribeChannel ( ) ;
26
52
}
27
53
28
54
componentWillUnmount ( ) {
29
- clearInterval ( this . intervalId ) ;
55
+ App . cable . subscriptions . remove ( { channel : "CommentsChannel" } ) ;
56
+ }
57
+
58
+ refreshComments ( ) {
59
+ const { fetchComments } = this . props . actions ;
60
+ fetchComments ( ) ;
30
61
}
31
62
32
63
render ( ) {
@@ -43,6 +74,7 @@ export default class CommentBox extends BaseComponent {
43
74
< h2 >
44
75
Comments { data . get ( 'isFetching' ) && 'Loading...' }
45
76
</ h2 >
77
+ < a href = "javascript:void(0)" onClick = { this . refreshComments } > Refresh</ a >
46
78
< p >
47
79
< b > Text</ b > supports Github Flavored Markdown.
48
80
Comments older than 24 hours are deleted.< br />
0 commit comments