|
1 | | -import { createFilterPreset } from "../elements/account/result-filters"; |
2 | | -import AnimatedModal, { ShowOptions } from "../utils/animated-modal"; |
3 | | -import * as Notifications from "../elements/notifications"; |
4 | | -import { InputIndicator } from "../elements/input-indicator"; |
5 | 1 | import { ResultFiltersSchema } from "@monkeytype/schemas/users"; |
| 2 | +import { createFilterPreset } from "../elements/account/result-filters"; |
| 3 | +import { SimpleModal } from "../utils/simple-modal"; |
6 | 4 |
|
7 | | -export function show(showOptions?: ShowOptions): void { |
8 | | - void modal.show({ |
9 | | - ...showOptions, |
10 | | - focusFirstInput: true, |
11 | | - beforeAnimation: async (modalEl) => { |
12 | | - (modalEl.querySelector("input") as HTMLInputElement).value = ""; |
13 | | - |
14 | | - const indicator = $(modalEl).data("indicator") as |
15 | | - | InputIndicator |
16 | | - | undefined; |
17 | | - if (indicator) { |
18 | | - indicator.hide(); |
19 | | - } |
20 | | - }, |
21 | | - }); |
22 | | -} |
23 | | - |
24 | | -function hide(clearChain = false): void { |
25 | | - void modal.hide({ |
26 | | - clearModalChain: clearChain, |
27 | | - }); |
28 | | -} |
29 | | - |
30 | | -function apply(): void { |
31 | | - const name = $("#newFilterPresetModal input").val() as string; |
32 | | - if (name === "") { |
33 | | - Notifications.add("Name cannot be empty", 0); |
34 | | - return; |
35 | | - } |
36 | | - void createFilterPreset(name); |
37 | | - hide(true); |
| 5 | +export function show(): void { |
| 6 | + newFilterPresetModal.show(undefined, {}); |
38 | 7 | } |
39 | 8 |
|
40 | | -function addValidation(element: JQuery, schema: Zod.Schema): InputIndicator { |
41 | | - const indicator = new InputIndicator(element, { |
42 | | - valid: { icon: "fa-check", level: 1 }, |
43 | | - invalid: { icon: "fa-times", level: -1 }, |
44 | | - }); |
45 | | - |
46 | | - element.on("input", (event) => { |
47 | | - const value = (event.target as HTMLInputElement).value; |
48 | | - if (value === undefined || value === "") { |
49 | | - indicator.hide(); |
50 | | - return; |
51 | | - } |
52 | | - const validationResult = schema.safeParse(value); |
53 | | - if (!validationResult.success) { |
54 | | - indicator.show( |
55 | | - "invalid", |
56 | | - validationResult.error.errors.map((err) => err.message).join(", ") |
57 | | - ); |
58 | | - return; |
| 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); |
| 26 | + |
| 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 }; |
59 | 33 | } |
60 | | - indicator.show("valid"); |
61 | | - }); |
62 | | - return indicator; |
63 | | -} |
64 | | - |
65 | | -async function setup(modalEl: HTMLElement): Promise<void> { |
66 | | - modalEl.addEventListener("submit", (e) => { |
67 | | - e.preventDefault(); |
68 | | - apply(); |
69 | | - }); |
70 | | - |
71 | | - const inputElement = $(modalEl).find("input"); |
72 | | - |
73 | | - const indicator = addValidation(inputElement, ResultFiltersSchema.shape.name); |
74 | | - $(modalEl).data("indicator", indicator); |
75 | | -} |
76 | | - |
77 | | -const modal = new AnimatedModal({ |
78 | | - dialogId: "newFilterPresetModal", |
79 | | - setup, |
| 34 | + }, |
80 | 35 | }); |
0 commit comments