Skip to content

Commit c76f8dc

Browse files
authored
fix: add input validation for new filter preset modal (@AmanDevelops) (monkeytypegame#7073)
### Description Added input validation for `New filter preset` modal in frontend and display appropriate indicators in realtime #### Updated UI <img width="610" height="333" alt="Screenshot From 2025-11-03 02-08-40" src="https://github.com/user-attachments/assets/0f0f3628-5e61-4de9-8100-d96475f8bf08" /> <img width="610" height="333" alt="Screenshot From 2025-11-03 02-08-26" src="https://github.com/user-attachments/assets/df0fd478-7d54-4a99-b7ed-198f1f9ebae6" /> ### Checks - [ ] Adding quotes? - [ ] Make sure to include translations for the quotes in the description (or another comment) so we can verify their content. - [ ] Adding a language? - Make sure to follow the [languages documentation](https://github.com/monkeytypegame/monkeytype/blob/master/docs/LANGUAGES.md) - [ ] Add language to `packages/schemas/src/languages.ts` - [ ] Add language to exactly one group in `frontend/src/ts/constants/languages.ts` - [ ] Add language json file to `frontend/static/languages` - [ ] Adding a theme? - Make sure to follow the [themes documentation](https://github.com/monkeytypegame/monkeytype/blob/master/docs/THEMES.md) - [ ] Add theme to `packages/schemas/src/themes.ts` - [ ] Add theme to `frontend/src/ts/constants/themes.ts` - [ ] Add theme css file to `frontend/static/themes` - [ ] Add some screenshot of the theme, especially with different test settings (colorful, flip colors) to your pull request - [ ] Adding a layout? - [ ] Make sure to follow the [layouts documentation](https://github.com/monkeytypegame/monkeytype/blob/master/docs/LAYOUTS.md) - [ ] Add layout to `packages/schemas/src/layouts.ts` - [ ] Add layout json file to `frontend/static/layouts` - [ ] Adding a font? - Make sure to follow the [themes documentation](https://github.com/monkeytypegame/monkeytype/blob/master/docs/FONTS.md) - [ ] Add font file to `frontend/static/webfonts` - [ ] Add font to `packages/schemas/src/fonts.ts` - [ ] Add font to `frontend/src/ts/constants/fonts.ts` - [X] Check if any open issues are related to this PR; if so, be sure to tag them below. - [X] Make sure the PR title follows the Conventional Commits standard. (https://www.conventionalcommits.org for more info) - [X] Make sure to include your GitHub username prefixed with @ inside parentheses at the end of the PR title. <!-- label(optional scope): pull request title (@your_github_username) --> <!-- I know I know they seem boring but please do them, they help us and you will find out it also helps you.--> Closes monkeytypegame#7063 <!-- the issue(s) your PR resolves if any (delete if that is not the case) --> <!-- please also reference any issues and or PRs related to your pull request --> <!-- Also remove it if you are not following any issues. --> <!-- pro tip: you can mention an issue, PR, or discussion on GitHub by referencing its hash number e.g: [monkeytypegame#1234](monkeytypegame#1234) --> <!-- pro tip: you can press . (dot or period) in the code tab of any GitHub repo to get access to GitHub's VS Code web editor Enjoy! :) -->
1 parent 12e3d65 commit c76f8dc

File tree

4 files changed

+34
-55
lines changed

4 files changed

+34
-55
lines changed

frontend/src/html/popups.html

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,14 +1276,6 @@
12761276
</div>
12771277
</dialog>
12781278

1279-
<dialog id="newFilterPresetModal" class="modalWrapper hidden">
1280-
<form class="modal">
1281-
<div class="title">New filter preset</div>
1282-
<input type="text" value="" />
1283-
<button>add</button>
1284-
</form>
1285-
</dialog>
1286-
12871279
<dialog id="editProfileModal" class="modalWrapper hidden">
12881280
<form class="modal">
12891281
<div class="title">Edit Profile</div>

frontend/src/styles/popups.scss

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,12 +1654,6 @@ body.darkMode {
16541654
}
16551655
}
16561656

1657-
#newFilterPresetModal {
1658-
.modal {
1659-
max-width: 450px;
1660-
}
1661-
}
1662-
16631657
#editProfileModal {
16641658
.modal {
16651659
max-width: 600px;

frontend/src/ts/elements/account/result-filters.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ function addFilterPresetToSnapshot(filter: ResultFilters): void {
179179
}
180180

181181
// callback function called by popup once user inputs name
182-
export async function createFilterPreset(name: string): Promise<void> {
182+
export async function createFilterPreset(
183+
name: string
184+
): Promise<number | undefined> {
183185
name = name.replace(/ /g, "_");
184186
Loader.show();
185187
const result = await Ape.users.addResultFilterPreset({
@@ -189,13 +191,10 @@ export async function createFilterPreset(name: string): Promise<void> {
189191
if (result.status === 200) {
190192
addFilterPresetToSnapshot({ ...filters, name, _id: result.body.data });
191193
void updateFilterPresets();
192-
Notifications.add("Filter preset created", 1);
194+
return 1;
193195
} else {
194-
Notifications.add(
195-
"Error creating filter preset: " + result.body.message,
196-
-1
197-
);
198196
console.log("error creating filter preset: " + result.body.message);
197+
return 0;
199198
}
200199
}
201200

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,35 @@
1+
import { ResultFiltersSchema } from "@monkeytype/schemas/users";
12
import { createFilterPreset } from "../elements/account/result-filters";
2-
import AnimatedModal, { ShowOptions } from "../utils/animated-modal";
3-
import * as Notifications from "../elements/notifications";
3+
import { SimpleModal } from "../utils/simple-modal";
44

5-
export function show(showOptions?: ShowOptions): void {
6-
void modal.show({
7-
...showOptions,
8-
focusFirstInput: true,
9-
beforeAnimation: async (modalEl) => {
10-
(modalEl.querySelector("input") as HTMLInputElement).value = "";
11-
},
12-
});
13-
}
14-
15-
function hide(clearChain = false): void {
16-
void modal.hide({
17-
clearModalChain: clearChain,
18-
});
5+
export function show(): void {
6+
newFilterPresetModal.show(undefined, {});
197
}
208

21-
function apply(): void {
22-
const name = $("#newFilterPresetModal input").val() as string;
23-
if (name === "") {
24-
Notifications.add("Name cannot be empty", 0);
25-
return;
26-
}
27-
void createFilterPreset(name);
28-
hide(true);
29-
}
30-
31-
async function setup(modalEl: HTMLElement): Promise<void> {
32-
modalEl.addEventListener("submit", (e) => {
33-
e.preventDefault();
34-
apply();
35-
});
36-
}
9+
const newFilterPresetModal = new SimpleModal({
10+
id: "newFilterPresetModal",
11+
title: "New Filter Preset",
12+
inputs: [
13+
{
14+
placeholder: "Preset Name",
15+
type: "text",
16+
initVal: "",
17+
validation: {
18+
schema: ResultFiltersSchema.shape.name,
19+
},
20+
},
21+
],
22+
buttonText: "add",
23+
onlineOnly: true,
24+
execFn: async (_thisPopup, name) => {
25+
const status = await createFilterPreset(name);
3726

38-
const modal = new AnimatedModal({
39-
dialogId: "newFilterPresetModal",
40-
setup,
27+
if (status === 1) {
28+
return { status: 1, message: "Filter preset created" };
29+
} else {
30+
let status: -1 | 0 | 1 = -1;
31+
let message: string = "Error creating filter preset";
32+
return { status, message, alwaysHide: true };
33+
}
34+
},
4135
});

0 commit comments

Comments
 (0)