-
Notifications
You must be signed in to change notification settings - Fork 3
/
VideoChat.js
167 lines (147 loc) · 4.97 KB
/
VideoChat.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 from 'react'
import CssBaseline from '@material-ui/core/CssBaseline';
import Typography from '@material-ui/core/Typography';
import Grid from '@material-ui/core/Grid';
import { withStyles } from '@material-ui/core/styles';
import SendIcon from '@material-ui/icons/Send';
import IconButton from '@material-ui/core/IconButton';
import {Card, CardActions, CardContent,List, ListItem, Input} from '@material-ui/core';
import { Auth, API, graphqlOperation } from 'aws-amplify';
import { createMessage } from '../graphql/mutations';
import { onCreateMessage } from '../graphql/subscriptions';
import { listMessagesByChannel } from '../graphql/queries';
import Chip from '@material-ui/core/Chip';
import VideoPlayer from './VideoPlayer';
const useStyles = theme => ({
root: {
flexGrow: 1,
},
paper: {
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.secondary,
},
item: {
padding: theme.spacing(1,0,0,0)
},
top: {
textAlign: 'left',
padding: theme.spacing(0,0,1,0)
}
});
class VideoChat extends React.Component {
constructor(props){
super(props);
this.state = {
tmpMessage: "",
messages: [],
username: "",
channelId: props.channelId,
videoJsOptions: {
autoplay: true,
controls: true,
sources: [
{
src: props.channelUrl,
type: 'application/x-mpegURL',
},
],
}
};
}
scrollToBottom = () => {
const chat = document.getElementById("chatMessages");
chat.scrollTop = chat.scrollHeight;
};
async componentDidMount() {
const data = await Auth.currentAuthenticatedUser();
this.setState({ username: data.username });
const result = await API.graphql(graphqlOperation(listMessagesByChannel, { channelId: this.state.channelId, sortDirection:'ASC' } ));
this.setState({ messages: result.data.listMessagesByChannel.items });
this.createMessageListener = API.graphql(graphqlOperation(onCreateMessage, { channelId: this.state.channelId })).subscribe({
next: messageData => {
const newMessage = messageData.value.data.onCreateMessage;
if (newMessage.username!==this.state.username){
this.setState({ messages: [...this.state.messages, newMessage ] })
this.scrollToBottom();
}
}
});
this.scrollToBottom();
}
componentWillUnmount() {
console.log("Removed.....");
this.createMessageListener.unsubscribe();
}
publishMessage = async event => {
const input = {
channelId: this.state.channelId,
username: this.state.username,
content: this.state.tmpMessage,
}
this.setState({ messages: [...this.state.messages, input ] })
this.setState({ tmpMessage: "" })
await API.graphql(graphqlOperation(createMessage, { input } ));
this.scrollToBottom();
};
handleKeyDown = (event) => {
if(event.target.id === "messageInput"){
if (event.key === 'Enter') {
this.publishMessage();
}
}
};
handleChangeInput = (event) => {
this.setState({ tmpMessage: event.target.value })
};
render() {
const { classes } = this.props;
return (
<div className="App">
<CssBaseline />
<Grid container spacing={3}>
<Grid item sm={12} md={8}>
<div data-vjs-player>
<VideoPlayer {...this.state.videoJsOptions} />
</div>
</Grid>
<Grid item sm={12} md={4}>
<Card >
<CardContent>
<div className={classes.top}>
<Typography variant="h5" color="primary" inline >Chat</Typography>
</div>
<List id="chatMessages" style={{height: 320, overflow: 'auto'}} >
{ this.state.messages.map((item, index)=>(
<ListItem className={classes.item} key={index}>
<Chip label={ item.username+" : "+item.content} color={ this.state.username===item.username ? "secondary" : "primary" } />
</ListItem>
))}
</List>
</CardContent>
<CardActions>
<Input
placeholder="Enter a message"
fullWidth={true}
id="messageInput"
value={this.state.tmpMessage}
onChange={this.handleChangeInput}
onKeyDown={this.handleKeyDown}
inputProps={{'aria-label': 'Message Field',}}
autoFocus={true}
/>
<IconButton
color="primary"
onClick={this.publishMessage}
>
<SendIcon />
</IconButton>
</CardActions>
</Card>
</Grid>
</Grid>
</div>
);
}
}
export default withStyles(useStyles)(VideoChat);