/
CCMessageBox.js
167 lines (148 loc) · 4.51 KB
/
CCMessageBox.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React, { Component } from "react";
import { Row } from "react-bootstrap";
import CCMessage from "./CCMessage";
import { connect } from "react-redux";
import { calculateAvailableHeight,CheckEmpty } from "./../../lib/uiComponentLib";
import * as actionCreator from "./../../store/actions/cc_action";
import { CometChat } from "@cometchat-pro/chat";
var heightCCMessageBox = calculateAvailableHeight(79, 65, "ccMessage");
var ccMessageBoxStyle = {
height: heightCCMessageBox,
overflow: "auto"
};
var ccNewMessageNotifierStyle = {
top: heightCCMessageBox - 10
};
class CCMessageBox extends Component {
constructor(props) {
super(props);
this.state = {
displayStatusNewMessage: false
};
this.refsMessageBox = React.createRef();
}
componentWillMount() {
this.fetchfirstTimeMessage(this.props.activeUser.type,this.props.activeUser.id);
CometChat.addMessageListener(
"CHAT_BOX_LISTENER",
new CometChat.MessageListener({
onTextMessageReceived: message => {
this.markAsRead(message);
},
onMediaMessageReceived: message => {
this.markAsRead(message);
}
})
);
}
componentWillUnmount() {
CometChat.removeMessageListener("CHAT_BOX_LISTENER");
}
markAsRead(msg) {
if (msg.receiverType == "user") {
if ((msg !== undefined && msg.readAt == undefined) && (msg.sender.uid == this.props.activeUser.id)) {
CometChat.markAsRead(msg.id, msg.sender.uid, msg.receiverType);
}
} else {
if ((msg !== undefined && msg.readByMeAt == undefined && msg.sender.uid !== this.props.loggedUid) && (msg.receiver == this.props.activeUser.id)) {
CometChat.markAsRead(msg.id, msg.receiver, msg.receiverType);
}
}
}
fetchfirstTimeMessage(userType, userid) {
try {
this.props.getMessage(userType, userid, 50);
} catch (error) {
console.log(error);
}
}
shouldComponentUpdate(nextProps, nextState) {
if (nextProps.activeUser != this.props.activeUser) {
this.fetchfirstTimeMessage(nextProps.activeUser.type,nextProps.activeUser.id);
}
return true;
}
componentDidUpdate() {
this.scrollToBottom();
}
showUnReadButton = () => {
this.setState({ displayStatusNewMessage: true });
};
hideUnReadButton = () => {
this.setState({ displayStatusNewMessage: false });
};
scrollToBottom() {
var node = document.getElementById("ccMessageBox");
const bottom = node.scrollHeight - node.scrollTop === node.clientHeight;
if (!bottom) {
node.scrollTop = node.scrollHeight;
}
}
render() {
var displayStatus = this.state.displayStatusNewMessage;
let classVar = ["messageBoxNewMessageNotification"];
if (displayStatus) {
classVar.push("displayBlock");
} else {
classVar.push("hideBlock");
}
var messageUser = this.props.messageList.find(e => {
if (e.muid == this.props.activeUser.id) {
return e;
}
});
if (!CheckEmpty(messageUser)) {
return (
<div id="ccMessageBoxContainer" key="15646896846sadasd">
<Row
ref={this.refsMessageBox}
key="ccmessagebox_b"
id="ccMessageBox"
className="ccMessageBox"
style={ccMessageBoxStyle}
></Row>
</div>
);
} else {
return (
<div id="ccMessageBoxContainer" key="15646896846">
<div
ref={this.refsMessageBox}
key="ccmessagebox_d"
id="ccMessageBox"
className="ccMessageBox row"
style={ccMessageBoxStyle}
>
{messageUser.message.map((msg, index) => {
if (index == messageUser.length - 1) {
this.scrollToBottom();
}
return <CCMessage key={msg.id} msgData={msg} />;
})}
</div>
<Row
key="ccunreadMessage"
class={classVar.join(" ")}
style={ccNewMessageNotifierStyle}
onClick={this.scrollToBottom.bind(this)}
>
<span>Recent message ▼</span>
</Row>
</div>
);
}
}
}
const mapStateToProps = store => {
return {
messageList: store.message.messages,
loggedUid: store.users.loggedInUser.uid,
activeUser: store.message.activeMessage
};
};
const mapDispachToProps = dispatch => {
return {
getMessage: (uType, uid, limit) => dispatch(actionCreator.getUserMessageHistory(uType, uid, limit))
};
};
export default connect(mapStateToProps,mapDispachToProps)(CCMessageBox);