// LoginScreen.jsx — Telefon + 6 haneli PIN ile tek-form giriş
const { useState: useStateLS, useRef: useRefLS } = React;
// Native numeric PIN input. Auto-submits when length reached.
// On mobile (inputMode=numeric) the OS keypad opens automatically; on
// desktop the user just types. Visual: single styled input with masked dots.
function PinInput({ length, onSubmit, error, autoFocus = true, disabled, name, accent }) {
const [pin, setPin] = useStateLS('');
const submitting = useRefLS(false);
const inputRef = useRefLS(null);
const color = accent || 'var(--accent)';
const reset = () => {
setPin('');
submitting.current = false;
if (inputRef.current) {
try { inputRef.current.focus(); } catch {}
}
};
const handleChange = (e) => {
if (submitting.current) return;
const v = e.target.value.replace(/\D/g, '').slice(0, length);
setPin(v);
if (v.length === length && onSubmit) {
submitting.current = true;
setTimeout(async () => {
try {
const ok = await onSubmit(v);
if (ok === false) reset();
else submitting.current = false;
} catch { reset(); }
}, 80);
}
};
return (
{ if (!error) e.currentTarget.style.borderColor = color; }}
onBlur={(e) => { if (!error && pin.length !== length) e.currentTarget.style.borderColor = 'var(--border)'; }}
/>
{error &&
{error}
}
);
}
// Format raw 10-digit input as "5XX XXX XX XX"
function formatPhoneInput(digits) {
const d = digits.slice(0, 10);
const parts = [];
if (d.length > 0) parts.push(d.slice(0, 3));
if (d.length > 3) parts.push(d.slice(3, 6));
if (d.length > 6) parts.push(d.slice(6, 8));
if (d.length > 8) parts.push(d.slice(8, 10));
return parts.join(' ');
}
function LoginScreen({ onLoggedIn }) {
const [phoneDigits, setPhoneDigits] = useStateLS('');
const [error, setError] = useStateLS(null);
const pinInputRef = useRefLS(null);
const fullPhone = '+90' + phoneDigits;
const phoneOk = phoneDigits.length === 10;
const submit = async (pin) => {
try {
setError(null);
await Store.login(fullPhone, pin);
onLoggedIn();
return true;
} catch (e) {
const msg = e.status === 429 ? e.message : 'Telefon veya PIN hatalı';
setError(msg);
return false;
}
};
const handlePhoneChange = (e) => {
const d = e.target.value.replace(/\D/g, '').slice(0, 10);
setPhoneDigits(d);
setError(null);
};
// When phone reaches 10 digits, auto-focus the PIN field.
const handlePhoneKeyUp = (e) => {
if (phoneDigits.length === 10 && pinInputRef.current) {
try { pinInputRef.current.querySelector('input')?.focus(); } catch {}
}
};
return (
);
}
Object.assign(window, { LoginScreen, PinInput, formatPhoneInput });