mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-11-10 06:01:28 +08:00
config support
This commit is contained in:
parent
9d7427ed3d
commit
844e67ac3e
4 changed files with 35 additions and 23 deletions
|
|
@ -1,7 +1,16 @@
|
|||
import ConfigDAO from "../../dao/config";
|
||||
import { validateConfig } from "../../handlers/validation";
|
||||
const ConfigDAO = require("../../dao/config");
|
||||
const { validateConfig } = require("../../handlers/validation");
|
||||
|
||||
class ConfigController {
|
||||
static async getConfig(req, res, next) {
|
||||
try {
|
||||
const { uid } = req.decodedToken;
|
||||
let config = await ConfigDAO.getConfig(uid);
|
||||
return res.status(200).json(config);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
}
|
||||
static async saveConfig(req, res, next) {
|
||||
try {
|
||||
const { config } = req.body;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
import { authenticateRequest } from "../../middlewares/auth";
|
||||
|
||||
const { authenticateRequest } = require("../../middlewares/auth");
|
||||
const { Router } = require("express");
|
||||
import ConfigController from "../controllers/auth";
|
||||
const ConfigController = require("../controllers/config");
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/config/save", authenticateRequest, ConfigController.saveConfig);
|
||||
router.get("/", authenticateRequest, ConfigController.getConfig);
|
||||
|
||||
router.post("/save", authenticateRequest, ConfigController.saveConfig);
|
||||
|
||||
module.exports = router;
|
||||
|
|
|
|||
|
|
@ -2,13 +2,17 @@ const MonkeyError = require("../handlers/error");
|
|||
const { mongoDB } = require("../init/mongodb");
|
||||
|
||||
class ConfigDAO {
|
||||
|
||||
static async saveConfig(uid, config) {
|
||||
return await mongoDB()
|
||||
.collection("configs")
|
||||
.updateOne({ uid }, { $set: { config } }, { upsert: true });
|
||||
}
|
||||
|
||||
static async getConfig(uid) {
|
||||
let config = await mongoDB().collection("configs").findOne({ uid });
|
||||
if (!config) throw new MonkeyError(404, "Config not found");
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConfigDAO;
|
||||
|
|
|
|||
30
src/js/db.js
30
src/js/db.js
|
|
@ -60,10 +60,11 @@ export async function initSnapshot() {
|
|||
snap.personalBests = userData.personalBests;
|
||||
snap.banned = userData.banned;
|
||||
snap.verified = userData.verified;
|
||||
if(userData.globalStats){
|
||||
if (userData.globalStats) {
|
||||
snap.globalStats = userData.globalStats;
|
||||
}
|
||||
snap.favouriteThemes = userData.favouriteThemes === undefined ? [] : userData.favouriteThemes;
|
||||
snap.favouriteThemes =
|
||||
userData.favouriteThemes === undefined ? [] : userData.favouriteThemes;
|
||||
try {
|
||||
if (userData.lbMemory.time15 !== undefined) {
|
||||
snap.lbMemory.time15 = userData.lbMemory.time15;
|
||||
|
|
@ -72,6 +73,11 @@ export async function initSnapshot() {
|
|||
snap.lbMemory.time60 = userData.lbMemory.time60;
|
||||
}
|
||||
} catch {}
|
||||
|
||||
let configData = await axiosInstance.get("/config");
|
||||
configData = configData.data;
|
||||
snap.config = configData.config;
|
||||
|
||||
dbSnapshot = snap;
|
||||
loadTags(dbSnapshot.tags);
|
||||
return dbSnapshot;
|
||||
|
|
@ -444,20 +450,12 @@ export function updateLbMemory(mode, mode2, type, value) {
|
|||
export async function saveConfig(config) {
|
||||
if (firebase.auth().currentUser !== null) {
|
||||
AccountButton.loading(true);
|
||||
axiosInstance
|
||||
.post("/saveConfig", {
|
||||
obj: config,
|
||||
})
|
||||
.then((response) => {
|
||||
AccountButton.loading(false);
|
||||
if (response.data.resultCode !== 1) {
|
||||
Notifications.add(
|
||||
`Error saving config to DB! ${response.data.message}`,
|
||||
4000
|
||||
);
|
||||
}
|
||||
return;
|
||||
});
|
||||
try {
|
||||
let response = await axiosInstance.post("/config/save", { config });
|
||||
} catch (e) {
|
||||
Notifications.add(e.message, -1);
|
||||
}
|
||||
AccountButton.loading(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue