+
{isRoomPrivate ? (
) : null}
diff --git a/src/components/LoginPage/NameInput/NameInput.module.css b/src/components/LoginPage/NameInput/NameInput.module.css
new file mode 100644
index 0000000..a55980e
--- /dev/null
+++ b/src/components/LoginPage/NameInput/NameInput.module.css
@@ -0,0 +1,20 @@
+.container {
+ display: flex;
+ flex-direction: column;
+ padding: 10px 20px 60px 20px;
+ width: 300px;
+ background: radial-gradient(circle, rgba(0, 138, 255, 1) 5%, rgba(9, 9, 121, 1) 81%);
+ border: 1px solid white;
+ border-radius: 8px;
+ margin: 20px;
+ z-index: 2;
+}
+.container > button {
+ margin-top: 5px;
+ text-align: center;
+ width: 100px;
+ align-self: center;
+}
+.container > input {
+ margin-top: 10px;
+}
diff --git a/src/components/LoginPage/NameInput/NameInput.test.js b/src/components/LoginPage/NameInput/NameInput.test.js
new file mode 100644
index 0000000..44e2702
--- /dev/null
+++ b/src/components/LoginPage/NameInput/NameInput.test.js
@@ -0,0 +1,89 @@
+import React from 'react';
+import { render, screen, fireEvent } from '@testing-library/react';
+import '@testing-library/jest-dom';
+import NameInput from './NameInput';
+import { SocketContext } from '../../../App';
+
+const mockSocket = {
+ on: jest.fn(),
+ emit: jest.fn(),
+};
+
+describe('NameInput component', () => {
+ it('should renders password field when room is private', () => {
+ render(
+
+
+
+ );
+ expect(screen.getByPlaceholderText('Room password')).toBeInTheDocument();
+ });
+
+ it('should not renders password field when room is not private', () => {
+ render(
+
+
+
+ );
+ expect(screen.queryByPlaceholderText('Room password')).not.toBeInTheDocument();
+ });
+
+ it('should handles input change', () => {
+ render(
+
+
+
+ );
+ const nicknameInput = screen.getByPlaceholderText('Nickname');
+ fireEvent.change(nicknameInput, { target: { value: 'TestName' } });
+ expect(nicknameInput.value).toBe('TestName');
+ });
+
+ it('should handles password change', () => {
+ render(
+
+
+
+ );
+ const passwordInput = screen.getByPlaceholderText('Room password');
+ fireEvent.change(passwordInput, { target: { value: 'TestPassword' } });
+ expect(passwordInput.value).toBe('TestPassword');
+ });
+
+ it('should handles button click', () => {
+ render(
+
+
+
+ );
+ const nicknameInput = screen.getByPlaceholderText('Nickname');
+ fireEvent.change(nicknameInput, { target: { value: 'TestName' } });
+ const passwordInput = screen.getByPlaceholderText('Room password');
+ fireEvent.change(passwordInput, { target: { value: 'TestPassword' } });
+ const button = screen.getByText('JOIN');
+ fireEvent.click(button);
+ expect(mockSocket.emit).toHaveBeenCalledWith('player:login', {
+ name: 'TestName',
+ password: 'TestPassword',
+ roomId: 123,
+ });
+ });
+
+ it('should handles Enter key press', () => {
+ render(
+
+
+
+ );
+ const nicknameInput = screen.getByPlaceholderText('Nickname');
+ fireEvent.change(nicknameInput, { target: { value: 'TestName' } });
+ const passwordInput = screen.getByPlaceholderText('Room password');
+ fireEvent.change(passwordInput, { target: { value: 'TestPassword' } });
+ fireEvent.keyDown(nicknameInput, { key: 'Enter' });
+ expect(mockSocket.emit).toHaveBeenCalledWith('player:login', {
+ name: 'TestName',
+ password: 'TestPassword',
+ roomId: 123,
+ });
+ });
+});
diff --git a/src/components/LoginPage/ServerList/ServerList.css b/src/components/LoginPage/ServerList/ServerList.css
deleted file mode 100644
index 318a008..0000000
--- a/src/components/LoginPage/ServerList/ServerList.css
+++ /dev/null
@@ -1,51 +0,0 @@
-th {
- text-align: left;
-}
-img {
- margin-right: 5px;
- width: 20px;
- height: 20px;
-}
-th,
-td {
- padding: 8px;
- text-align: left;
- height: 50px;
-}
-tr {
- max-height: 50px;
-}
-
-table {
- border-collapse: collapse;
- width: 100%;
-}
-.server-container {
- display: flex;
- height: 500px;
- overflow: scroll;
-}
-.room-name {
- max-width: 150px;
- overflow: hidden;
-}
-/* Firefox */
-* {
- scrollbar-width: auto;
- scrollbar-color: #ffffff rgba(0, 0, 0, 0.1);
-}
-
-/* Chrome, Edge, and Safari */
-*::-webkit-scrollbar {
- background: rgba(0, 0, 0, 0);
- width: 10px;
-}
-
-*::-webkit-scrollbar-track {
- background: rgba(0, 0, 0, 0);
-}
-
-*::-webkit-scrollbar-thumb {
- background-color: #ffffff;
- border-radius: 10px;
-}
diff --git a/src/components/LoginPage/ServerList/ServerList.jsx b/src/components/LoginPage/ServerList/ServerList.jsx
deleted file mode 100644
index c6af53e..0000000
--- a/src/components/LoginPage/ServerList/ServerList.jsx
+++ /dev/null
@@ -1,82 +0,0 @@
-import React, { useContext, useEffect, useState } from 'react';
-import { SocketContext } from '../../../App';
-import lock from '../../../images/login-page/lock.png';
-import refresh from '../../../images/login-page/refresh.png';
-import ReactLoading from 'react-loading';
-
-import './ServerList.css';
-import NameInput from '../NameInput/NameInput';
-import Overlay from '../../Overlay/Overlay';
-
-const ServerList = () => {
- const socket = useContext(SocketContext);
- const [rooms, setRooms] = useState([]);
- const [joining, setJoining] = useState(false);
- const [clickedRoom, setClickedRoom] = useState(null);
- useEffect(() => {
- socket.emit('room:rooms');
- socket.on('room:rooms', data => {
- data = JSON.parse(data);
- setRooms(data);
- });
- }, [socket]);
-
- const getRooms = () => {
- setRooms(null);
- socket.emit('room:rooms');
- };
-
- const handleJoinClick = room => {
- setClickedRoom(room);
- setJoining(true);
- };
-
- return (
-
-
-
Server List
-
-

-
-
-
- {rooms ? (
-
-
-
- |
- Server |
- #/# |
- Status |
- |
-
-
-
- {rooms.map((room, index) => (
-
- {room.private ? : null} |
- {room.name} |
- {`${room.players.length}/4`} |
- {room.isStarted ? 'started' : 'waiting'} |
-
-
- |
-
- ))}
-
-
- ) : (
-
-
-
- )}
-
- {joining ? (
-
setJoining(false)}>
-
-
- ) : null}
-
- );
-};
-export default ServerList;
diff --git a/src/components/LoginPage/WindowLayout/WindowLayout.jsx b/src/components/LoginPage/WindowLayout/WindowLayout.jsx
new file mode 100644
index 0000000..ee85e85
--- /dev/null
+++ b/src/components/LoginPage/WindowLayout/WindowLayout.jsx
@@ -0,0 +1,15 @@
+import styles from './WindowLayout.module.css';
+
+const WindowLayout = ({ title, titleComponent, content }) => {
+ return (
+
+
+
{title}
+ {titleComponent}
+
+
{content}
+
+ );
+};
+
+export default WindowLayout;
diff --git a/src/components/LoginPage/LoginPage.css b/src/components/LoginPage/WindowLayout/WindowLayout.module.css
similarity index 76%
rename from src/components/LoginPage/LoginPage.css
rename to src/components/LoginPage/WindowLayout/WindowLayout.module.css
index 3376c6f..ca9f74e 100644
--- a/src/components/LoginPage/LoginPage.css
+++ b/src/components/LoginPage/WindowLayout/WindowLayout.module.css
@@ -1,25 +1,14 @@
-.login-page-container {
- display: flex;
- flex-direction: row;
-
- justify-content: center;
- align-items: flex-start;
- height: 50%;
- width: 100%;
-}
-
-.lp-container {
+.container {
margin: 50px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 500px;
- padding: 20px;
color: white;
}
-.title-container {
+.title {
display: flex;
flex-direction: row;
justify-content: center;
@@ -34,17 +23,21 @@
text-align: center;
}
-.title-container > h1 {
+.title > h1 {
width: 100%;
margin: 0;
padding: 0;
}
-.content-container {
+.content {
display: flex;
flex-direction: column;
+ justify-content: center;
+ align-items: center;
width: 100%;
- padding: 10px;
+ padding-left: 5px;
+ padding-right: 5px;
+ padding-top: 10px;
background-color: rgba(0, 0, 0, 0.5);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
border-left: 1px solid black;
diff --git a/src/components/LoginPage/WindowLayout/WindowLayout.test.js b/src/components/LoginPage/WindowLayout/WindowLayout.test.js
new file mode 100644
index 0000000..2bd1827
--- /dev/null
+++ b/src/components/LoginPage/WindowLayout/WindowLayout.test.js
@@ -0,0 +1,28 @@
+import React from 'react';
+import { render, screen } from '@testing-library/react';
+import '@testing-library/jest-dom';
+import WindowLayout from './WindowLayout';
+
+jest.mock('./WindowLayout', () => ({ title, titleComponent, content }) => (
+
+
{title}
+
{titleComponent}
+
{content}
+
+));
+
+describe('WindowLayout component', () => {
+ it('should render without crashing', () => {
+ render(
+
Test Title Component}
+ />
+ );
+ expect(screen.getByTestId('mocked-window-layout')).toBeInTheDocument();
+ expect(screen.getByTestId('mocked-title')).toHaveTextContent('Test Title');
+ expect(screen.getByTestId('mocked-title-component')).toHaveTextContent('Test Title Component');
+ expect(screen.getByTestId('mocked-content')).toHaveTextContent('Test Content');
+ });
+});
diff --git a/src/hooks/useInput.js b/src/hooks/useInput.js
index 1144f96..a933dc3 100644
--- a/src/hooks/useInput.js
+++ b/src/hooks/useInput.js
@@ -1,11 +1,11 @@
import { useState } from 'react';
-export default function useInput({ initialValue }) {
+export default function useInput(initialValue = '') {
const [value, setValue] = useState(initialValue);
const handleChange = e => {
setValue(e.target.value);
};
return {
- value,
+ value: value,
onChange: handleChange,
};
}
diff --git a/src/hooks/useKeyPress.js b/src/hooks/useKeyPress.js
index 072a74c..70e4a19 100644
--- a/src/hooks/useKeyPress.js
+++ b/src/hooks/useKeyPress.js
@@ -11,5 +11,5 @@ export default function useKeyPress(targetKey, callback) {
return () => {
window.removeEventListener('keydown', keyPressHandler);
};
- }, []);
+ }, [keyPressHandler]);
}
diff --git a/src/hooks/useSocketData.js b/src/hooks/useSocketData.js
index d33b7dc..527bc84 100644
--- a/src/hooks/useSocketData.js
+++ b/src/hooks/useSocketData.js
@@ -4,8 +4,14 @@ import { SocketContext } from '../App';
const useSocketData = port => {
const socket = useContext(SocketContext);
const [data, setData] = useState(null);
- socket.on(port, data => {
- setData(data);
+ socket.on(port, res => {
+ let parsedData;
+ try {
+ parsedData = JSON.parse(res);
+ } catch (error) {
+ parsedData = res;
+ }
+ setData(parsedData);
});
return [data, setData];
};
diff --git a/src/index.css b/src/index.css
index cae3a75..a7f45a9 100644
--- a/src/index.css
+++ b/src/index.css
@@ -67,3 +67,70 @@ canvas {
grid-column: 1 / span 2;
grid-row: 2 / span 2;
}
+
+input,
+button {
+ padding: 0;
+ border: none;
+ outline: none;
+ box-sizing: border-box;
+}
+
+input {
+ width: 100%;
+ padding: 12px;
+ font-size: 16px;
+ border-radius: 8px;
+ color: white;
+ border: 1px solid #ccc;
+ background-color: rgba(0, 0, 0, 0.2);
+ transition: border-color 0.3s ease-in-out, background-color 0.3s ease-in-out;
+}
+
+input:disabled {
+ background-color: black;
+ color: #999;
+ border: 1px solid #ddd;
+}
+
+input:focus {
+ color: black;
+ border-color: #4a90e2;
+ background-color: #fff;
+}
+
+button {
+ padding: 12px 20px;
+ font-size: 16px;
+ border-radius: 8px;
+ border: none;
+ color: #fff;
+ background-color: rgba(0, 0, 0, 0.4);
+ cursor: pointer;
+ transition: background-color 0.3s ease-in-out;
+}
+
+button:hover {
+ background-color: rgba(0, 0, 0, 1);
+}
+
+/* Firefox */
+* {
+ scrollbar-width: auto;
+ scrollbar-color: #ffffff rgba(0, 0, 0, 0.1);
+}
+
+/* Chrome, Edge, and Safari */
+*::-webkit-scrollbar {
+ background: rgba(0, 0, 0, 0);
+ width: 10px;
+}
+
+*::-webkit-scrollbar-track {
+ background: rgba(0, 0, 0, 0);
+}
+
+*::-webkit-scrollbar-thumb {
+ background-color: #ffffff;
+ border-radius: 10px;
+}