diff --git a/src/App.tsx b/src/App.tsx index ecf2d19..fa2cdc8 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,9 +1,11 @@ import React from "react"; +import Chat from "./Chat/Chat"; const App = (): React.ReactElement => { return (
This web application was built for the purposes of an EPQ project.+
Message from {sender}: {text}
) } -const Chat = (): React.ReactElement => { +const Chat = ( + { + user + }: + { + user: string, + } +): React.ReactElement => { + var messageElementsArray: JSX.Element[] = []; const msgWrapperClassName = "msg-wrapper" const connection = new WebSocket(connectionAddress) + const sendDataButtonHandler = (ev: React.MouseEvent) => { + ev.preventDefault() + const entryElement: HTMLInputElement = document.getElementById("data-entry") as HTMLInputElement + const messageData = + { + from: user, + to: "everyone", + content: entryElement.value + } + connection.send(JSON.stringify(messageData)) + } connection.addEventListener("open", (ev: Event) => { ev.preventDefault() connection.send("Hello world!") @@ -27,20 +46,27 @@ const Chat = (): React.ReactElement => { connection.addEventListener("message", (ev: MessageEvent) => { ev.preventDefault() const wrappers = document.getElementsByClassName(msgWrapperClassName) - const data = JSON.parse(ev.data) + + // Matches data from JSON data input against ChatMessage datatype for processing + const data = JSON.parse(ev.data) as ChatMessage for (let index = 0; index < wrappers.length; index++) { const element: Element | null = wrappers.item(index); if (!element) { console.error("msgWrapper class cannot be found! Message not delivered.") return } + messageElementsArray.push(