From dbfd69c8966e62cbfc86b82db350472d7dd09b6b Mon Sep 17 00:00:00 2001 From: Baris G Date: Tue, 6 Feb 2024 19:00:20 +0100 Subject: [PATCH] suppression bon, confirmer mot de passe, mis a jour en temps reel argent --- backend/src/controllers/adminControllers.js | 13 +++++++++++++ backend/src/models/PlayerManager.js | 8 ++++++++ backend/src/router.js | 1 + .../src/components/Admin/BonManagement.jsx | 16 +++++++++++++++- .../src/components/Boutique/BuyValidate.jsx | 3 ++- frontend/src/components/Logout.jsx | 4 ++-- frontend/src/components/ProfilSettings.jsx | 3 ++- frontend/src/contexts/UserContext.jsx | 14 +++++++++++++- frontend/src/pages/Signup.jsx | 18 +++++++++++++++++- 9 files changed, 73 insertions(+), 7 deletions(-) diff --git a/backend/src/controllers/adminControllers.js b/backend/src/controllers/adminControllers.js index 32e160d..b4e5dbe 100644 --- a/backend/src/controllers/adminControllers.js +++ b/backend/src/controllers/adminControllers.js @@ -37,8 +37,21 @@ const browseBons = async (req, res) => { } }; +const destroyCode = async (req, res) => { + try { + const result = await tables.player.deleteCode(req.params.id); + if (result.affectedRows === 0) { + return res.status(404).json({ error: "Un erreur est survenue" }); + } + return res.sendStatus(200); + } catch (error) { + return res.status(500).json({ error: error.message }); + } +}; + module.exports = { browse, destroy, browseBons, + destroyCode, }; diff --git a/backend/src/models/PlayerManager.js b/backend/src/models/PlayerManager.js index bfcb5fb..1a7dac0 100644 --- a/backend/src/models/PlayerManager.js +++ b/backend/src/models/PlayerManager.js @@ -253,6 +253,14 @@ class PlayerManager extends AbstractManager { return result; } + + async deleteCode(id) { + const [rows] = await this.database.query(`DELETE FROM bon WHERE id=?`, [ + id, + ]); + + return rows; + } } module.exports = PlayerManager; diff --git a/backend/src/router.js b/backend/src/router.js index 76bc407..17c77f2 100644 --- a/backend/src/router.js +++ b/backend/src/router.js @@ -86,5 +86,6 @@ const adminControllers = require("./controllers/adminControllers"); router.get("/admin/players", verifyToken, adminControllers.browse); router.delete("/admin/players/:id", verifyToken, adminControllers.destroy); router.get("/admin/bons", verifyToken, adminControllers.browseBons); +router.delete("/admin/bons/:id", verifyToken, adminControllers.destroyCode); module.exports = router; diff --git a/frontend/src/components/Admin/BonManagement.jsx b/frontend/src/components/Admin/BonManagement.jsx index a25b9cf..98a7f91 100644 --- a/frontend/src/components/Admin/BonManagement.jsx +++ b/frontend/src/components/Admin/BonManagement.jsx @@ -31,6 +31,18 @@ function BonManagement() { }) .catch((err) => failed(err.response.data.error)); }; + + const handleDelete = (id) => { + axios + .delete(`${import.meta.env.VITE_BACKEND_URL}/api/admin/bons/${id}`, { + withCredentials: true, + }) + .then(() => { + success("Code supprimé"); + setRefresh(!refresh); + }) + .catch((err) => failed(err.response.data.error)); + }; return (

@@ -69,7 +81,9 @@ function BonManagement() { {element.gain_credit}

- +
))} diff --git a/frontend/src/components/Boutique/BuyValidate.jsx b/frontend/src/components/Boutique/BuyValidate.jsx index 46f4247..593ed4d 100644 --- a/frontend/src/components/Boutique/BuyValidate.jsx +++ b/frontend/src/components/Boutique/BuyValidate.jsx @@ -4,7 +4,7 @@ import { useUser } from "../../contexts/UserContext"; import { failed, success } from "../../services/toast"; function BuyValidate({ setModalValidate, modalValidate, avatarId }) { - const { user } = useUser(); + const { user, refreshUser } = useUser(); const handleBuyAvatar = async () => { try { const res = await axios.post( @@ -14,6 +14,7 @@ function BuyValidate({ setModalValidate, modalValidate, avatarId }) { if (res.status === 200) { success("Avatar acheté avec succès !"); setModalValidate(!modalValidate); + refreshUser(); } } catch (error) { failed(error.response.data.error); diff --git a/frontend/src/components/Logout.jsx b/frontend/src/components/Logout.jsx index f4cfa00..b9c6621 100644 --- a/frontend/src/components/Logout.jsx +++ b/frontend/src/components/Logout.jsx @@ -6,8 +6,8 @@ import { useUser } from "../contexts/UserContext"; import piece from "../assets/images/credit_1.png"; import "../styles/navbar.scss"; -function Logout({ user }) { - const { setUser } = useUser(); +function Logout() { + const { setUser, user } = useUser(); const navigate = useNavigate(); const handleLogout = async (event) => { diff --git a/frontend/src/components/ProfilSettings.jsx b/frontend/src/components/ProfilSettings.jsx index 8103af4..002a43a 100644 --- a/frontend/src/components/ProfilSettings.jsx +++ b/frontend/src/components/ProfilSettings.jsx @@ -18,7 +18,7 @@ import close from "../assets/images/close.svg"; import robot from "../assets/images/robot.png"; export default function ProfilSettings() { - const { user, setUser } = useUser(); + const { user, setUser, refreshUser } = useUser(); const [redeemBody, setRedeemBody] = useState({ code: "", id: 0, @@ -105,6 +105,7 @@ export default function ProfilSettings() { if (res.status === 201) { success(`Code utilisé avec succes +${res.data.gain} !`); + refreshUser(); } } catch (error) { failed(error.response.data.error); diff --git a/frontend/src/contexts/UserContext.jsx b/frontend/src/contexts/UserContext.jsx index f8dadce..b9c491d 100644 --- a/frontend/src/contexts/UserContext.jsx +++ b/frontend/src/contexts/UserContext.jsx @@ -1,5 +1,6 @@ import { createContext, useState, useContext, useMemo } from "react"; import PropTypes from "prop-types"; +import axios from "axios"; const UserContext = createContext(null); export const useUser = () => useContext(UserContext); @@ -7,8 +8,19 @@ export const useUser = () => useContext(UserContext); export function UserProvider({ children }) { const [user, setUser] = useState(false); + const refreshUser = async () => { + try { + const response = await axios.get( + `${import.meta.env.VITE_BACKEND_URL}/api/players/${user.id}` + ); + setUser(response.data); + } catch (error) { + console.error(error); + } + }; + const value = useMemo(() => { - return { user, setUser }; + return { user, setUser, refreshUser }; }, [user, setUser]); return {children}; diff --git a/frontend/src/pages/Signup.jsx b/frontend/src/pages/Signup.jsx index e604b26..9471d87 100644 --- a/frontend/src/pages/Signup.jsx +++ b/frontend/src/pages/Signup.jsx @@ -8,8 +8,9 @@ import { success, failed } from "../services/toast"; function Signup() { const [created, setCreated] = useState(false); + const [confirmPassword, setConfirmPassword] = useState(""); const [user, setUser] = useState({ - fistname: "", + firstname: "", lastname: "", email: "", pseudo: "", @@ -24,6 +25,11 @@ function Signup() { const handleSubmit = async (event) => { event.preventDefault(); + if (user.password !== confirmPassword) { + failed("Les mots de passe ne correspondent pas"); + return; + } + try { const res = await axios.post( `${import.meta.env.VITE_BACKEND_URL}/api/players`, @@ -110,6 +116,16 @@ function Signup() { onChange={handleChange} /> +
+ + setConfirmPassword(e.target.value)} + /> +