@@ -209,7 +209,7 @@ document.addEventListener('DOMContentLoaded', () => {
209209 } ) ;
210210
211211 // Save Add Profile Button Click
212- saveAddProfileButton . addEventListener ( 'click' , ( ) => {
212+ saveAddProfileButton . addEventListener ( 'click' , async ( ) => {
213213 const profileName = addProfileInput . value ;
214214 if ( profileName . trim ( ) === "" ) {
215215 addProfileInput . classList . add ( 'input-error' ) ;
@@ -220,21 +220,28 @@ document.addEventListener('DOMContentLoaded', () => {
220220 return ;
221221 }
222222 addProfileInput . classList . remove ( 'input-error' ) ;
223- addNewEmptyProfile ( profileName ) ;
224- // Reset and hide input
225- addProfileInput . value = '' ;
226- addProfileInput . style . borderColor = '' ;
227- addProfileContainer . style . display = 'none' ;
228- // Show both "Add Profile" and "Duplicate Profile" buttons
229- addProfileButton . style . display = 'inline-block' ;
230- copyProfileButton . style . display = 'inline-block' ;
231- deleteProfileButton . style . display = 'inline-block' ; // Show delete button after add
232- profileSelect . disabled = false ; // Unlock profile selector
233- currentProfileLabel . style . display = 'none' ;
223+ const success = await addNewEmptyProfile ( profileName ) ;
224+
225+ if ( success ) {
226+ // Reset and hide input
227+ addProfileInput . value = '' ;
228+ addProfileInput . style . borderColor = '' ;
229+ addProfileContainer . style . display = 'none' ;
230+ // Show both "Add Profile" and "Duplicate Profile" buttons
231+ addProfileButton . style . display = 'inline-block' ;
232+ copyProfileButton . style . display = 'inline-block' ;
233+ deleteProfileButton . style . display = 'inline-block' ; // Show delete button after add
234+ profileSelect . disabled = false ; // Unlock profile selector
235+ currentProfileLabel . style . display = 'none' ;
236+ } else {
237+ // On failure (e.g., duplicate name), keep UI open for correction
238+ addProfileInput . classList . add ( 'input-error' ) ;
239+ addProfileInput . style . borderColor = 'var(--danger-color)' ;
240+ }
234241 } ) ;
235242
236243 // Save Copy Profile Button Click
237- saveCopyProfileButton . addEventListener ( 'click' , ( ) => {
244+ saveCopyProfileButton . addEventListener ( 'click' , async ( ) => {
238245 const newProfileName = copyProfileInput . value ;
239246 if ( newProfileName . trim ( ) === "" ) {
240247 copyProfileInput . classList . add ( 'input-error' ) ;
@@ -245,17 +252,24 @@ document.addEventListener('DOMContentLoaded', () => {
245252 return ;
246253 }
247254 copyProfileInput . classList . remove ( 'input-error' ) ;
248- copyCurrentProfile ( newProfileName ) ;
249- // Reset and hide input
250- copyProfileInput . value = '' ;
251- copyProfileInput . style . borderColor = '' ;
252- copyProfileContainer . style . display = 'none' ;
253- // Show both "Add Profile" and "Duplicate Profile" buttons
254- copyProfileButton . style . display = 'inline-block' ;
255- addProfileButton . style . display = 'inline-block' ;
256- deleteProfileButton . style . display = 'inline-block' ; // Show delete button after copy
257- profileSelect . disabled = false ; // Unlock profile selector
258- currentProfileLabel . style . display = 'none' ;
255+ const success = await copyCurrentProfile ( newProfileName ) ;
256+
257+ if ( success ) {
258+ // Reset and hide input
259+ copyProfileInput . value = '' ;
260+ copyProfileInput . style . borderColor = '' ;
261+ copyProfileContainer . style . display = 'none' ;
262+ // Show both "Add Profile" and "Duplicate Profile" buttons
263+ copyProfileButton . style . display = 'inline-block' ;
264+ addProfileButton . style . display = 'inline-block' ;
265+ deleteProfileButton . style . display = 'inline-block' ; // Show delete button after copy
266+ profileSelect . disabled = false ; // Unlock profile selector
267+ currentProfileLabel . style . display = 'none' ;
268+ } else {
269+ // On failure (e.g., duplicate name), keep UI open for correction
270+ copyProfileInput . classList . add ( 'input-error' ) ;
271+ copyProfileInput . style . borderColor = 'var(--danger-color)' ;
272+ }
259273 } ) ;
260274
261275 // Delete Profile Button Click
0 commit comments