// Settings — name, exam type, target band, level, prep, daily minutes, password, sign-out, reset. function Settings({ state, update, onSignOut }) { const [name, setName] = useState(state.userName); const [examType, setExamType] = useState(state.profile?.examType || null); const [band, setBand] = useState(state.targetBand); const [level, setLevel] = useState(state.level); const [prepMonths, setPrepMonths] = useState(state.profile?.prepMonths || 3); const [dailyMinutes, setDailyMinutes] = useState(state.profile?.dailyMinutes || 30); const [confirmReset, setConfirmReset] = useState(false); const [confirmDelete, setConfirmDelete] = useState(false); const [delPw, setDelPw] = useState(''); const [delBusy, setDelBusy] = useState(false); const [delMsg, setDelMsg] = useState(''); const [savedFlash, setSavedFlash] = useState(false); const [pwOld, setPwOld] = useState(''); const [pwNew, setPwNew] = useState(''); const [pwBusy, setPwBusy] = useState(false); const [pwMsg, setPwMsg] = useState(''); const [theme, setTheme] = useState(() => localStorage.getItem('ielts-theme') || 'system'); const [pushStatus, setPushStatus] = useState('checking'); // checking | unsupported | denied | off | on const [pushBusy, setPushBusy] = useState(false); const [pushMsg, setPushMsg] = useState(''); useEffect(() => { if (!('serviceWorker' in navigator) || !('PushManager' in window)) { setPushStatus('unsupported'); return; } if (Notification.permission === 'denied') { setPushStatus('denied'); return; } navigator.serviceWorker.ready.then(reg => reg.pushManager.getSubscription()).then(sub => setPushStatus(sub ? 'on' : 'off')); }, []); function urlBase64ToUint8Array(base64) { const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4); const raw = atob(padded.replace(/-/g, '+').replace(/_/g, '/')); return Uint8Array.from([...raw].map(c => c.charCodeAt(0))); } const enablePush = async () => { setPushBusy(true); setPushMsg(''); try { const perm = await Notification.requestPermission(); if (perm !== 'granted') { setPushStatus('denied'); setPushBusy(false); return; } const { publicKey } = await window.apiClient.push.vapidPublicKey(); const reg = await navigator.serviceWorker.ready; const sub = await reg.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: urlBase64ToUint8Array(publicKey) }); await window.apiClient.push.subscribe(sub.toJSON()); setPushStatus('on'); } catch (e) { setPushMsg(e.message || 'Failed to enable.'); } setPushBusy(false); }; const disablePush = async () => { setPushBusy(true); setPushMsg(''); try { const reg = await navigator.serviceWorker.ready; const sub = await reg.pushManager.getSubscription(); if (sub) { await window.apiClient.push.unsubscribe(sub.endpoint); await sub.unsubscribe(); } setPushStatus('off'); } catch (e) { setPushMsg(e.message || 'Failed to disable.'); } setPushBusy(false); }; const sendTestPush = async () => { setPushBusy(true); setPushMsg(''); try { const r = await window.apiClient.push.test(); setPushMsg(`Sent to ${r.sent}/${r.total} device(s) — check your notifications.`); } catch (e) { if (e.message && e.message.includes('No push subscription on file')) { // The server already dropped this subscription (it expired or the // push service rejected it) but the browser's own PushManager object // doesn't know that — reconcile local state instead of leaving the // toggle stuck showing "on" with no working path back to "off". try { const reg = await navigator.serviceWorker.ready; const sub = await reg.pushManager.getSubscription(); if (sub) await sub.unsubscribe(); } catch { /* best effort */ } setPushStatus('off'); setPushMsg('Your subscription had expired and was cleared — click Enable to resubscribe.'); } else { setPushMsg(e.message || 'Failed to send.'); } } setPushBusy(false); }; const applyTheme = (val) => { setTheme(val); if (val === 'system') { localStorage.removeItem('ielts-theme'); const sys = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; document.documentElement.setAttribute('data-theme', sys); } else { localStorage.setItem('ielts-theme', val); document.documentElement.setAttribute('data-theme', val); } }; const stats = window.api.stats(state); const cfg = window.bandConfig.for(band); const isAuth = state.auth?.user; const dirty = name.trim() !== (state.userName || '').trim() || band !== state.targetBand || level !== state.level || examType !== (state.profile?.examType || null) || prepMonths !== (state.profile?.prepMonths || 3) || dailyMinutes !== (state.profile?.dailyMinutes || 30); const save = (regeneratePlan = false) => { let s = { ...state, userName: (name || '').trim() || 'Learner', targetBand: band, level, profile: { ...(state.profile || {}), examType, prepMonths, dailyMinutes, startDate: state.profile?.startDate || window.api.todayKey() }, }; if (regeneratePlan && examType) { s.plan = window.planEngine.generate({ examType, targetBand: band, level, prepMonths, dailyMinutes, startDate: s.profile.startDate, }); } update(s); // Sync profile to backend if signed in if (isAuth && window.apiClient.isConfigured()) { window.apiClient.auth.updateProfile({ name: s.userName, target_band: band, level, exam_type: examType, prep_months: prepMonths, daily_minutes: dailyMinutes, start_date: s.profile.startDate, }).catch(e => console.warn('profile sync failed:', e.message)); } setSavedFlash(true); setTimeout(() => setSavedFlash(false), 1800); }; const reset = () => { window.api.resetState(); window.location.reload(); }; const deleteAccount = async () => { setDelBusy(true); setDelMsg(''); try { await window.apiClient.auth.deleteAccount(delPw); window.api.resetState(); window.location.reload(); } catch (e) { setDelMsg(e.message || 'Failed.'); setDelBusy(false); } }; const changePw = async () => { setPwBusy(true); setPwMsg(''); try { await window.apiClient.auth.changePassword(pwOld, pwNew); setPwMsg('Password changed.'); setPwOld(''); setPwNew(''); } catch (e) { setPwMsg(e.message || 'Failed.'); } setPwBusy(false); }; return (
Settings

