This commit is contained in:
Lukian LEIZOUR 2024-06-02 18:04:03 +02:00
parent a23fa2a709
commit 4808822058
5 changed files with 75 additions and 10 deletions

View file

@ -0,0 +1,7 @@
.helpButton-enabled {
background-color: green;
}
.helpButton-disabled {
background-color: red;
}

42
src/assets/HelpButton.jsx Normal file
View file

@ -0,0 +1,42 @@
import { useEffect, useState } from 'react';
import axios from 'axios';
import './HelpButton.css';
export default function HelpButton({ gameid, helpingprop, token }) {
const [helping, setHelping] = useState(helpingprop);
function addHelper() {
axios.post("http://leizour.fr:3000/api/v1/games/addHelper", { token, gameid })
.then(() => setHelping(true))
.catch((error) => console.error("Error adding helper"));
}
function removeHelper() {
axios.post("http://leizour.fr:3000/api/v1/games/removeHelper", { token, gameid })
.then(() => setHelping(false))
.catch((error) => console.error("Error removing helper"));
}
function handleClick(event) {
if (helping) {
removeHelper();
} else {
addHelper();
}
}
useEffect(() => {
if (helping) {
document.getElementById(`helpbutton-${gameid}`).classList.add("helpButton-enabled");
document.getElementById(`helpbutton-${gameid}`).classList.remove("helpButton-disabled");
} else {
document.getElementById(`helpbutton-${gameid}`).classList.remove("helpButton-enabled");
document.getElementById(`helpbutton-${gameid}`).classList.add("helpButton-disabled");
}
}, [helping]);
return (
<button className="helpButton" id={`helpbutton-${gameid}`} onClick={handleClick}>Set helper</button>
)
}

View file

@ -2,23 +2,30 @@ import { useState, useEffect } from 'react';
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import axios from "axios"; import axios from "axios";
import HelpButton from '../assets/HelpButton';
export default function Home() { export default function Home() {
const navigate = useNavigate(); const navigate = useNavigate();
const [user, setUser] = useState();
const [token, setToken] = useState();
const [games, setGames] = useState([]); const [games, setGames] = useState([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [name, setName] = useState(""); const [name, setName] = useState("");
useEffect(() => { useEffect(() => {
const token = localStorage.getItem("token"); const tokenLocal = localStorage.getItem("token");
if (!token) { if (!tokenLocal) {
navigate("/login"); navigate("/login");
return; return;
} }
setToken(tokenLocal);
setUser(JSON.parse(atob(tokenLocal.split(".")[1])).user);
async function fetchGames() { async function fetchGames() {
setLoading(true); setLoading(true);
try { try {
const response = await axios.post("http://localhost:3000/api/v1/games/getall", { token }); const response = await axios.post("http://leizour.fr:3000/api/v1/games/getall", { token: tokenLocal });
setGames(response.data); setGames(response.data);
} catch (error) { } catch (error) {
console.error("Error fetching games:", error); console.error("Error fetching games:", error);
@ -28,9 +35,9 @@ export default function Home() {
} }
fetchGames(); fetchGames();
}, [navigate]); }, []);
const handleSearchChange = (event) => { function handleSearchChange(event) {
setName(event.target.value); setName(event.target.value);
}; };
@ -68,9 +75,18 @@ export default function Home() {
<div>Loading...</div> <div>Loading...</div>
) : ( ) : (
filteredGames.map((game) => ( filteredGames.map((game) => (
<div key={game.id}> <div className='game'>
<img src={`https://www.myludo.fr/img/jeux/1/300/${getCode(Math.floor(game.id / 1000))}/${game.id}.png`} /> <img src={`https://www.myludo.fr/img/jeux/1/300/${getCode(Math.floor(game.id / 1000))}/${game.id}.png`} />
{game.title} <h1 className='game-title'>{game.title}</h1>
<div className='helpers'>
{(JSON.parse(game.helpers).map((helper) => (
helper === user.id ? () => {} : (
<div className='helper'>
<h2 className='helper-title'>{helper}</h2>
</div>
))))}
</div>
<HelpButton gameid={game.id} helpingprop={JSON.parse(game.helpers).includes(user.id)} token={token} />
</div> </div>
)) ))
)} )}

View file

@ -21,7 +21,7 @@ export default function Login() {
} }
try { try {
const response = await axios.post("http://localhost:3000/api/v1/auth/login", { const response = await axios.post("http://leizour.fr:3000/api/v1/auth/login", {
username: username, username: username,
password: password password: password
}) })

View file

@ -22,14 +22,14 @@ export default function Register() {
} }
try { try {
const response = await axios.post("http://localhost:3000/api/v1/auth/register", { const response = await axios.post("http://leizour.fr:3000/api/v1/auth/register", {
username: username, username: username,
name: name, name: name,
lastname: lastname, lastname: lastname,
password: password password: password
}) })
const loginResponse = await axios.post("http://localhost:3000/api/v1/auth/login", { const loginResponse = await axios.post("http://leizour.fr:3000/api/v1/auth/login", {
username: username, username: username,
password: password password: password
}) })