From ab9fc2836c3721945e07ee0629d2d8c7a9e06de8 Mon Sep 17 00:00:00 2001 From: sksmagr23 Date: Tue, 11 Jun 2024 16:39:44 +0530 Subject: [PATCH] Frontend: JoinExistingGame Modal implementation JoinExistingGame modal component created to capture room code for game entry. onClose function utilized for modal closure by tapping anywhere on the screen. Navigation to the /game route triggered by pressing the Enter button after input entry; otherwise,a toast prompts for input completion. Fixes #84 --- frontend/src/library/modal.tsx | 78 ++++++++++++++++++++++++++++++++++ frontend/src/pages/Home.tsx | 6 ++- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 frontend/src/library/modal.tsx diff --git a/frontend/src/library/modal.tsx b/frontend/src/library/modal.tsx new file mode 100644 index 0000000..2579584 --- /dev/null +++ b/frontend/src/library/modal.tsx @@ -0,0 +1,78 @@ +import { useRef, useState, useContext } from 'react'; +import { useNavigate } from 'react-router-dom'; +import Button from './button'; +import { ToastContext } from './toast/toast-context'; + +interface ModalProps { + onClose: () => void; +} + +const Modal: React.FC = ({ onClose }) => { + const modalRef = useRef(null); + const navigate = useNavigate(); + const [gameCode, setGameCode] = useState(''); + const { open } = useContext(ToastContext); + + const closeModal = (e: React.MouseEvent) => { + if (modalRef.current === e.target) { + onClose(); + } + }; + + const handleButtonClick = () => { + if (gameCode.trim()) { + navigate('/game'); + } else { + open({ + message: 'Please Enter The Game Code', + duration: 3000, + position: 'top-center', + color: 'warning', + }); + } + }; + + const handleInputChange = (e: React.ChangeEvent) => { + setGameCode(e.target.value); + }; + + return ( +
+
+

+ Join an +
+ Existing Game +

+
+ +
+
+
+ ); +}; + +export default Modal; diff --git a/frontend/src/pages/Home.tsx b/frontend/src/pages/Home.tsx index 237e8c7..0c2730e 100644 --- a/frontend/src/pages/Home.tsx +++ b/frontend/src/pages/Home.tsx @@ -1,11 +1,14 @@ import { useNavigate } from 'react-router-dom'; +import { useState } from 'react'; import Button from '../library/button'; import Navbar from '../Navbar'; +import Modal from '../library/modal'; import '../index.css'; import { useAuth } from '../contexts/AuthContext'; const Home: React.FC = () => { const navigate = useNavigate(); + const [showModal, setShowModal] = useState(false); const CreateGame = () => { // Logic to create a game console.log('Create Game'); @@ -14,8 +17,8 @@ const Home: React.FC = () => { const JoinGame = () => { // Logic to join a game + setShowModal(true); console.log('Join Game with code'); - navigate('/error'); }; const auth = useAuth(); @@ -62,6 +65,7 @@ const Home: React.FC = () => { onClick={JoinGame} /> + {showModal && setShowModal(false)} />} ); };