import { apiFetch } from '../api'; import React, { useState } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { X, Wifi, CheckCircle2, AlertCircle, Smartphone } from 'lucide-react'; import Numpad from './Numpad.tsx'; interface LinkDeviceModalProps { isOpen: boolean; onClose: () => void; onSuccess: () => void; terminalId: number | null; terminalName: string; } const LinkDeviceModal: React.FC = ({ isOpen, onClose, onSuccess, terminalId, terminalName }) => { const [otp, setOtp] = useState(''); const [success, setSuccess] = useState(false); const [linkedDeviceId, setLinkedDeviceId] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const handleLink = async (freshOtp?: string) => { if (!terminalId) return; const otpToSubmit = freshOtp || otp; if (!otpToSubmit || otpToSubmit.length < 6) return; setLoading(true); setError(null); try { const response = await apiFetch(`/api/terminals/${terminalId}/link-device`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ otp: otpToSubmit }), }); const data = await response.json(); if (response.ok) { setSuccess(true); setLinkedDeviceId(data.deviceId || null); onSuccess(); } else { setError(data.message || 'Failed to link device'); setOtp(''); } } catch (err) { setError('Connection error. Please try again.'); } finally { setLoading(false); } }; const handleClose = () => { setOtp(''); setSuccess(false); setLinkedDeviceId(null); setError(null); onClose(); }; if (!isOpen) return null; return (

Link Physical Device

{!success ? (

{terminalName}

Enter the 6-digit OTP shown on the device screen

{/* Step-by-step instructions */}
1

Power on the ESP32 device and connect it to Wi-Fi

2

A 6-digit OTP will appear on the device display

3

Enter that OTP below to link the device

{error && ( {error} )} { setOtp(v); if (v.length === 6) { setTimeout(() => handleLink(v), 100); } }} maxLength={6} />
) : (

Device Linked Successfully!

The physical device is now paired with {terminalName}

{linkedDeviceId && (
{linkedDeviceId}
)}

The device will automatically receive its API key and restart. It's now ready to scan QR codes and print bills.

)}
); }; export default LinkDeviceModal;