|
1 | 1 | import React, { useState } from "react"; |
2 | 2 | import { over } from "stompjs"; |
3 | 3 | import SockJS from "sockjs-client"; |
| 4 | +import "./index.css" |
4 | 5 |
|
5 | 6 | var stomClient = null; |
6 | | -export default function Chat() { |
| 7 | +export function Chat() { |
7 | 8 | const [clientData, setClientData] = useState({ |
8 | 9 | name:"", |
9 | 10 | receiver:"", |
10 | 11 | connected: false, |
11 | 12 | message:"", |
12 | 13 | }); |
13 | 14 | const [publicRoom, setPublicRoom] = useState([]); |
14 | | - const [privateRoom, setPrivateRoom] = useState(new Map()); |
| 15 | + const [privateRoom, setPrivateRoom] = useState(new Map()); |
15 | 16 |
|
16 | 17 | function handleValue(e) { |
17 | 18 | const {value, name} = e.target |
@@ -45,12 +46,12 @@ export default function Chat() { |
45 | 46 | function onConnected() { |
46 | 47 | setClientData({...clientData, "connected":true}); |
47 | 48 | stomClient.subscribe("/chat/public", onPublic); |
48 | | - stomClient.subscribe("/user/"+clientData.name+"/private", onPrivate); |
49 | 49 | clientConnected(); |
50 | 50 | } |
51 | 51 | function clientConnected() { |
52 | 52 | let client = { |
53 | 53 | author: clientData.name, |
| 54 | + message: "connected", |
54 | 55 | status: "JOIN", |
55 | 56 | }; |
56 | 57 | stomClient.send("/app/message", {}, JSON.stringify(client)); |
@@ -85,66 +86,43 @@ export default function Chat() { |
85 | 86 | } |
86 | 87 | } |
87 | 88 |
|
88 | | - // Essa função pode ser usada para mandar mensagem privada |
89 | | - // function sendMessagePrivate() { |
90 | | - // if(stomClient) { |
91 | | - // let message = { |
92 | | - // name: clientData.name, |
93 | | - // receiver: room, |
94 | | - // message: clientData.message, |
95 | | - // status: "MESSAGE", |
96 | | - // }; |
97 | | - // if(clientData.name !== room) { |
98 | | - // privateRoom.set(room).push(message); |
99 | | - // setPrivateRoom(new Map(privateRoom)); |
100 | | - // } |
101 | | - // stomClient.send("/app/message", {}, JSON.stringify(message)); |
102 | | - // setClientData({...clientData, "message":""}); |
103 | | - // } |
104 | | - // } |
105 | | - |
106 | | - function onPrivate(payload) { |
107 | | - let payloadData = JSON.parse(payload); |
108 | | - if(privateRoom.get(payloadData.name)) { |
109 | | - let data = []; |
110 | | - data.push(payloadData); |
111 | | - privateRoom.set(payloadData.name, data); |
112 | | - setPrivateRoom(new Map(privateRoom)); |
113 | | - } |
114 | | - } |
115 | | - |
116 | 89 | return( |
117 | 90 | <div> |
118 | | - <h1>ChatgpD</h1> |
119 | | - <div className="container"> |
| 91 | + <h1>ChatgpD</h1> |
| 92 | + <div className="container "> |
120 | 93 | {clientData.connected? |
| 94 | + <> |
121 | 95 | <div> |
122 | 96 | <div className="chat-content"> |
123 | | - <ul className="chat-messages list-group list-group-flush"> |
124 | | - {publicRoom.map((chat, index) => ( |
125 | | - <> |
126 | | - { |
127 | | - chat.message !== null && |
128 | | - <li key={index} className={`message list-group-item`}> |
| 97 | + <div className="box rounded mt-5"> |
| 98 | + <ul className="chat-messages list-group list-group-flush"> |
| 99 | + |
| 100 | + {publicRoom.map((chat, index) => ( |
| 101 | + <> |
| 102 | + {chat.message === "connected" && chat.author !== clientData.name ? <small key={index} className="list-group-item text-secondary d-flex justify-content-center"> User ({chat.author}) has joined the Server!</small> : ''} |
129 | 103 | { |
130 | | - chat.message !== null && chat.author !== clientData.name ? <div className="message-data">{chat.author}: {chat.message}</div> |
131 | | - :<div className="message-data">You: {chat.message}</div> |
| 104 | + chat.message !== null && chat.message !== "connected" && |
| 105 | + <li key={index} className={`message list-group-item`}> |
| 106 | + { |
| 107 | + chat.author !== clientData.name? <div className="message-data">{chat.author}: {chat.message}</div> |
| 108 | + :<div className="message-data">You: {chat.message}</div> |
| 109 | + } |
| 110 | + </li> |
132 | 111 | } |
133 | | - </li> |
134 | | - } |
135 | | - </> |
136 | | - ))} |
137 | | - </ul> |
138 | | - |
139 | | - <div className="send-message d-flex"> |
140 | | - <input type="text" className="form-control m-2" placeholder="Enter your message" name="message" onKeyUp={handleKey} value={clientData.message} onChange={handleMessage}/> |
141 | | - <button type="button" className="btn btn-primary m-2" onClick={sendMessagePublic}>Enviar</button> |
142 | | - </div> |
| 112 | + </> |
| 113 | + ))} |
| 114 | + </ul> |
| 115 | + </div> |
| 116 | + <div className="send-message d-flex mt-2"> |
| 117 | + <input type="text" className="form-control m-2" placeholder="Enter your message" name="message" onKeyUp={handleKey} value={clientData.message} onChange={handleMessage}/> |
| 118 | + <button type="button" className="btn btn-primary m-2" onClick={sendMessagePublic}>Enviar</button> |
| 119 | + </div> |
143 | 120 | </div> |
144 | 121 | </div> |
| 122 | + </> |
145 | 123 | : |
146 | 124 |
|
147 | | - <div className="form-group d-flex h-100 align-items-center justify-content-center"> |
| 125 | + <div className="form-group d-flex mt-5 align-items-center justify-content-center"> |
148 | 126 | <label >Name: </label> |
149 | 127 | <input placeholder="Insert your name" className="form-control m-2" name="name" value={clientData.name} onChange={handleValue} onKeyUp={handleKey}></input> |
150 | 128 | <button type="button" name="name" className="btn btn-primary" onClick={registerClient}>Enter</button> |
|
0 commit comments