Skip to content

Commit e51433d

Browse files
committed
More client styling and functionality
1 parent 7dcddea commit e51433d

File tree

5 files changed

+145
-21
lines changed

5 files changed

+145
-21
lines changed

app/src/App.css

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
width: 75%;
55
flex-direction: column;
66
border-radius: 10px;
7+
background-color: #FFF;
8+
}
9+
10+
#init {
11+
display: flex;
12+
align-items: center;
13+
justify-content: center;
714
}
815

916
#top {
@@ -13,14 +20,19 @@
1320
flex-direction: row;
1421
border-radius-top: 10px;
1522
}
16-
#users-main {
23+
#participants-main {
1724
width: 20%;
1825
height: 100%;
26+
height: calc(100% - 20px);
27+
overflow: auto;
28+
padding: 10px;
1929
background-color: #DDD;
2030
}
2131
#conversation-main {
2232
width: 80%;
23-
height: 100%;
33+
height: calc(100% - 20px);
34+
overflow: auto;
35+
padding: 10px;
2436
background-color: #FFF;
2537
}
2638

@@ -38,9 +50,18 @@
3850
width: 100%;
3951
background-color: #CCC;
4052
}
41-
#text-entry-form {
53+
#text-entry {
4254
width: 80%;
4355
height: 100%;
56+
padding: 10px;
57+
}
58+
#text-entry-form {
59+
width: calc(100% - 7px);
60+
height: calc(100% - 28px);
61+
}
62+
#text-entry-input {
63+
width: 100%;
64+
height: 100%;
4465
}
4566
#text-entry-submit {
4667
display: flex;

app/src/App.js

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { Component } from 'react';
22

3+
import Message from './components/Message';
34
import './App.css';
45

56
const socket = new WebSocket("ws://127.0.0.1:8081/ws");
@@ -17,25 +18,61 @@ class App extends Component {
1718
super(props);
1819

1920
this.state = {
21+
init: true,
22+
participants: [],
2023
messages: [],
2124
};
2225

23-
this.buttonClick = this.buttonClick.bind(this);
26+
this.participantSubmit = this.participantSubmit.bind(this);
27+
this.textSubmit = this.textSubmit.bind(this);
2428
}
2529

26-
buttonClick() {
30+
participantSubmit(e) {
31+
e.preventDefault();
32+
const input = document.getElementById("participant-form").value;
33+
console.log(input);
34+
if (input === "") {
35+
return;
36+
}
37+
38+
this.setState({
39+
init: false,
40+
participants: [
41+
...this.state.participants,
42+
input,
43+
]
44+
});
45+
}
46+
47+
textSubmit(e) {
48+
e.preventDefault();
49+
const input = document.getElementById("text-entry-input").value;
50+
if (input === "") {
51+
return;
52+
}
53+
2754
socket.send(JSON.stringify({
28-
text: "test",
55+
text: input,
2956
client: "testClient",
57+
timestamp: new Date().toLocaleTimeString(),
3058
}));
59+
60+
document.getElementById("text-entry-input").value = "";
3161
}
3262

3363
componentDidMount() {
3464
socket.onmessage = (event) => {
35-
const { text, client } = JSON.parse(event.data);
65+
const { text, client, timestamp } = JSON.parse(event.data);
3666

3767
this.setState({
38-
messages: [...this.state.messages, text],
68+
messages: [
69+
...this.state.messages,
70+
{
71+
text,
72+
client,
73+
timestamp,
74+
}
75+
],
3976
});
4077
}
4178
}
@@ -44,23 +81,44 @@ class App extends Component {
4481
console.log(this.state);
4582
return (
4683
<div id="box-main">
47-
<div id="top">
48-
<div id="users-main"></div>
49-
<div id="conversation-main">
50-
{this.state.messages.map(message =>
51-
<div>{message}</div>
52-
)}
84+
{this.state.init &&
85+
<div id="init">
86+
<form onSubmit={this.participantSubmit}>
87+
<input type="text"
88+
placeholder="Enter name here..."
89+
id="participant-form" />
90+
</form>
5391
</div>
54-
</div>
55-
<div id="bottom">
56-
<div id="text-entry-main">
57-
<div id="text-entry-form">
92+
}
93+
{!this.state.init &&
94+
<div style={{width: "100%", height: "100%"}}>
95+
<div id="top">
96+
<div id="participants-main">
97+
<b>{`Participants (${this.state.participants.length}):`}</b>
98+
{this.state.participants.map(participant =>
99+
<div>{participant}</div>
100+
)}
101+
</div>
102+
<div id="conversation-main">
103+
{this.state.messages.map(message =>
104+
<Message message={message} />
105+
)}
106+
</div>
58107
</div>
59-
<div onClick={this.buttonClick} id="text-entry-submit">
60-
Send Message
108+
<div id="bottom">
109+
<div id="text-entry-main">
110+
<div id="text-entry">
111+
<form onSubmit={this.textSubmit} id="text-entry-form">
112+
<input type="text" id="text-entry-input" />
113+
</form>
114+
</div>
115+
<div onClick={this.textSubmit} id="text-entry-submit">
116+
Send Message
117+
</div>
118+
</div>
61119
</div>
62120
</div>
63-
</div>
121+
}
64122
</div>
65123
);
66124
}

app/src/components/Message.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
3+
import '../styles/Message.css';
4+
5+
const Message = (props) => {
6+
console.log(props);
7+
const { text, client, timestamp } = props.message;
8+
9+
return (
10+
<div className="message">
11+
<div className="message-top">
12+
<span className="client-name">{client}</span>
13+
<span className="timestamp">{timestamp}</span>
14+
</div>
15+
<div className="message-text">
16+
{text}
17+
</div>
18+
</div>
19+
);
20+
}
21+
22+
export default Message;

app/src/styles/Message.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.message {
2+
animation: fadein 0.5s;
3+
margin-bottom: 5px;
4+
}
5+
@keyframes fadein {
6+
from { opacity: 0; }
7+
to { opacity: 1; }
8+
}
9+
10+
.message-top {
11+
display: flex;
12+
flex-direction: row;
13+
}
14+
15+
.client-name {
16+
font-weight: bold;
17+
margin-right: 10px;
18+
}
19+
20+
.timestamp {
21+
color: #AAA;
22+
}

server/websocket.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
type JsonData struct {
1010
Text string `json:"text"`
1111
Client string `json:"client"`
12+
Timestamp string `json:"timestamp"`
1213
}
1314

1415
var upgrader = websocket.Upgrader{

0 commit comments

Comments
 (0)