import config from './config.js'; import { createDeletionFlow } from './flow.mjs'; const status = document.querySelector('#deletion-status'); const signedOutSection = document.querySelector('#signed-out-deletion'); const form = document.querySelector('#deletion-form'); const email = document.querySelector('#deletion-email'); const code = document.querySelector('#deletion-code'); const confirmation = document.querySelector('#deletion-confirmation'); const cancel = document.querySelector('#deletion-cancel'); const submit = document.querySelector('#deletion-submit'); let flow; let attempt = 0; document.querySelectorAll('[data-year]').forEach((node) => { node.textContent = new Date().getFullYear(); }); function render() { const state = flow.snapshot(); form.hidden = state.phase === 'done'; document.querySelector('#email-step').hidden = state.phase !== 'email'; document.querySelector('#code-step').hidden = state.phase !== 'code'; document.querySelector('#confirm-step').hidden = state.phase !== 'confirm'; document.querySelector('#verified-email').textContent = state.email; submit.textContent = { email: 'Email me a code', code: 'Verify code', confirm: 'Permanently delete my account' }[state.phase] || 'Complete'; form.setAttribute('aria-busy', String(state.busy)); submit.disabled = state.busy; cancel.disabled = state.busy && state.phase === 'confirm'; email.disabled = state.phase !== 'email' || state.busy; code.disabled = state.phase !== 'code' || state.busy; confirmation.disabled = state.phase !== 'confirm' || state.busy; } try { flow = createDeletionFlow(config); form.hidden = false; status.textContent = 'Enter your registered email to request a verification code.'; render(); } catch { signedOutSection.hidden = true; status.textContent = ''; } form.addEventListener('submit', async (event) => { event.preventDefault(); if (!flow || flow.snapshot().busy) return; const phase = flow.snapshot().phase; const current = ++attempt; status.textContent = 'Please wait…'; try { const pending = phase === 'email' ? flow.sendCode(email.value) : phase === 'code' ? flow.verifyCode(code.value) : flow.deleteAccount(confirmation.value); render(); const message = await pending; if (current !== attempt) return; status.textContent = message || (flow.snapshot().phase === 'done' ? 'Your account deletion is complete. Store subscriptions must be canceled separately. Independent provider and backup records follow their applicable retention processes.' : 'Email verified. Review the deletion details, then type DELETE to confirm.'); } catch (error) { if (current === attempt) status.textContent = error.message; } finally { if (current === attempt) { code.value = ''; confirmation.value = ''; render(); if (!flow.snapshot().busy) ({ email, code, confirm: confirmation }[flow.snapshot().phase])?.focus(); } } }); cancel.addEventListener('click', () => { if (flow?.cancel()) { attempt += 1; form.reset(); status.textContent = 'Canceled. No deletion request was sent.'; render(); email.focus(); } }); // Also clear transient state when a page enters the back/forward cache. window.addEventListener('pagehide', () => { attempt += 1; flow?.dispose(); form.reset(); }); window.addEventListener('pageshow', () => { if (flow) render(); });