Skip to content

Commit 6738d8e

Browse files
author
Dave Newman
committed
Fix commenting issues
1 parent cab448f commit 6738d8e

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

app/assets/javascripts/components/Chat.es6.jsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Chat extends React.Component {
55
super(props)
66
this.state = {
77
moreComments: true,
8-
comments: props.comments
8+
comments: props.comments,
99
}
1010
}
1111

@@ -37,7 +37,7 @@ class Chat extends React.Component {
3737
}
3838

3939
renderChatInput() {
40-
if (this.props.signedIn) {
40+
if (this.props.signedIn && this.pusher) {
4141
return (
4242
<form onSubmit={this.handleSubmit.bind(this)}>
4343
<input type="text" ref="body" defaultValue="" placeholder="Ask question" className="col-9 focus-no-border font-sm resize-chat-on-change m0" style={{"border": "none", "outline": "none"}} />
@@ -71,7 +71,7 @@ class Chat extends React.Component {
7171
method: 'POST',
7272
dataType: 'json',
7373
data: {
74-
socket_id: pusher.connection.socket_id,
74+
socket_id: this.pusher.connection.socket_id,
7575
comment: {
7676
article_id: this.props.stream.id,
7777
body: this.refs.body.value,
@@ -122,8 +122,13 @@ class Chat extends React.Component {
122122
})
123123
}
124124

125+
componentWillMount() {
126+
this.pusher = new Pusher(this.props.pusherKey)
127+
this.channel = this.pusher.subscribe(this.props.chatChannel)
128+
}
129+
125130
componentDidMount() {
126-
window.channel.bind('new-comment', comment => {
131+
this.channel.bind('new-comment', comment => {
127132
this.setState({comments: [...this.state.comments, comment]})
128133
})
129134

@@ -177,8 +182,10 @@ class Chat extends React.Component {
177182
}
178183

179184
Chat.propTypes = {
180-
stream: React.PropTypes.object.isRequired,
185+
chatChannel: React.PropTypes.string.isRequired,
181186
comments: React.PropTypes.array.isRequired,
182-
signedIn: React.PropTypes.bool,
183187
isLive: React.PropTypes.bool,
188+
pusherKey: React.PropTypes.string.isRequired,
189+
signedIn: React.PropTypes.bool,
190+
stream: React.PropTypes.object.isRequired,
184191
}

app/controllers/comments_controller.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ class CommentsController < ApplicationController
22
before_action :require_login, only: [:create, :destroy]
33

44
def index
5-
return head(:forbidden) unless admin?
65
respond_to do |format|
7-
format.html { @comments = Comment.order(created_at: :desc).page(params[:page]) }
6+
format.html {
7+
# TODO: do we need this check?
8+
return head(:forbidden) unless admin?
9+
@comments = Comment.order(created_at: :desc).page(params[:page])
10+
}
811
format.json {
912
@comments = Comment.
1013
where(article_id: params[:article_id]).

app/views/streams/_chat.html.erb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
<script src="https://js.pusher.com/2.2/pusher.min.js"></script>
22
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js"></script>
3-
<script>
4-
window.pusher = new Pusher('<%= Pusher.key %>');
5-
window.channel = pusher.subscribe('<%= @stream.dom_id %>');
6-
</script>

app/views/streams/show.json.jbuilder

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ if current_user
33
json.authorUsername current_user.username
44
end
55
json.signedIn !!current_user
6+
json.pusherKey ENV['PUSHER_KEY']
7+
json.chatChannel @stream.dom_id
68

79
json.stream do
810
json.extract! @stream, :id

0 commit comments

Comments
 (0)