feat: implement the open fallback access point and captive portal
This commit is contained in:
+27
-5
@@ -4,20 +4,42 @@
|
||||
const ssid = document.querySelector('#ssid');
|
||||
const refresh = document.querySelector('#refresh');
|
||||
const form = document.querySelector('#network-form');
|
||||
const password = document.querySelector('#password');
|
||||
const networkStatus = document.querySelector('#network-status');
|
||||
const remove = document.querySelector('#delete-network');
|
||||
let timer = 0;
|
||||
const scan = async () => {
|
||||
clearTimeout(timer); refresh.disabled = true; status.textContent = 'Ищем сети…';
|
||||
try {
|
||||
const response = await fetch('/setup/scan', { cache: 'no-store' });
|
||||
const text = await response.text();
|
||||
const response = await fetch('/api/network/scan', { cache: 'no-store' });
|
||||
const result = await response.json();
|
||||
if (!response.ok) throw new Error('scan unavailable');
|
||||
if (text.trim() === 'scanning') { timer = setTimeout(scan, 700); return; }
|
||||
const names = [...new Set(text.split('\n').map(value => value.trim()).filter(Boolean))];
|
||||
if (result.state === 'scanning') { timer = setTimeout(scan, 700); return; }
|
||||
const names = [...new Set(result.networks || [])];
|
||||
networks.replaceChildren(...names.map(name => { const button = document.createElement('button'); button.className = 'network'; button.type = 'button'; button.textContent = name; button.addEventListener('click', () => { ssid.value = name; ssid.focus(); }); return button; }));
|
||||
status.textContent = names.length ? 'Выберите сеть или введите её вручную.' : 'Сети не найдены. Введите скрытую сеть вручную.';
|
||||
} catch (_) { status.textContent = 'Не удалось выполнить поиск. Введите сеть вручную.'; }
|
||||
finally { if (!timer) refresh.disabled = false; }
|
||||
};
|
||||
const updateStatus = async () => {
|
||||
try {
|
||||
const result = await fetch('/api/network/status', { cache: 'no-store' }).then(response => response.json());
|
||||
networkStatus.textContent = result.message || '';
|
||||
if (result.state === 'validating') setTimeout(updateStatus, 1000);
|
||||
} catch (_) { networkStatus.textContent = 'Не удалось получить состояние сети.'; }
|
||||
};
|
||||
refresh.addEventListener('click', scan);
|
||||
form.addEventListener('submit', event => { event.preventDefault(); status.textContent = 'Сеть выбрана. Проверка и сохранение будут доступны на следующем шаге настройки.'; document.querySelector('#password').value = ''; });
|
||||
form.addEventListener('submit', async event => {
|
||||
event.preventDefault();
|
||||
try {
|
||||
const response = await fetch('/api/network/validate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ssid: ssid.value, password: password.value }) });
|
||||
const result = await response.json(); password.value = ''; networkStatus.textContent = result.message || 'Проверяем подключение…';
|
||||
if (response.ok) updateStatus();
|
||||
} catch (_) { password.value = ''; networkStatus.textContent = 'Не удалось начать проверку. Повторите попытку.'; }
|
||||
});
|
||||
remove.addEventListener('click', async () => {
|
||||
try { const result = await fetch('/api/network/delete', { method: 'POST' }).then(response => response.json()); networkStatus.textContent = result.message || 'Сеть удалена.'; }
|
||||
catch (_) { networkStatus.textContent = 'Не удалось удалить сеть.'; }
|
||||
});
|
||||
updateStatus();
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user