Skip to content

Commit c97b307

Browse files
committed
Refactor profile management to not hide controls on duplicate entry error
1 parent c5f97f4 commit c97b307

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

popup-page-scripts/popup-page-profiles.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ async function addNewEmptyProfile(profileName) {
8989
if (trimmedProfileName === "") {
9090
showToast('Profile name cannot be empty.', 'error');
9191
logToGUIConsole('Profile creation failed: Empty name provided.');
92-
return;
92+
return false;
9393
}
9494

9595
// Check if profile name already exists
9696
const existingProfiles = Array.from(profileSelect.options).map(option => option.value);
9797
if (existingProfiles.includes(trimmedProfileName)) {
9898
showToast('A profile with this name already exists.', 'error');
9999
logToGUIConsole(`Profile creation failed: "${trimmedProfileName}" already exists.`);
100-
return;
100+
return false;
101101
}
102102

103103
try {
@@ -114,9 +114,11 @@ async function addNewEmptyProfile(profileName) {
114114
await switchProfile(trimmedProfileName);
115115
showToast(`Profile "${trimmedProfileName}" added successfully.`, 'success');
116116
logToGUIConsole(`Created new empty profile: ${trimmedProfileName}`);
117+
return true;
117118
} catch (error) {
118119
showToast(`Error creating profile: ${error.message}`, 'error');
119120
logToGUIConsole(`Error creating profile: ${error.message}`);
121+
return false;
120122
}
121123
}
122124

@@ -134,15 +136,15 @@ async function copyCurrentProfile(newProfileName) {
134136
if (trimmedProfileName === "") {
135137
showToast('Profile name cannot be empty.', 'error');
136138
logToGUIConsole('Profile copy failed: Empty name provided.');
137-
return;
139+
return false;
138140
}
139141

140142
// Check if profile name already exists
141143
const existingProfiles = Array.from(profileSelect.options).map(option => option.value);
142144
if (existingProfiles.includes(trimmedProfileName)) {
143145
showToast('A profile with this name already exists.', 'error');
144146
logToGUIConsole(`Profile copy failed: "${trimmedProfileName}" already exists.`);
145-
return;
147+
return false;
146148
}
147149

148150
try {
@@ -161,9 +163,11 @@ async function copyCurrentProfile(newProfileName) {
161163
await switchProfile(trimmedProfileName);
162164
showToast(`Profile duplicated as "${trimmedProfileName}" successfully.`, 'success');
163165
logToGUIConsole(`Copied profile to new profile: ${trimmedProfileName}`);
166+
return true;
164167
} catch (error) {
165168
showToast(`Error copying profile: ${error.message}`, 'error');
166169
logToGUIConsole(`Error copying profile: ${error.message}`);
170+
return false;
167171
}
168172
}
169173

@@ -201,3 +205,4 @@ async function deleteCurrentProfile() {
201205
logToGUIConsole(`Error deleting profile: ${error.message}`);
202206
}
203207
}
208+

popup-page-scripts/popup-page-script.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)