forked from yowiputra/BuddyUp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopupChat.jsx
69 lines (60 loc) · 1.99 KB
/
PopupChat.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { Component } from 'react';
//Used react-popupbox because of ease of integration. However, if given more time, would have switched it to the official reactjs 'react-modal' package that has the same functionality https://reactcommunity.org/react-modal/
import { connect } from 'react-redux';
import io from 'socket.io-client';
class PopupChat extends Component {
constructor(props) {
super(props);
this.state = {
currentUser: this.props.auth.user.username,
input: '',
showHiddenChat: false
}
this.onChange = this.onChange.bind(this);
}
toggleHiddenChat = () => {
this.setState({ showHiddenChat: !this.state.showHiddenChat })
}
componentDidMount() {
console.log("PopupChat component mounted");
}
onChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
render() {
const messageList = this.props.messages.map((message) => {
return (<div key={message.id} className="message">
<span >{message.username}: {message.content}</span>
</div>);
})
const className = this.state.showHiddenChat ? "messagelist shown" : "messagelist bottom";
return (
<div className={className}>
<p className="chatheaderClicked" onClick={ this.toggleHiddenChat }>Chat</p>
<div className="message">{ messageList }</div>
<input
name="input"
id="chatbar"
placeholder="Type a message and hit ENTER"
onChange={ this.onChange }
onKeyDown={ (event) => {
if (event.key === 'Enter') {
this.props.newPost(this.state.input);
const element = document.getElementsByClassName('message');
element.scrollIntoView(false);
}
}}
/>
</div>
)
}
}
PopupChat.PropTypes = {
auth: React.PropTypes.object.isRequired,
}
function mapStateToProps(state) {
return {
auth: state.auth
};
}
export default connect(mapStateToProps)(PopupChat);