mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-11 08:05:56 +08:00
converted presets to new system
This commit is contained in:
parent
c730d5fa44
commit
491be89653
7 changed files with 177 additions and 91 deletions
|
@ -1,4 +1,4 @@
|
|||
const PresetDAO = require("../../dao/presetDAO");
|
||||
const PresetDAO = require("../../dao/preset");
|
||||
const {
|
||||
isTagPresetNameValid,
|
||||
validateConfig,
|
||||
|
@ -6,6 +6,16 @@ const {
|
|||
const MonkeyError = require("../../handlers/error");
|
||||
|
||||
class PresetController {
|
||||
static async getPresets(req, res, next) {
|
||||
try {
|
||||
const { uid } = req.decodedToken;
|
||||
let presets = await PresetDAO.getPresets(uid);
|
||||
return res.status(200).json(presets);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
}
|
||||
|
||||
static async addPreset(req, res, next) {
|
||||
try {
|
||||
const { name, config } = req.body;
|
||||
|
@ -13,8 +23,8 @@ class PresetController {
|
|||
if (!isTagPresetNameValid(name))
|
||||
throw new MonkeyError(400, "Invalid preset name.");
|
||||
validateConfig(config);
|
||||
await PresetDAO.addPreset(uid, name, config);
|
||||
return res.sendStatus(200);
|
||||
let preset = await PresetDAO.addPreset(uid, name, config);
|
||||
return res.status(200).json(preset);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
|
@ -26,7 +36,7 @@ class PresetController {
|
|||
const { uid } = req.decodedToken;
|
||||
if (!isTagPresetNameValid(name))
|
||||
throw new MonkeyError(400, "Invalid preset name.");
|
||||
validateConfig(config);
|
||||
if (config) validateConfig(config);
|
||||
await PresetDAO.editPreset(uid, id, name, config);
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
import { authenticateRequest } from "../../middlewares/auth";
|
||||
import PresetController from "../controllers/preset";
|
||||
const { authenticateRequest } = require("../../middlewares/auth");
|
||||
const PresetController = require("../controllers/preset");
|
||||
|
||||
const { Router } = require("express");
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/presets/add", authenticateRequest, PresetController.addPreset);
|
||||
router.get("/", authenticateRequest, PresetController.getPresets);
|
||||
|
||||
router.post("/presets/edit", authenticateRequest, PresetController.editPreset);
|
||||
router.post("/add", authenticateRequest, PresetController.addPreset);
|
||||
|
||||
router.get(
|
||||
"/presets/remove",
|
||||
authenticateRequest,
|
||||
PresetController.removePreset
|
||||
);
|
||||
router.post("/edit", authenticateRequest, PresetController.editPreset);
|
||||
|
||||
router.post("/remove", authenticateRequest, PresetController.removePreset);
|
||||
|
||||
module.exports = router;
|
||||
|
|
|
@ -3,20 +3,38 @@ const { mongoDB } = require("../init/mongodb");
|
|||
const uuid = require("uuid");
|
||||
|
||||
class PresetDAO {
|
||||
static async getPresets(uid) {
|
||||
const preset = await mongoDB()
|
||||
.collection("presets")
|
||||
.find({ uid })
|
||||
.sort({ timestamp: -1 })
|
||||
.toArray(); // this needs to be changed to later take patreon into consideration
|
||||
return preset;
|
||||
}
|
||||
|
||||
static async addPreset(uid, name, config) {
|
||||
const count = await mongoDB().collection("presets").find({ uid }).count();
|
||||
if (count >= 10) throw new MonkeyError(409, "Too many presets");
|
||||
return await mongoDB()
|
||||
.collection("presets")
|
||||
.insertOne({ id: uuid.v4(), uid, name, config });
|
||||
const id = uuid.v4();
|
||||
await mongoDB().collection("presets").insertOne({ id, uid, name, config });
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
};
|
||||
}
|
||||
|
||||
static async editPreset(uid, id, name, config) {
|
||||
const preset = await mongoDB().collection("presets").findOne({ uid, id });
|
||||
if (!preset) throw new MonkeyError(404, "Preset not found");
|
||||
return await mongoDB()
|
||||
.collection("presets")
|
||||
.updateOne({ uid, id }, { $set: { name, config } });
|
||||
if (config) {
|
||||
return await mongoDB()
|
||||
.collection("presets")
|
||||
.updateOne({ uid, id }, { $set: { name, config } });
|
||||
} else {
|
||||
return await mongoDB()
|
||||
.collection("presets")
|
||||
.updateOne({ uid, id }, { $set: { name } });
|
||||
}
|
||||
}
|
||||
|
||||
static async removePreset(uid, id) {
|
||||
|
|
12
src/js/db.js
12
src/js/db.js
|
@ -95,6 +95,18 @@ export async function initSnapshot() {
|
|||
}
|
||||
});
|
||||
|
||||
let presetsData = await axiosInstance.get("/presets");
|
||||
snap.presets = presetsData.data;
|
||||
snap.presets = snap.presets.sort((a, b) => {
|
||||
if (a.name > b.name) {
|
||||
return 1;
|
||||
} else if (a.name < b.name) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
dbSnapshot = snap;
|
||||
loadTags(dbSnapshot.tags);
|
||||
return dbSnapshot;
|
||||
|
|
|
@ -4,6 +4,7 @@ import * as Notifications from "./notifications";
|
|||
import * as Settings from "./settings";
|
||||
import * as Config from "./config";
|
||||
import axiosInstance from "./axios-instance";
|
||||
import { config } from "dotenv";
|
||||
|
||||
export function show(action, id, name) {
|
||||
if (action === "add") {
|
||||
|
@ -63,10 +64,11 @@ function hide() {
|
|||
}
|
||||
}
|
||||
|
||||
function apply() {
|
||||
async function apply() {
|
||||
let action = $("#presetWrapper #presetEdit").attr("action");
|
||||
let inputVal = $("#presetWrapper #presetEdit input").val();
|
||||
let presetid = $("#presetWrapper #presetEdit").attr("presetid");
|
||||
let override = $("#presetWrapper #presetEdit label input").prop("checked");
|
||||
let configChanges = Config.getConfigChanges();
|
||||
let activeTagIds = [];
|
||||
DB.getSnapshot().tags.forEach((tag) => {
|
||||
|
@ -78,80 +80,126 @@ function apply() {
|
|||
hide();
|
||||
if (action === "add") {
|
||||
Loader.show();
|
||||
axiosInstance
|
||||
.post("/addPreset", {
|
||||
obj: {
|
||||
name: inputVal,
|
||||
config: configChanges,
|
||||
},
|
||||
})
|
||||
.then((e) => {
|
||||
console.log(e);
|
||||
console.log("Should be ready to go away");
|
||||
Loader.hide();
|
||||
let status = e.data.resultCode;
|
||||
if (status === 1) {
|
||||
Notifications.add("Preset added", 1, 2);
|
||||
DB.getSnapshot().presets.push({
|
||||
name: inputVal,
|
||||
config: configChanges,
|
||||
_id: e.data.id,
|
||||
});
|
||||
Settings.update();
|
||||
} else if (status === -1) {
|
||||
Notifications.add("Invalid preset name", 0);
|
||||
} else if (status === -2) {
|
||||
Notifications.add("You can't add any more presets", 0);
|
||||
} else if (status < -1) {
|
||||
Notifications.add("Unknown error: " + e.data.message, -1);
|
||||
}
|
||||
});
|
||||
} else if (action === "edit") {
|
||||
Loader.show();
|
||||
axiosInstance
|
||||
.post("/editPreset", {
|
||||
presetName: inputVal,
|
||||
presetid: presetid,
|
||||
let response;
|
||||
try {
|
||||
response = await axiosInstance.post("/presets/add", {
|
||||
name: inputVal,
|
||||
config: configChanges,
|
||||
})
|
||||
.then((e) => {
|
||||
Loader.hide();
|
||||
let status = e.data.resultCode;
|
||||
if (status === 1) {
|
||||
Notifications.add("Preset updated", 1);
|
||||
let preset = DB.getSnapshot().presets.filter(
|
||||
(preset) => preset._id == presetid
|
||||
)[0];
|
||||
preset.name = inputVal;
|
||||
preset.config = configChanges;
|
||||
Settings.update();
|
||||
} else if (status === -1) {
|
||||
Notifications.add("Invalid preset name", 0);
|
||||
} else if (status < -1) {
|
||||
Notifications.add("Unknown error: " + e.data.message, -1);
|
||||
}
|
||||
});
|
||||
} else if (action === "remove") {
|
||||
} catch (e) {
|
||||
Loader.hide();
|
||||
let msg = e?.response?.data?.message ?? e.message;
|
||||
Notifications.add("Failed to add preset: " + msg, -1);
|
||||
return;
|
||||
}
|
||||
Loader.hide();
|
||||
if (response.status !== 200) {
|
||||
Notifications.add(response.data.message);
|
||||
} else {
|
||||
Notifications.add("Preset added", 1, 2);
|
||||
DB.getSnapshot().presets.push({
|
||||
name: inputVal,
|
||||
config: configChanges,
|
||||
id: response.data.id,
|
||||
});
|
||||
Settings.update();
|
||||
}
|
||||
} else if (action === "edit") {
|
||||
// Loader.show();
|
||||
// axiosInstance
|
||||
// .post("/editPreset", {
|
||||
// presetName: inputVal,
|
||||
// presetid: presetid,
|
||||
// config: configChanges,
|
||||
// })
|
||||
// .then((e) => {
|
||||
// Loader.hide();
|
||||
// let status = e.data.resultCode;
|
||||
// if (status === 1) {
|
||||
// Notifications.add("Preset updated", 1);
|
||||
// let preset = DB.getSnapshot().presets.filter(
|
||||
// (preset) => preset._id == presetid
|
||||
// )[0];
|
||||
// preset.name = inputVal;
|
||||
// preset.config = configChanges;
|
||||
// Settings.update();
|
||||
// } else if (status === -1) {
|
||||
// Notifications.add("Invalid preset name", 0);
|
||||
// } else if (status < -1) {
|
||||
// Notifications.add("Unknown error: " + e.data.message, -1);
|
||||
// }
|
||||
// });
|
||||
Loader.show();
|
||||
axiosInstance
|
||||
.post("/removePreset", {
|
||||
presetid,
|
||||
})
|
||||
.then((e) => {
|
||||
Loader.hide();
|
||||
let status = e.data.resultCode;
|
||||
if (status === 1) {
|
||||
Notifications.add("Preset removed", 1);
|
||||
DB.getSnapshot().presets.forEach((preset, index) => {
|
||||
if (preset._id === presetid) {
|
||||
DB.getSnapshot().presets.splice(index, 1);
|
||||
}
|
||||
});
|
||||
Settings.update();
|
||||
} else if (status < -1) {
|
||||
Notifications.add("Unknown error: " + e.data.message, -1);
|
||||
let response;
|
||||
try {
|
||||
response = await axiosInstance.post("/presets/edit", {
|
||||
name: inputVal,
|
||||
id: presetid,
|
||||
config: override === true ? configChanges : null,
|
||||
});
|
||||
} catch (e) {
|
||||
Loader.hide();
|
||||
let msg = e?.response?.data?.message ?? e.message;
|
||||
Notifications.add("Failed to edit preset: " + msg, -1);
|
||||
return;
|
||||
}
|
||||
Loader.hide();
|
||||
if (response.status !== 200) {
|
||||
Notifications.add(response.data.message);
|
||||
} else {
|
||||
Notifications.add("Preset updated", 1);
|
||||
let preset = DB.getSnapshot().presets.filter(
|
||||
(preset) => preset.id == presetid
|
||||
)[0];
|
||||
preset.name = inputVal;
|
||||
if (override === true) preset.config = configChanges;
|
||||
Settings.update();
|
||||
}
|
||||
} else if (action === "remove") {
|
||||
// Loader.show();
|
||||
// axiosInstance
|
||||
// .post("/removePreset", {
|
||||
// presetid,
|
||||
// })
|
||||
// .then((e) => {
|
||||
// Loader.hide();
|
||||
// let status = e.data.resultCode;
|
||||
// if (status === 1) {
|
||||
// Notifications.add("Preset removed", 1);
|
||||
// DB.getSnapshot().presets.forEach((preset, index) => {
|
||||
// if (preset.id === presetid) {
|
||||
// DB.getSnapshot().presets.splice(index, 1);
|
||||
// }
|
||||
// });
|
||||
// Settings.update();
|
||||
// } else if (status < -1) {
|
||||
// Notifications.add("Unknown error: " + e.data.message, -1);
|
||||
// }
|
||||
// });
|
||||
Loader.show();
|
||||
let response;
|
||||
try {
|
||||
response = await axiosInstance.post("/presets/remove", {
|
||||
id: presetid,
|
||||
});
|
||||
} catch (e) {
|
||||
Loader.hide();
|
||||
let msg = e?.response?.data?.message ?? e.message;
|
||||
Notifications.add("Failed to remove preset: " + msg, -1);
|
||||
return;
|
||||
}
|
||||
Loader.hide();
|
||||
if (response.status !== 200) {
|
||||
Notifications.add(response.data.message);
|
||||
} else {
|
||||
Notifications.add("Preset removed", 1);
|
||||
DB.getSnapshot().presets.forEach((preset, index) => {
|
||||
if (preset.id === presetid) {
|
||||
DB.getSnapshot().presets.splice(index, 1);
|
||||
}
|
||||
});
|
||||
Settings.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ import * as TagController from "./tag-controller";
|
|||
export function apply(id) {
|
||||
// console.log(DB.getSnapshot().presets);
|
||||
DB.getSnapshot().presets.forEach((preset) => {
|
||||
if (preset._id == id) {
|
||||
if (preset.id == id) {
|
||||
Config.apply(JSON.parse(JSON.stringify(preset.config)));
|
||||
TagController.clear(true);
|
||||
if (preset.config.tags) {
|
||||
|
|
|
@ -452,7 +452,7 @@ function refreshPresetsSettingsSection() {
|
|||
let presetsEl = $(".pageSettings .section.presets .presetsList").empty();
|
||||
DB.getSnapshot().presets.forEach((preset) => {
|
||||
presetsEl.append(`
|
||||
<div class="buttons preset" id="${preset._id}">
|
||||
<div class="buttons preset" id="${preset.id}">
|
||||
<div class="button presetButton">
|
||||
<div class="title">${preset.name}</div>
|
||||
</div>
|
||||
|
|
Loading…
Add table
Reference in a new issue