fix: error handling + robust state in Portal access panel JS (per review)
This commit is contained in:
@@ -2142,6 +2142,8 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
const PA_PROJECT_ID = "{{ project_id }}";
|
const PA_PROJECT_ID = "{{ project_id }}";
|
||||||
|
let paEnabled = false;
|
||||||
|
function paToast(msg) { if (window.showToast) showToast(msg, 'error'); else alert(msg); }
|
||||||
function openPortalAccess() { document.getElementById('portal-access-modal').classList.remove('hidden'); loadPortalAccess(); }
|
function openPortalAccess() { document.getElementById('portal-access-modal').classList.remove('hidden'); loadPortalAccess(); }
|
||||||
function closePortalAccess() { document.getElementById('portal-access-modal').classList.add('hidden'); }
|
function closePortalAccess() { document.getElementById('portal-access-modal').classList.add('hidden'); }
|
||||||
|
|
||||||
@@ -2153,27 +2155,38 @@ function copyField(id, btn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadPortalAccess() {
|
async function loadPortalAccess() {
|
||||||
const j = await (await fetch(`/projects/${PA_PROJECT_ID}/portal-access`)).json();
|
try {
|
||||||
renderPortalAccess(j);
|
const r = await fetch(`/projects/${PA_PROJECT_ID}/portal-access`);
|
||||||
|
if (!r.ok) throw new Error('load failed');
|
||||||
|
renderPortalAccess(await r.json());
|
||||||
|
} catch (e) { paToast('Could not load portal access.'); }
|
||||||
}
|
}
|
||||||
function renderPortalAccess(j) {
|
function renderPortalAccess(j) {
|
||||||
|
paEnabled = !!j.enabled;
|
||||||
const toggle = document.getElementById('pa-toggle');
|
const toggle = document.getElementById('pa-toggle');
|
||||||
const details = document.getElementById('pa-details');
|
const details = document.getElementById('pa-details');
|
||||||
toggle.textContent = j.enabled ? 'On — click to disable' : 'Off — click to enable';
|
toggle.textContent = paEnabled ? 'On — click to disable' : 'Off — click to enable';
|
||||||
toggle.className = 'px-3 py-1.5 text-sm rounded-lg border ' +
|
toggle.className = 'px-3 py-1.5 text-sm rounded-lg border ' +
|
||||||
(j.enabled ? 'border-green-500 text-green-600 dark:text-green-400' : 'border-slate-300 dark:border-slate-600');
|
(paEnabled ? 'border-green-500 text-green-600 dark:text-green-400' : 'border-slate-300 dark:border-slate-600');
|
||||||
details.classList.toggle('hidden', !j.enabled);
|
details.classList.toggle('hidden', !paEnabled);
|
||||||
if (j.enabled && j.link_url) document.getElementById('pa-link').value = j.link_url;
|
document.getElementById('pa-link').value = (paEnabled && j.link_url) ? j.link_url : '';
|
||||||
}
|
}
|
||||||
async function togglePortalEnabled() {
|
async function togglePortalEnabled() {
|
||||||
const on = document.getElementById('pa-toggle').textContent.startsWith('On');
|
const action = paEnabled ? 'disable' : 'enable';
|
||||||
const j = await (await fetch(`/projects/${PA_PROJECT_ID}/portal-access/${on ? 'disable' : 'enable'}`, { method: 'POST' })).json();
|
try {
|
||||||
if (on) renderPortalAccess({ enabled: false, link_url: null });
|
const r = await fetch(`/projects/${PA_PROJECT_ID}/portal-access/${action}`, { method: 'POST' });
|
||||||
else renderPortalAccess(j);
|
if (!r.ok) throw new Error('toggle failed');
|
||||||
|
const j = await r.json();
|
||||||
|
renderPortalAccess(action === 'disable' ? { enabled: false, link_url: null } : j);
|
||||||
|
} catch (e) { paToast(`Could not ${action} the portal.`); }
|
||||||
}
|
}
|
||||||
async function regeneratePassword() {
|
async function regeneratePassword() {
|
||||||
const j = await (await fetch(`/projects/${PA_PROJECT_ID}/portal-access/password`, { method: 'POST' })).json();
|
try {
|
||||||
if (j.password) { const f = document.getElementById('pa-pass'); f.value = j.password; f.placeholder = ''; }
|
const r = await fetch(`/projects/${PA_PROJECT_ID}/portal-access/password`, { method: 'POST' });
|
||||||
|
if (!r.ok) throw new Error('password failed');
|
||||||
|
const j = await r.json();
|
||||||
|
if (j.password) { const f = document.getElementById('pa-pass'); f.value = j.password; f.placeholder = ''; }
|
||||||
|
} catch (e) { paToast('Could not generate a password.'); }
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user