mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2026-01-09 00:45:32 +08:00
switched presets to _id
This commit is contained in:
parent
62084071f4
commit
2f1e129d88
6 changed files with 31 additions and 72 deletions
|
|
@ -32,12 +32,12 @@ class PresetController {
|
|||
|
||||
static async editPreset(req, res, next) {
|
||||
try {
|
||||
const { id, name, config } = req.body;
|
||||
const { _id, name, config } = req.body;
|
||||
const { uid } = req.decodedToken;
|
||||
if (!isTagPresetNameValid(name))
|
||||
throw new MonkeyError(400, "Invalid preset name.");
|
||||
if (config) validateConfig(config);
|
||||
await PresetDAO.editPreset(uid, id, name, config);
|
||||
await PresetDAO.editPreset(uid, _id, name, config);
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
|
|
@ -46,9 +46,9 @@ class PresetController {
|
|||
|
||||
static async removePreset(req, res, next) {
|
||||
try {
|
||||
const { id } = req.body;
|
||||
const { _id } = req.body;
|
||||
const { uid } = req.decodedToken;
|
||||
await PresetDAO.removePreset(uid, id);
|
||||
await PresetDAO.removePreset(uid, _id);
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
const MonkeyError = require("../handlers/error");
|
||||
const { mongoDB } = require("../init/mongodb");
|
||||
const uuid = require("uuid");
|
||||
const { ObjectID } = require("mongodb");
|
||||
|
||||
class PresetDAO {
|
||||
static async getPresets(uid) {
|
||||
|
|
@ -15,36 +15,39 @@ class PresetDAO {
|
|||
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");
|
||||
const id = uuid.v4();
|
||||
await mongoDB()
|
||||
let preset = await mongoDB()
|
||||
.collection("presets")
|
||||
.insertOne({ _id: id, uid, name, config });
|
||||
.insertOne({ uid, name, config });
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
insertedId: preset.insertedId,
|
||||
};
|
||||
}
|
||||
|
||||
static async editPreset(uid, id, name, config) {
|
||||
const preset = await mongoDB().collection("presets").findOne({ uid, id });
|
||||
static async editPreset(uid, _id, name, config) {
|
||||
console.log(_id);
|
||||
const preset = await mongoDB()
|
||||
.collection("presets")
|
||||
.findOne({ uid, _id: ObjectID(_id) });
|
||||
if (!preset) throw new MonkeyError(404, "Preset not found");
|
||||
if (config) {
|
||||
return await mongoDB()
|
||||
.collection("presets")
|
||||
.updateOne({ uid, _id: id }, { $set: { name, config } });
|
||||
.updateOne({ uid, _id: ObjectID(_id) }, { $set: { name, config } });
|
||||
} else {
|
||||
return await mongoDB()
|
||||
.collection("presets")
|
||||
.updateOne({ uid, _id: id }, { $set: { name } });
|
||||
.updateOne({ uid, _id: ObjectID(_id) }, { $set: { name } });
|
||||
}
|
||||
}
|
||||
|
||||
static async removePreset(uid, id) {
|
||||
static async removePreset(uid, _id) {
|
||||
const preset = await mongoDB()
|
||||
.collection("presets")
|
||||
.findOne({ uid, _id: id });
|
||||
.findOne({ uid, _id: ObjectID(_id) });
|
||||
if (!preset) throw new MonkeyError(404, "Preset not found");
|
||||
return await mongoDB().collection("presets").remove({ uid, _id: id });
|
||||
return await mongoDB()
|
||||
.collection("presets")
|
||||
.deleteOne({ uid, _id: ObjectID(_id) });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -273,10 +273,10 @@ export function updatePresetCommands() {
|
|||
let dis = preset.name;
|
||||
|
||||
commandsPresets.list.push({
|
||||
id: "applyPreset" + preset.id,
|
||||
id: "applyPreset" + preset._id,
|
||||
display: dis,
|
||||
exec: () => {
|
||||
PresetController.apply(preset.id);
|
||||
PresetController.apply(preset._id);
|
||||
TestUI.updateModesNotice();
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ async function apply() {
|
|||
let activeTagIds = [];
|
||||
DB.getSnapshot().tags.forEach((tag) => {
|
||||
if (tag.active) {
|
||||
activeTagIds.push(tag.id);
|
||||
activeTagIds.push(tag._id);
|
||||
}
|
||||
});
|
||||
configChanges.tags = activeTagIds;
|
||||
|
|
@ -100,41 +100,17 @@ async function apply() {
|
|||
DB.getSnapshot().presets.push({
|
||||
name: inputVal,
|
||||
config: configChanges,
|
||||
id: response.data.id,
|
||||
_id: response.data.insertedId,
|
||||
});
|
||||
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();
|
||||
let response;
|
||||
try {
|
||||
response = await axiosInstance.post("/presets/edit", {
|
||||
name: inputVal,
|
||||
id: presetid,
|
||||
_id: presetid,
|
||||
config: override === true ? configChanges : null,
|
||||
});
|
||||
} catch (e) {
|
||||
|
|
@ -149,38 +125,18 @@ async function apply() {
|
|||
} else {
|
||||
Notifications.add("Preset updated", 1);
|
||||
let preset = DB.getSnapshot().presets.filter(
|
||||
(preset) => preset.id == presetid
|
||||
(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,
|
||||
_id: presetid,
|
||||
});
|
||||
} catch (e) {
|
||||
Loader.hide();
|
||||
|
|
@ -194,7 +150,7 @@ async function apply() {
|
|||
} else {
|
||||
Notifications.add("Preset removed", 1);
|
||||
DB.getSnapshot().presets.forEach((preset, index) => {
|
||||
if (preset.id === presetid) {
|
||||
if (preset._id === presetid) {
|
||||
DB.getSnapshot().presets.splice(index, 1);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ import * as Settings from "./settings";
|
|||
import * as TestLogic from "./test-logic";
|
||||
import * as TagController from "./tag-controller";
|
||||
|
||||
export function apply(id) {
|
||||
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