diff --git a/frontend/src/ts/modals/edit-tag.ts b/frontend/src/ts/modals/edit-tag.ts index 5582aa14d59d..a56f4e0836c0 100644 --- a/frontend/src/ts/modals/edit-tag.ts +++ b/frontend/src/ts/modals/edit-tag.ts @@ -5,6 +5,11 @@ import * as Settings from "../pages/settings"; import AnimatedModal, { ShowOptions } from "../utils/animated-modal"; import { SimpleModal, TextInput } from "../utils/simple-modal"; import { TagNameSchema } from "@monkeytype/schemas/users"; +import { SnapshotUserTag } from "../constants/default-snapshot"; + +function getTagFromSnapshot(tagId: string): SnapshotUserTag | undefined { + return DB.getSnapshot()?.tags.find((tag) => tag._id === tagId); +} const cleanTagName = (tagName: string): string => tagName.replaceAll(" ", "_"); const tagNameValidation = async (tagName: string): Promise => { @@ -89,12 +94,13 @@ const actionModals: Record = { }; } - DB.getSnapshot()?.tags?.forEach((tag) => { - if (tag._id === tagId) { - tag.name = tagName; - tag.display = propTagName; - } - }); + const matchingTag = getTagFromSnapshot(tagId); + + if (matchingTag !== undefined) { + matchingTag.name = tagName; + matchingTag.display = propTagName; + } + void Settings.update(); return { status: 1, message: `Tag updated` }; @@ -120,11 +126,11 @@ const actionModals: Record = { }; } - DB.getSnapshot()?.tags?.forEach((tag, index: number) => { - if (tag._id === tagId) { - DB.getSnapshot()?.tags?.splice(index, 1); - } - }); + const snapshot = DB.getSnapshot(); + if (snapshot?.tags) { + snapshot.tags = snapshot.tags.filter((it) => it._id !== tagId); + } + void Settings.update(); return { status: 1, message: `Tag removed` }; }, @@ -151,6 +157,18 @@ const actionModals: Record = { }; } + const matchingTag = getTagFromSnapshot(tagId); + + if (matchingTag !== undefined) { + matchingTag.personalBests = { + time: {}, + words: {}, + quote: {}, + zen: {}, + custom: {}, + }; + } + void Settings.update(); return { status: 1, message: `Tag PB cleared` }; },