2022-01-13 19:22:54 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { w3cwebsocket as W3CWebSocket } from "websocket";
|
2022-01-20 20:50:25 +00:00
|
|
|
import {Button, TextField, Grid, Container, Card, CardHeader, Paper, Avatar, FormHelperText} from "@mui/material";
|
2022-01-13 19:22:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
export default class Chat extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
messages: [],
|
|
|
|
value:'',
|
|
|
|
};
|
|
|
|
|
2022-01-14 00:43:26 +00:00
|
|
|
client = new W3CWebSocket('ws://' + window.location.host + '/ws/chat/' + this.props.orderId + '/');
|
2022-01-13 19:22:54 +00:00
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.client.onopen = () => {
|
|
|
|
console.log('WebSocket Client Connected')
|
|
|
|
}
|
|
|
|
this.client.onmessage = (message) => {
|
|
|
|
const dataFromServer = JSON.parse(message.data);
|
|
|
|
console.log('Got reply!', dataFromServer.type);
|
|
|
|
if (dataFromServer){
|
|
|
|
this.setState((state) =>
|
|
|
|
({
|
|
|
|
messages: [...state.messages,
|
|
|
|
{
|
|
|
|
msg: dataFromServer.message,
|
|
|
|
userNick: dataFromServer.user_nick,
|
|
|
|
}],
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 00:43:26 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
this.scrollToBottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollToBottom = () => {
|
|
|
|
this.messagesEnd.scrollIntoView({ behavior: "smooth" });
|
|
|
|
}
|
|
|
|
|
2022-01-13 19:22:54 +00:00
|
|
|
onButtonClicked = (e) => {
|
|
|
|
this.client.send(JSON.stringify({
|
|
|
|
type: "message",
|
|
|
|
message: this.state.value,
|
2022-01-22 23:05:03 +00:00
|
|
|
nick: this.props.ur_nick,
|
2022-01-13 19:22:54 +00:00
|
|
|
}));
|
|
|
|
this.state.value = ''
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<Container component="main" maxWidth="xs">
|
|
|
|
<Paper style={{ height: 300, maxHeight: 300, overflow: 'auto', boxShadow: 'none', }}>
|
|
|
|
{this.state.messages.map(message => <>
|
2022-01-13 20:41:48 +00:00
|
|
|
<Card elevation={5} align="left" >
|
|
|
|
{/* If message sender is not our nick, gray color, if it is our nick, green color */}
|
2022-01-22 23:05:03 +00:00
|
|
|
{message.userNick == this.props.ur_nick ?
|
2022-01-13 19:22:54 +00:00
|
|
|
<CardHeader
|
2022-01-13 20:41:48 +00:00
|
|
|
avatar={
|
|
|
|
<Avatar
|
|
|
|
alt={message.userNick}
|
|
|
|
src={window.location.origin +'/static/assets/avatars/' + message.userNick + '.png'}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
style={{backgroundColor: '#e8ffe6'}}
|
|
|
|
title={message.userNick}
|
|
|
|
subheader={message.msg}
|
|
|
|
/>
|
|
|
|
:
|
|
|
|
<CardHeader
|
|
|
|
avatar={
|
|
|
|
<Avatar
|
|
|
|
alt={message.userNick}
|
|
|
|
src={window.location.origin +'/static/assets/avatars/' + message.userNick + '.png'}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
style={{backgroundColor: '#fcfcfc'}}
|
|
|
|
title={message.userNick}
|
|
|
|
subheader={message.msg}
|
|
|
|
/>}
|
2022-01-13 19:22:54 +00:00
|
|
|
</Card>
|
|
|
|
</>)}
|
2022-01-14 00:43:26 +00:00
|
|
|
<div style={{ float:"left", clear: "both" }} ref={(el) => { this.messagesEnd = el; }}></div>
|
2022-01-13 19:22:54 +00:00
|
|
|
</Paper>
|
|
|
|
<form noValidate onSubmit={this.onButtonClicked}>
|
|
|
|
<Grid containter alignItems="stretch" style={{ display: "flex" }}>
|
|
|
|
<Grid item alignItems="stretch" style={{ display: "flex" }}>
|
|
|
|
<TextField
|
|
|
|
label="Type a message"
|
|
|
|
variant="outlined"
|
|
|
|
size="small"
|
|
|
|
value={this.state.value}
|
|
|
|
onChange={e => {
|
|
|
|
this.setState({ value: e.target.value });
|
|
|
|
this.value = this.state.value;
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Grid>
|
|
|
|
<Grid item alignItems="stretch" style={{ display: "flex" }}>
|
|
|
|
<Button type="submit" variant="contained" color="primary" > Send </Button>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
</form>
|
2022-01-27 22:51:57 +00:00
|
|
|
<FormHelperText>This chat has no memory. If you leave and come back the messages are lost.</FormHelperText>
|
2022-01-13 19:22:54 +00:00
|
|
|
</Container>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|