Skip to content

Commit

Permalink
suppression bon,
Browse files Browse the repository at this point in the history
confirmer mot de passe,
mis a jour en temps reel argent
  • Loading branch information
BarisRz committed Feb 6, 2024
1 parent a269442 commit dbfd69c
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 7 deletions.
13 changes: 13 additions & 0 deletions backend/src/controllers/adminControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
8 changes: 8 additions & 0 deletions backend/src/models/PlayerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
16 changes: 15 additions & 1 deletion frontend/src/components/Admin/BonManagement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
<p className="text-2xl text-center font-mont font-bold mb-2">
Expand Down Expand Up @@ -69,7 +81,9 @@ function BonManagement() {
{element.gain_credit}
</p>
<div className="admin-case2 flex justify-center">
<button type="button">Supprimer?</button>
<button type="button" onClick={() => handleDelete(element.id)}>
Supprimer?
</button>
</div>
</div>
))}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Boutique/BuyValidate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Logout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/ProfilSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 13 additions & 1 deletion frontend/src/contexts/UserContext.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
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);

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 <UserContext.Provider value={value}>{children}</UserContext.Provider>;
Expand Down
18 changes: 17 additions & 1 deletion frontend/src/pages/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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`,
Expand Down Expand Up @@ -110,6 +116,16 @@ function Signup() {
onChange={handleChange}
/>
</div>
<div className="signup-column">
<label htmlFor="password2">Mot de passe</label>
<input
type="password"
name="password2"
id="password2"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
/>
</div>
</div>
<button type="submit" className="btn-connexion">
Inscription
Expand Down

0 comments on commit dbfd69c

Please sign in to comment.