Skip to content

Commit fbec8db

Browse files
Update dashboard.js
1 parent 0a26123 commit fbec8db

File tree

1 file changed

+46
-68
lines changed

1 file changed

+46
-68
lines changed

static/dashboard.js

Lines changed: 46 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function loadEmailAccounts() {
3232
}
3333
}
3434

35-
// Update destination dropdown with email accounts
35+
// Update destination dropdown with email accounts AND custom option
3636
function updateDestinationDropdown() {
3737
const select = document.getElementById('destination');
3838
if (!select) return;
@@ -58,47 +58,33 @@ function updateDestinationDropdown() {
5858
});
5959
}
6060

61-
// Handle destination type radio buttons (if they exist)
62-
const radioButtons = document.querySelectorAll('input[name="destination_type"]');
63-
radioButtons.forEach(radio => {
64-
radio.addEventListener('change', function() {
65-
updateDestinationVisibility();
66-
});
67-
});
68-
69-
// Initial visibility update
70-
updateDestinationVisibility();
71-
}
72-
73-
// Update visibility based on destination type selection
74-
function updateDestinationVisibility() {
75-
const destinationType = document.querySelector('input[name="destination_type"]:checked');
76-
const destSelect = document.getElementById('destination');
77-
const customGroup = document.getElementById('custom-destination-group');
78-
const customInput = document.getElementById('custom-destination'); // FIXED: Using kebab-case
79-
80-
if (!destinationType) return;
81-
82-
const isCustom = destinationType.value === 'custom';
83-
84-
// Handle destination select
85-
if (destSelect) {
86-
destSelect.style.display = isCustom ? 'none' : 'block';
87-
destSelect.required = !isCustom;
88-
}
89-
90-
// Handle custom destination group
91-
if (customGroup) {
92-
customGroup.style.display = isCustom ? 'block' : 'none';
93-
}
94-
95-
// Handle custom destination input
96-
if (customInput) {
97-
customInput.required = isCustom;
98-
if (!isCustom) {
61+
// Add separator
62+
const separator = document.createElement('option');
63+
separator.disabled = true;
64+
separator.textContent = '──────────────';
65+
select.appendChild(separator);
66+
67+
// Add custom option
68+
const customOption = document.createElement('option');
69+
customOption.value = 'custom';
70+
customOption.textContent = 'Custom destination...';
71+
select.appendChild(customOption);
72+
73+
// Add change event listener
74+
select.addEventListener('change', function() {
75+
const customInput = document.getElementById('custom-destination');
76+
if (!customInput) return;
77+
78+
if (this.value === 'custom') {
79+
customInput.style.display = 'block';
80+
customInput.required = true;
81+
customInput.focus();
82+
} else {
83+
customInput.style.display = 'none';
84+
customInput.required = false;
9985
customInput.value = '';
10086
}
101-
}
87+
});
10288
}
10389

10490
// Load forwarders from API
@@ -156,7 +142,7 @@ function displayForwarders() {
156142

157143
const actionsCell = document.createElement('td');
158144
const deleteBtn = document.createElement('button');
159-
deleteBtn.className = 'btn-danger btn-sm';
145+
deleteBtn.className = 'btn-danger';
160146
deleteBtn.textContent = 'Delete';
161147
deleteBtn.onclick = () => deleteForwarder(forwarder.address);
162148
actionsCell.appendChild(deleteBtn);
@@ -173,43 +159,41 @@ async function createForwarder(event) {
173159
event.preventDefault();
174160

175161
const form = event.target;
176-
const addressInput = form.querySelector('#alias'); // FIXED: Changed from #address to #alias
177-
const destinationType = document.querySelector('input[name="destination_type"]:checked');
162+
const addressInput = form.querySelector('#alias');
178163
const destinationSelect = form.querySelector('#destination');
179-
const customDestInput = form.querySelector('#custom-destination'); // FIXED: Using kebab-case
164+
const customDestInput = form.querySelector('#custom-destination');
180165
const submitButton = form.querySelector('button[type="submit"]');
181166

167+
// Validate alias
168+
if (!addressInput || !addressInput.value.trim()) {
169+
showMessage('Please enter an alias', 'error');
170+
return;
171+
}
172+
182173
// Get the actual destination
183174
let destination;
184175

185-
if (destinationType && destinationType.value === 'custom') {
176+
if (destinationSelect.value === 'custom') {
186177
// Using custom destination
187178
if (!customDestInput || !customDestInput.value.trim()) {
188179
showMessage('Please enter a custom destination', 'error');
189-
if (customDestInput) customDestInput.focus();
180+
customDestInput.focus();
190181
return;
191182
}
192183

193184
destination = customDestInput.value.trim();
194185

195186
// Validate the destination
196187
if (!isValidDestination(destination)) {
197-
showMessage('Please enter a valid email address or special destination (e.g., :blackhole:, :fail:, |/path/to/script)', 'error');
188+
showMessage('Please enter a valid email address or special destination (e.g., :blackhole:, :fail:)', 'error');
198189
customDestInput.focus();
199190
return;
200191
}
201-
} else {
192+
} else if (destinationSelect.value) {
202193
// Using existing email from dropdown
203-
if (!destinationSelect || !destinationSelect.value) {
204-
showMessage('Please select a destination email', 'error');
205-
return;
206-
}
207194
destination = destinationSelect.value;
208-
}
209-
210-
// Validate alias
211-
if (!addressInput || !addressInput.value.trim()) {
212-
showMessage('Please enter an alias', 'error');
195+
} else {
196+
showMessage('Please select a destination', 'error');
213197
return;
214198
}
215199

@@ -235,8 +219,11 @@ async function createForwarder(event) {
235219
// Clear form
236220
form.reset();
237221

238-
// Reset visibility
239-
updateDestinationVisibility();
222+
// Hide custom input
223+
if (customDestInput) {
224+
customDestInput.style.display = 'none';
225+
customDestInput.required = false;
226+
}
240227

241228
// Reload forwarders
242229
await loadForwarders();
@@ -353,15 +340,6 @@ document.addEventListener('DOMContentLoaded', function() {
353340
} else {
354341
console.error('Create forwarder form not found');
355342
}
356-
357-
// Set up manual refresh button (if you want one)
358-
const refreshBtn = document.getElementById('refreshBtn');
359-
if (refreshBtn) {
360-
refreshBtn.addEventListener('click', () => {
361-
loadForwarders();
362-
showMessage('Forwarders refreshed', 'info');
363-
});
364-
}
365343
});
366344

367345
// Utility function to escape HTML (prevent XSS)

0 commit comments

Comments
 (0)