Files
AthonetTools/static/js/wizard/step0_target.js

60 lines
2.0 KiB
JavaScript

// static/js/graph/wizard/step0_target.js
import { setTarget, bootstrapAccess, getTarget } from './api.js';
export function mountTargetControls() {
const row = document.querySelector('#target-host-row');
if (!row) return;
// Use explicit IDs so we always hit the right elements
const ipInput = document.getElementById('target-ip-input');
const btn = document.getElementById('btn-enable-access');
const badge = document.getElementById('access-badge');
if (!ipInput || !btn || !badge) return;
// Prefill: localStorage first, then try backend GET /api/host/target
const saved = localStorage.getItem('targetHostIp');
if (saved) ipInput.value = saved;
getTarget()
.then(({ ip }) => { if (ip && !saved) ipInput.value = ip; })
.catch(() => { /* ignore if endpoint missing */ });
btn.addEventListener('click', async () => {
const ip = (ipInput.value || '').trim();
if (!ip) {
badge.className = 'badge bg-warning';
badge.textContent = 'Enter IP first';
return;
}
try {
btn.disabled = true;
const old = btn.textContent;
btn.textContent = 'Enabling…';
badge.className = 'badge bg-secondary';
badge.textContent = 'Working';
await setTarget(ip); // POST /api/host/target
await bootstrapAccess(); // POST /api/host/bootstrap_access (SSH+webconsole)
// Persist for Stage 2 (and page reloads)
localStorage.setItem('targetHostIp', ip);
badge.className = 'badge bg-success';
badge.textContent = 'SSH & Webconsole Ready';
btn.textContent = old;
btn.disabled = false;
} catch (e) {
console.error(e);
badge.className = 'badge bg-danger';
badge.textContent = 'Failed';
btn.disabled = false;
}
});
}
// Helper for other steps to read the latest target IP
export function getTargetHostData() {
const inputIp = (document.getElementById('target-ip-input')?.value || '').trim();
const stored = localStorage.getItem('targetHostIp') || '';
return { ip: inputIp || stored || '' };
}