Your study setup.

Change anything any time. Your progress is preserved.

{isAuth && }
{/* Account */} {isAuth && (
Account
Signed in as
{state.auth.user.email}
{state.auth.user.email_verified ? ✓ Verified : Not verified}
)} {/* Name */}
Display name
setName(e.target.value)} placeholder="e.g. Arif" maxLength={40} />
{/* Exam type */}
Exam type
{/* Target band */}
Target band
{window.SUPPORTED_BANDS.map(b => ( ))}
Band {band}: {cfg.blurb}
{/* Level */}
Your current level
{[ { l: 1, label: 'Foundation', note: 'Building basic vocabulary' }, { l: 2, label: 'Intermediate', note: 'Comfortable with most topics' }, { l: 3, label: 'Advanced', note: 'Polishing for band 7.5+' }, ].map(o => ( ))}
{/* Prep duration */}
Preparation duration
{[1, 2, 3, 6, 12].map(m => ( ))}
{/* Daily minutes */}
Daily study time
{[15, 30, 60, 90].map(m => ( ))}
{/* Appearance */}
Appearance
{[ { v: 'light', label: '☀ Light' }, { v: 'dark', label: '☾ Dark' }, { v: 'system', label: '🖥 System' }, ].map(o => ( ))}
Theme applies instantly and is remembered on this device.
{/* Study reminders (push notifications) */} {isAuth && (
Study reminders
{pushStatus === 'unsupported' &&
Not supported in this browser.
} {pushStatus === 'denied' &&
Notifications are blocked for this site in your browser settings.
} {pushStatus === 'checking' &&
Checking…
} {pushStatus === 'off' && ( )} {pushStatus === 'on' && (
✓ Enabled on this device
)} {pushMsg &&
{pushMsg}
}
)} {/* Save bar */}
{savedFlash ? ✓ Saved : dirty ? 'You have unsaved changes.' : 'All changes saved.'}
{/* Change password */} {isAuth && state.auth.user.has_password && (
Change password
setPwOld(e.target.value)} placeholder="Current password" /> setPwNew(e.target.value)} placeholder="New password (min 8 chars)" /> {pwMsg &&
{pwMsg}
}
)} {/* Stats summary */}
Your numbers
{stats.learned}
words learned
{stats.accuracy}%
vocab accuracy
{stats.grammarAccuracy}%
grammar accuracy
{state.streak.count}
day streak
{/* Danger zone */}
Danger zone
Reset wipes everything in this browser. Cannot be undone.
{!confirmReset ? ( ) : (
Are you sure?
)} {isAuth && (
Delete your account permanently — profile, progress, and test history are erased on our servers. Cannot be undone.
{!confirmDelete ? ( ) : (
This cannot be undone. {state.auth.user.has_password && ( setDelPw(e.target.value)} placeholder="Confirm your password" /> )}
{delMsg &&
{delMsg}
}
)}
)}
); } Object.assign(window, { Settings });