Skip to content

Commit

Permalink
Merge pull request #51 from Arquisoft/wiq
Browse files Browse the repository at this point in the history
Game and styling
  • Loading branch information
ChristianFN2 authored Mar 10, 2024
2 parents 15c4490 + 9dff314 commit 146fe27
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 19 deletions.
4 changes: 3 additions & 1 deletion questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const wiq = new WIQ_API()
* @param {Object} res - Contains the question (question) and the images of the flags (flags)
*/
app.get('/flags/question', async (req, res) => {
const question = await wiq.getQuestionAndCountryFlags()
const question = JSON.parse(await wiq.getQuestionAndCountryFlags());
res.json(question);
});

Expand All @@ -109,6 +109,8 @@ app.get('/flags/question', async (req, res) => {
*/
app.get('/flags/answer', (req, res) => {
const answeredFlag = req.body
console.log(answeredFlag);
console.log(correctAnswerFlag);
if(correctAnswerFlag==answeredFlag){
res.json({
correct: "true"
Expand Down
5 changes: 3 additions & 2 deletions webapp/src/App.js → webapp/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Typography from '@mui/material/Typography';
import Link from '@mui/material/Link';
import { useEffect } from 'react';
import axios from 'axios';
import Game from './components/Game';
function App() {
const [showLogin, setShowLogin] = useState(true);
const [user, setUser] = useState({});
Expand All @@ -33,11 +34,11 @@ function App() {
<BrowserRouter>
<Navbar />
<Routes>
<Route path='/' element={<h1 class='font-bold font-xl'>Home page</h1>}/>
<Route path='/' element={<h1 className='font-bold font-xl'>Home page</h1>}/>
<Route path='/login' element={<Login />} />
<Route path='/register' element={<AddUser />} />
<Route path='/rankings' element={<Rankings />} />
<Route path='/play' element={<h1 class='font-bold font-xl'>Play page</h1>} />
<Route path='/play' element={<Game />} />
</Routes>
</BrowserRouter>

Expand Down
29 changes: 29 additions & 0 deletions webapp/src/components/Game.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useState } from "react";
import axios from "axios";
import Question from "./Question";

const Game = () => {
const [gameStarted, setGameStarted] = useState(false);

const startGame = () => {
setGameStarted(!gameStarted);
};

return (
<div>
{gameStarted ? (
<Question />
) : (
<div className="flex flex-col items-center justify-center mt-16">
<h1 className="text-6xl font-bold text-zinc-700">Welcome to WIQ!</h1>
<button onClick={startGame} className="mt-10 border border-blue-500 text-blue-500 font-bold text-2xl py-2 px-4 rounded-full
transition-transform transform-gpu hover:scale-105">
Play
</button>
</div>
)}
</div>
)
};

export default Game;
2 changes: 1 addition & 1 deletion webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Login = () => {
{loginSuccess ? (
<div>
<Typography component="h1" variant="h5" sx={{ textAlign: 'center' }}>
{localStorage.getItem('user')}
{localStorage.getItem('uToken')}
</Typography>

</div>
Expand Down
16 changes: 8 additions & 8 deletions webapp/src/components/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
function Navbar() {

return (
<header class="lg:px-16 px-4 bg-white flex flex-wrap items-center py-4 shadow-md">
<div class="flex-1 flex justify-between items-center">
<a href="/" class="text-xl font-bold">WIQ</a>
<header className="lg:px-16 px-4 bg-white flex flex-wrap items-center py-4 shadow-md">
<div className="flex-1 flex justify-between items-center">
<a href="/" className="text-xl font-bold">WIQ</a>
</div>
<div class="hidden md:flex md:items-center md:w-auto w-full" id="menu">
<div className="hidden md:flex md:items-center md:w-auto w-full" id="menu">
<nav>
<ul class="md:flex items-center justify-between text-base text-gray-700 pt-4 md:pt-0">
<li><a class="md:p-4 py-3 px-0 block" href='/play' >Play</a></li>
<li><a class="md:p-4 py-3 px-0 block" href="/rankings">Rankings</a></li>
<li><a class="md:p-4 py-3 px-0 block" href="/login">Sign in</a></li>
<ul className="md:flex items-center justify-between text-base text-gray-700 pt-4 md:pt-0">
<li><a className="md:p-4 py-3 px-0 block font-bold text-gray-600 hover:text-gray-900" href='/play' >Play</a></li>
<li><a className="md:p-4 py-3 px-0 block font-bold text-gray-600 hover:text-gray-900" href="/rankings">Rankings</a></li>
<li><a className="md:p-4 py-3 px-0 block font-bold text-sky-500 hover:text-sky-800" href="/login">Sign in</a></li>
</ul>
</nav>
</div>
Expand Down
60 changes: 60 additions & 0 deletions webapp/src/components/Question.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React, { useState, useEffect } from "react";
import axios from "axios";

const Question = () => {
const apiEndpoint = 'http://localhost:8000';
const [question, setQuestion] = useState([])
const [loading, setLoading] = useState(true);

const fetchQuestion = async () => {
try {
const res = await axios.get(`${apiEndpoint}/flags/question`);
setQuestion(res.data);
setLoading(false);
} catch (error) {
console.error('Error fetching question:', error);
}
};

const answerQuestion = async (answer) => {
try {
setLoading(true);
const result = await axios.get(`${apiEndpoint}/flags/answer`, {answer}); // to fix

const res = await axios.get(`${apiEndpoint}/flags/question`);
setQuestion(res.data);
setLoading(false);

} catch (error) {
console.log(error);
}
}

useEffect(() => {
fetchQuestion();
}, []);

return (
<div className="bg-slate-50 shadow-lg rounded-md p-4 mx-auto max-w-2xl mt-16">
{ loading ? (
<h1 className="font-bold text-2xl text-gray-800 pl-8">Loading...</h1>
) : (
<>
<h1 className="font-bold text-3xl text-gray-800 pl-8">{question.question}</h1>
<div className="grid grid-cols-2 mt-10 item">
{question.flags.map( flag => (
<div className="rounded-xl mx-8 my-8">
<button className="transition-transform transform-gpu hover:scale-105">
<img src={flag} alt='Loading flag...' className="rounded-lg max-h-50 object-contain shadow-md"
onClick={() => answerQuestion(flag)}></img>
</button>
</div>
))}
</div>
</>
)}
</div>
)
};

export default Question;
14 changes: 7 additions & 7 deletions webapp/src/components/Rankings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ const Rankings = () => {

return (
<div className='main'>
<div class="bg-white shadow-md rounded-md p-4 mx-auto max-w-sm mt-16">
<h1 class="text-xl font-semibold mb-4">Rankings</h1>
<div className="bg-white shadow-md rounded-md p-4 mx-auto max-w-sm mt-16">
<h1 className="text-xl font-semibold mb-4">Rankings</h1>
<ul>
{users.map(user => (
<li class="flex items-center justify-between py-2 border-b border-gray-300">
<div class="flex items-center">
<span class="text-lg font-semibold mr-4">{user.ranking}</span>
<span class="text-gray-800 font-semibold">{user.user}</span>
<li className="flex items-center justify-between py-2 border-b border-gray-300">
<div className="flex items-center">
<span className="text-lg font-semibold mr-4">{user.ranking}</span>
<span className="text-gray-800 font-semibold">{user.user}</span>
</div>
<span class="text-green-500 font-semibold">{user.points} points</span>
<span className="text-green-500 font-semibold">{user.points} points</span>
</li>
))}
</ul>
Expand Down

0 comments on commit 146fe27

Please sign in to comment.