Show validation errors in domain creation form

Frontend now displays backend error messages when domain creation fails.
This commit is contained in:
Christoph Haas 2026-03-22 23:54:34 +01:00
parent 9dab3bc12c
commit a8aaa08d29

View file

@ -18,6 +18,7 @@
let loading = $state(true);
let showModal = $state(false);
let newDomain = $state({ name: '' });
let createError = $state('');
async function fetchWithAuth(url: string) {
const token = localStorage.getItem('token');
@ -48,18 +49,23 @@
}
async function createDomain() {
createError = '';
try {
const res = await fetch('/api/domains', {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${localStorage.getItem('token')}` },
body: JSON.stringify(newDomain)
});
const data = await res.json();
if (res.ok) {
showModal = false;
newDomain = { name: '' };
await loadData();
} else {
createError = data.error || 'Failed to create domain';
}
} catch (e) {
createError = 'Failed to create domain';
console.error('Failed to create domain:', e);
}
}
@ -134,7 +140,7 @@
{/if}
{#if showModal}
<button class="modal-backdrop" onclick={() => showModal = false} aria-label="Close modal"></button>
<button class="modal-backdrop" onclick={() => { showModal = false; createError = ''; }} aria-label="Close modal"></button>
<div class="modal">
<h3>Add Domain</h3>
<form onsubmit={(e) => { e.preventDefault(); createDomain(); }}>
@ -142,8 +148,11 @@
Domain name:
<input type="text" bind:value={newDomain.name} placeholder="example.org" required />
</label>
{#if createError}
<p class="error">{createError}</p>
{/if}
<div class="actions">
<button type="button" onclick={() => showModal = false}>Cancel</button>
<button type="button" onclick={() => { showModal = false; createError = ''; }}>Cancel</button>
<button type="submit">Create</button>
</div>
</form>
@ -274,6 +283,14 @@
border-radius: 4px;
}
.modal .error {
color: #e74c3c;
background: #fdecea;
padding: 0.5rem;
border-radius: 4px;
margin: 0;
}
.actions {
display: flex;
gap: 1rem;