diff --git a/backend/api/controllers/user.js b/backend/api/controllers/user.js index 9625e3c47..366500c51 100644 --- a/backend/api/controllers/user.js +++ b/backend/api/controllers/user.js @@ -47,6 +47,16 @@ class UserController { } } + static async clearPb(req, res, next) { + try { + const { uid } = req.decodedToken; + await UsersDAO.clearPb(uid); + return res.sendStatus(200); + } catch (e) { + return next(e); + } + } + static async checkName(req, res, next) { try { const { name } = req.body; diff --git a/backend/api/routes/user.js b/backend/api/routes/user.js index f8a4bee48..1369ecd62 100644 --- a/backend/api/routes/user.js +++ b/backend/api/routes/user.js @@ -4,6 +4,8 @@ const UserController = require("../controllers/user"); const router = Router(); +router.get("/", authenticateRequest, UserController.getUser); + router.post("/signup", UserController.createNewUser); router.post("/checkName", UserController.checkName); @@ -14,7 +16,7 @@ router.post("/updateName", authenticateRequest, UserController.updateName); router.post("/updateEmail", authenticateRequest, UserController.updateEmail); -router.get("/", authenticateRequest, UserController.getUser); +router.post("/clearPb", authenticateRequest, UserController.clearPb); router.post("/tags/add", authenticateRequest, UserController.addTag); diff --git a/backend/dao/user.js b/backend/dao/user.js index b00ca4216..768f9526b 100644 --- a/backend/dao/user.js +++ b/backend/dao/user.js @@ -25,6 +25,12 @@ class UsersDAO { .updateOne({ uid }, { $set: { name } }); } + static async clearPb(uid) { + return await mongoDB() + .collection("users") + .updateOne({ uid }, { $set: { personalBests: {} } }); + } + static async isNameAvailable(name) { const nameDoc = await mongoDB().collection("users").findOne({ name }); if (nameDoc) { diff --git a/src/js/simple-popups.js b/src/js/simple-popups.js index 5a86c2c66..b791e1213 100644 --- a/src/js/simple-popups.js +++ b/src/js/simple-popups.js @@ -179,7 +179,7 @@ list.updateEmail = new SimplePopup( .post("/user/updateEmail", { uid: user.uid, previousEmail: user.email, - newEmail: newEmail, + newEmail: email, }) .then((data) => { Loader.hide(); @@ -333,7 +333,7 @@ list.resetPersonalBests = new SimplePopup( let response; try { - response = await axiosInstance.post("/user/resetPbs"); + response = await axiosInstance.post("/user/clearPb"); } catch (e) { Loader.hide(); let msg = e?.response?.data?.message ?? e.message; @@ -344,10 +344,8 @@ list.resetPersonalBests = new SimplePopup( if (response.status !== 200) { Notifications.add(response.data.message); } else { - Notifications.add("Personal bests removed, refreshing the page...", 0); - setTimeout(() => { - location.reload(); - }, 1500); + Notifications.add("Personal bests have been reset", 1); + DB.getSnapshot().personalBests = {}; } } catch (e) { Loader.hide();