Files
HostKeeper/app/backend/views/host_modal.html
T
swanadiva a2412b7cd2 fix: host modal — single POST endpoint for create+edit, remove hx-put conflict
- hx-put="" (empty) was causing HTMX to send wrong requests
- Now use single hx-post="/hosts" with hidden hostId field
- Server checks hostId: empty → create, present → update
- UpdateHost reads ID from both URL param and form data
2026-07-07 15:56:54 +07:00

186 lines
12 KiB
HTML

{{define "host_modal"}}
<div id="host-modal" class="fixed inset-0 z-50" x-data="hostForm()" x-show="open" x-cloak
@open-modal.window="reset(); open = true"
@edit-host.window="populate($event.detail); open = true"
@keydown.escape.window="open = false">
<div class="absolute inset-0 bg-black/40 backdrop-blur-sm" @click="open = false"></div>
<div class="absolute inset-0 flex items-center justify-center p-4" x-show="open" x-transition>
<div class="bg-white rounded-2xl max-w-lg w-full border border-outline-variant/30 shadow-2xl max-h-[90vh] overflow-y-auto">
<div class="flex justify-between items-center border-b border-outline-variant/30 p-6 pb-4">
<h3 class="font-bold text-lg text-on-surface" x-text="editing ? 'Edit Host' : 'Add Host'"></h3>
<button @click="open = false"
class="text-outline hover:text-primary cursor-pointer p-1 rounded-lg">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
</button>
</div>
<form hx-post="/hosts"
hx-target="#host-list" hx-swap="innerHTML"
@submit="open = false; $dispatch('toast-message', { message: editing ? 'Host updated' : 'Host added' })"
class="p-6 space-y-4 text-sm">
<input type="hidden" name="hostId" :value="editing ? hostId : ''">
<div class="grid grid-cols-2 gap-4">
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">Host Identifier</label>
<input type="text" name="name" x-model="form.name" required placeholder="e.g. prod-db-replica"
class="w-full bg-surface-container-low border border-outline-variant/40 rounded-lg py-2.5 px-4 focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none transition-all font-medium text-on-surface">
</div>
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">IP Address / Domain</label>
<input type="text" name="ip" x-model="form.ip" required placeholder="e.g. 10.0.12.19"
class="w-full bg-surface-container-low border border-outline-variant/40 rounded-lg py-2.5 px-4 focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none transition-all font-mono text-on-surface">
</div>
</div>
<div class="grid grid-cols-2 gap-4">
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">Hostname (SSH)</label>
<input type="text" name="hostname" x-model="form.hostname" placeholder="e.g. prod.example.com"
class="w-full bg-surface-container-low border border-outline-variant/40 rounded-lg py-2.5 px-4 focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none transition-all font-mono text-on-surface text-xs">
</div>
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">SSH Port</label>
<input type="number" name="port" x-model="form.port" min="1" max="65535" placeholder="22"
class="w-full bg-surface-container-low border border-outline-variant/40 rounded-lg py-2.5 px-4 focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none transition-all font-mono text-on-surface text-xs">
</div>
</div>
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">SSH Username</label>
<input type="text" name="username" x-model="form.username" placeholder="root"
class="w-full bg-surface-container-low border border-outline-variant/40 rounded-lg py-2.5 px-4 focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none transition-all font-medium text-on-surface">
</div>
<div class="grid grid-cols-2 gap-4">
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">Operating System</label>
<select name="os" x-model="form.os"
class="bg-surface-container-low border border-outline-variant/40 rounded-lg py-2 px-3 focus:outline-none focus:border-primary text-sm text-on-surface">
<option value="Ubuntu 22.04">Ubuntu 22.04</option>
<option value="Ubuntu 24.04">Ubuntu 24.04</option>
<option value="Debian 12">Debian 12</option>
<option value="Alpine 3.19">Alpine 3.19</option>
<option value="Fedora 39">Fedora 39</option>
<option value="Rocky 9">Rocky 9</option>
<option value="CentOS 8">CentOS 8</option>
<option value="Other">Other</option>
</select>
</div>
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">Provider</label>
<select name="provider" x-model="form.provider"
class="bg-surface-container-low border border-outline-variant/40 rounded-lg py-2 px-3 focus:outline-none focus:border-primary text-sm text-on-surface">
<option value="AWS US-East">AWS Cloud</option>
<option value="AWS EU-West">AWS EU-West</option>
<option value="DigitalOcean">DigitalOcean</option>
<option value="Hetzner">Hetzner</option>
<option value="GCP US-Central">GCP Cloud</option>
<option value="Vultr">Vultr</option>
<option value="Linode">Linode</option>
<option value="Local">Local</option>
</select>
</div>
</div>
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">Host Purpose / Type</label>
<div class="grid grid-cols-3 gap-2">
<button type="button" @click="form.type = 'api'"
:class="form.type === 'api' ? 'bg-primary text-white border-primary' : 'bg-surface-container-low border-outline-variant/40 text-on-surface-variant hover:bg-surface-container'"
class="py-2.5 px-3 border rounded-lg font-bold text-xs uppercase tracking-wider transition-all">API Node</button>
<button type="button" @click="form.type = 'db'"
:class="form.type === 'db' ? 'bg-primary text-white border-primary' : 'bg-surface-container-low border-outline-variant/40 text-on-surface-variant hover:bg-surface-container'"
class="py-2.5 px-3 border rounded-lg font-bold text-xs uppercase tracking-wider transition-all">DB Node</button>
<button type="button" @click="form.type = 'web'"
:class="form.type === 'web' ? 'bg-primary text-white border-primary' : 'bg-surface-container-low border-outline-variant/40 text-on-surface-variant hover:bg-surface-container'"
class="py-2.5 px-3 border rounded-lg font-bold text-xs uppercase tracking-wider transition-all">Web Node</button>
</div>
<input type="hidden" name="type" :value="form.type">
</div>
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">Authentication</label>
<div class="grid grid-cols-2 gap-2">
<button type="button" @click="form.authType = 'key'"
:class="form.authType === 'key' ? 'bg-primary text-white border-primary' : 'bg-surface-container-low border-outline-variant/40 text-on-surface-variant hover:bg-surface-container'"
class="py-2 px-3 border rounded-lg font-bold text-xs uppercase tracking-wider transition-all flex items-center justify-center gap-1.5">
<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.75 5.25a3 3 0 013 3m3 0a6 6 0 01-7.029 5.912c-.563-.097-1.159.026-1.563.43L10.5 17.25H8.25v2.25H6v2.25H2.25v-2.818c0-.597.237-1.17.659-1.591l6.499-6.499c.404-.404.527-1 .43-1.563A6 6 0 1121.75 8.25z"/></svg>
SSH Key
</button>
<button type="button" @click="form.authType = 'password'"
:class="form.authType === 'password' ? 'bg-primary text-white border-primary' : 'bg-surface-container-low border-outline-variant/40 text-on-surface-variant hover:bg-surface-container'"
class="py-2 px-3 border rounded-lg font-bold text-xs uppercase tracking-wider transition-all flex items-center justify-center gap-1.5">
<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16.5 10.5V6.75a4.5 4.5 0 10-9 0v3.75m-.75 11.25h10.5a2.25 2.25 0 002.25-2.25v-6.75a2.25 2.25 0 00-2.25-2.25H6.75a2.25 2.25 0 00-2.25 2.25v6.75a2.25 2.25 0 002.25 2.25z"/></svg>
Password
</button>
</div>
<input type="hidden" name="authType" :value="form.authType">
</div>
<div x-show="form.authType === 'password'" x-cloak class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">Password</label>
<input type="password" name="password" x-model="form.password" placeholder="SSH password"
class="w-full bg-surface-container-low border border-outline-variant/40 rounded-lg py-2.5 px-4 focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none transition-all font-mono text-on-surface text-xs">
</div>
<div class="flex flex-col gap-1.5">
<label class="text-xs font-bold text-on-surface-variant uppercase">Notes</label>
<textarea name="notes" x-model="form.notes" rows="2" placeholder="Optional notes about this host"
class="w-full bg-surface-container-low border border-outline-variant/40 rounded-lg py-2.5 px-4 focus:ring-2 focus:ring-primary/20 focus:border-primary focus:outline-none transition-all text-xs resize-none"></textarea>
</div>
<div class="pt-4 flex justify-end gap-3 border-t border-outline-variant/20">
<button type="button" @click="open = false"
class="px-4 py-2 text-xs font-bold text-outline hover:text-on-surface transition-colors">Cancel</button>
<button type="submit"
class="bg-primary text-white py-2 px-5 rounded-lg font-bold text-xs shadow-md hover:bg-primary-container hover:text-on-primary-container transition-all"
x-text="editing ? 'Save Changes' : 'Add Host'"></button>
</div>
</form>
</div>
</div>
</div>
<script>
function hostForm() {
return {
open: false,
editing: false,
hostId: '',
form: {
name: '', ip: '', hostname: '', port: '22', username: 'root',
os: 'Ubuntu 22.04', provider: 'AWS US-East', type: 'api',
authType: 'key', password: '', notes: ''
},
reset() {
this.editing = false;
this.hostId = '';
this.form = {
name: '', ip: '', hostname: '', port: '22', username: 'root',
os: 'Ubuntu 22.04', provider: 'AWS US-East', type: 'api',
authType: 'key', password: '', notes: ''
};
},
populate(host) {
this.editing = true;
this.hostId = host.ID;
this.form = {
name: host.Name || '',
ip: host.IP || '',
hostname: host.Hostname || '',
port: String(host.Port || 22),
username: host.Username || 'root',
os: host.OS || 'Ubuntu 22.04',
provider: host.Provider || 'AWS US-East',
type: host.Type || 'api',
authType: host.AuthType || 'key',
password: host.Password || '',
notes: host.Notes || ''
};
}
};
}
</script>
{{end}}