import { html } from 'lit'; import { LightElement } from '../lib/base.js'; import { t, I18nMixin, LOCALES, getLocale, setLocale } from '../lib/i18n.js'; export class SetupPage extends I18nMixin(LightElement) { static get properties() { return { _username: { state: true }, _password: { state: true }, _confirm: { state: true }, _encrypted: { state: true }, _locale: { state: true }, _profiles: { state: true }, _profile: { state: true }, _error: { state: true }, _busy: { state: true }, }; } constructor() { super(); this._username = ''; this._password = ''; this._confirm = ''; this._encrypted = true; this._locale = getLocale(); this._profiles = []; this._profile = 'family'; this._error = null; this._busy = false; } connectedCallback() { super.connectedCallback(); this._loadProfiles(); } async _loadProfiles() { try { const res = await fetch('/api/setup/profiles'); if (!res.ok) return; const list = await res.json(); if (Array.isArray(list) && list.length) { this._profiles = list; this._profile = list[0].id; } } catch { /* one preset ships; a failed fetch just keeps the default */ } } _submit(e) { e.preventDefault(); if (this._busy) return; this._error = null; if (!this._username.trim()) { this._error = t('setup.username'); return; } if (this._password.length < 4) { this._error = t('setup.pw.short'); return; } if (this._password !== this._confirm) { this._error = t('setup.pw.mismatch'); return; } this._doCreate(); } async _doCreate() { this._busy = true; try { const res = await fetch('/api/setup/user', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: this._username.trim(), password: this._password, encrypted: this._encrypted, locale: this._locale, profile: this._profile, }), }); if (!res.ok) { const txt = await res.text(); this._error = txt || `Request failed (${res.status}).`; return; } // First user created — reload into the app. window.location.reload(); } catch { this._error = t('setup.network'); } finally { this._busy = false; } } render() { const btnLabel = this._busy ? html`${t('setup.creating')}` : t('setup.submit'); return html`