forked from TwilioDevEd/conversations-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversation.js
159 lines (143 loc) · 5.61 KB
/
Conversation.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
import React, { Component } from 'react';
import './assets/Conversation.css';
import MessageBubble from './MessageBubble'
import Dropzone from 'react-dropzone';
import styles from './assets/Conversation.module.css'
import {Button, Form, Icon, Input} from "antd";
import ConversationsMessages from "./ConversationsMessages";
import PropTypes from "prop-types";
class Conversation extends Component {
constructor(props) {
super(props);
this.state = {
newMessage: '',
conversationProxy: props.conversationProxy,
messages: [],
loadingState: 'initializing',
boundConversations: new Set()
};
}
loadMessagesFor = (thisConversation) => {
if (this.state.conversationProxy === thisConversation) {
thisConversation.getMessages()
.then(messagePaginator => {
if (this.state.conversationProxy === thisConversation) {
this.setState({ messages: messagePaginator.items, loadingState: 'ready' });
}
})
.catch(err => {
console.error("Couldn't fetch messages IMPLEMENT RETRY", err);
this.setState({ loadingState: "failed" });
});
}
};
componentDidMount = () => {
if (this.state.conversationProxy) {
this.loadMessagesFor(this.state.conversationProxy);
if (!this.state.boundConversations.has(this.state.conversationProxy)) {
let newConversation = this.state.conversationProxy;
newConversation.on('messageAdded', m => this.messageAdded(m, newConversation));
this.setState({boundConversations: new Set([...this.state.boundConversations, newConversation])});
}
}
}
componentDidUpdate = (oldProps, oldState) => {
if (this.state.conversationProxy !== oldState.conversationProxy) {
this.loadMessagesFor(this.state.conversationProxy);
if (!this.state.boundConversations.has(this.state.conversationProxy)) {
let newConversation = this.state.conversationProxy;
newConversation.on('messageAdded', m => this.messageAdded(m, newConversation));
this.setState({boundConversations: new Set([...this.state.boundConversations, newConversation])});
}
}
};
static getDerivedStateFromProps(newProps, oldState) {
let logic = (oldState.loadingState === 'initializing') || oldState.conversationProxy !== newProps.conversationProxy;
if (logic) {
return { loadingState: 'loading messages', conversationProxy: newProps.conversationProxy };
} else {
return null;
}
}
messageAdded = (message, targetConversation) => {
if (targetConversation === this.state.conversationProxy)
this.setState((prevState, props) => ({
messages: [...prevState.messages, message]
}));
};
onMessageChanged = event => {
this.setState({ newMessage: event.target.value });
};
sendMessage = event => {
event.preventDefault();
const message = this.state.newMessage;
this.setState({ newMessage: '' });
this.state.conversationProxy.sendMessage(message);
};
onDrop = acceptedFiles => {
this.state.conversationProxy.sendMessage({contentType: acceptedFiles[0].type, media: acceptedFiles[0]});
};
render = () => {
return (
<Dropzone
onDrop={this.onDrop}
accept="image/*">
{({getRootProps, getInputProps, isDragActive}) => (
<div
{...getRootProps()}
onClick={() => {
}}
id="OpenChannel"
style={{position: "relative", top: 0}}>
{isDragActive &&
<div className={styles.drop}>
<Icon type={"cloud-upload"}
style={{fontSize: "5em", color: "#fefefe"}}/>
<h3 style={{color: "#fefefe"}}>Release to Upload</h3>
</div>
}
<div
className={styles.messages}
style={{
filter: `blur(${isDragActive ? 4 : 0}px)`,
}}
>
<input id="files" {...getInputProps()} />
<div style={{flexBasis: "100%", flexGrow: 2, flexShrink: 1, overflowY: "scroll"}}>
<ConversationsMessages
identity={this.props.myIdentity}
messages={this.state.messages}/>
</div>
<div>
<Form onSubmit={this.sendMessage}>
<Input.Group compact={true} style={{
width: "100%",
display: "flex",
flexDirection: "row"
}}>
<Input
style={{flexBasis: "100%"}}
placeholder={"Type your message here..."}
type={"text"}
name={"message"}
id={styles['type-a-message']}
autoComplete={"off"}
disabled={this.state.loadingState !== 'ready'}
onChange={this.onMessageChanged}
value={this.state.newMessage}
/>
<Button icon="enter" htmlType="submit" type={"submit"}/>
</Input.Group>
</Form>
</div>
</div>
</div>
)}
</Dropzone>
);
}
}
Conversation.propTypes = {
myIdentity: PropTypes.string.isRequired
};
export default Conversation;