@@ -6,7 +6,11 @@ import * as Settings from "../pages/settings";
66import * as Notifications from "../elements/notifications" ;
77import * as ConnectionState from "../states/connection" ;
88import AnimatedModal from "../utils/animated-modal" ;
9- import { PresetType , PresetTypeSchema } from "@monkeytype/schemas/presets" ;
9+ import {
10+ PresetNameSchema ,
11+ PresetType ,
12+ PresetTypeSchema ,
13+ } from "@monkeytype/schemas/presets" ;
1014import { getPreset } from "../controllers/preset-controller" ;
1115import {
1216 ConfigGroupName ,
@@ -17,6 +21,10 @@ import {
1721} from "@monkeytype/schemas/configs" ;
1822import { getDefaultConfig } from "../constants/default-config" ;
1923import { SnapshotPreset } from "../constants/default-snapshot" ;
24+ import {
25+ ValidatedHtmlInputElement ,
26+ validateWithIndicator ,
27+ } from "../elements/input-validation" ;
2028
2129const state = {
2230 presetType : "full" as PresetType ,
@@ -26,6 +34,8 @@ const state = {
2634 setPresetToCurrent : false ,
2735} ;
2836
37+ let presetNameEl : ValidatedHtmlInputElement | null = null ;
38+
2939export function show ( action : string , id ?: string , name ?: string ) : void {
3040 if ( ! ConnectionState . get ( ) ) {
3141 Notifications . add ( "You are offline" , 0 , {
@@ -39,11 +49,22 @@ export function show(action: string, id?: string, name?: string): void {
3949 beforeAnimation : async ( ) => {
4050 $ ( "#editPresetModal .modal .text" ) . addClass ( "hidden" ) ;
4151 addCheckBoxes ( ) ;
52+ if ( ! presetNameEl ) {
53+ presetNameEl = validateWithIndicator (
54+ document . querySelector (
55+ "#editPresetModal .modal input"
56+ ) as HTMLInputElement ,
57+ {
58+ schema : PresetNameSchema ,
59+ }
60+ ) ;
61+ }
4262 if ( action === "add" ) {
4363 $ ( "#editPresetModal .modal" ) . attr ( "data-action" , "add" ) ;
4464 $ ( "#editPresetModal .modal .popupTitle" ) . html ( "Add new preset" ) ;
4565 $ ( "#editPresetModal .modal .submit" ) . html ( `add` ) ;
46- $ ( "#editPresetModal .modal input" ) . val ( "" ) ;
66+ presetNameEl ?. setValue ( null ) ;
67+ presetNameEl ?. parentElement ?. classList . remove ( "hidden" ) ;
4768 $ ( "#editPresetModal .modal input" ) . removeClass ( "hidden" ) ;
4869 $ (
4970 "#editPresetModal .modal label.changePresetToCurrentCheckbox"
@@ -57,7 +78,9 @@ export function show(action: string, id?: string, name?: string): void {
5778 $ ( "#editPresetModal .modal" ) . attr ( "data-preset-id" , id ) ;
5879 $ ( "#editPresetModal .modal .popupTitle" ) . html ( "Edit preset" ) ;
5980 $ ( "#editPresetModal .modal .submit" ) . html ( `save` ) ;
60- $ ( "#editPresetModal .modal input" ) . val ( name ) ;
81+ presetNameEl ?. setValue ( name ) ;
82+ presetNameEl ?. parentElement ?. classList . remove ( "hidden" ) ;
83+
6184 $ ( "#editPresetModal .modal input" ) . removeClass ( "hidden" ) ;
6285 $ (
6386 "#editPresetModal .modal label.changePresetToCurrentCheckbox"
@@ -85,6 +108,7 @@ export function show(action: string, id?: string, name?: string): void {
85108 $ ( "#editPresetModal .modal .inputs" ) . addClass ( "hidden" ) ;
86109 $ ( "#editPresetModal .modal .presetType" ) . addClass ( "hidden" ) ;
87110 $ ( "#editPresetModal .modal .presetNameTitle" ) . addClass ( "hidden" ) ;
111+ presetNameEl ?. parentElement ?. classList . add ( "hidden" ) ;
88112 }
89113 updateUI ( ) ;
90114 } ,
@@ -238,6 +262,11 @@ async function apply(): Promise<void> {
238262 return ;
239263 }
240264
265+ if ( presetNameEl ?. isValid ( ) === false ) {
266+ Notifications . add ( "Preset name is not valid" , 0 ) ;
267+ return ;
268+ }
269+
241270 hide ( ) ;
242271
243272 Loader . show ( ) ;
0 commit comments