|
1 | | -import { useEffect, useRef, useState } from "react"; |
2 | | -import QrScanner from "qr-scanner"; |
3 | | -import { useTicketQrCodeValidate } from "../hooks/useOrderHook"; |
4 | | -import Button from "../../../../design-system/ui/Button"; |
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import QrScanner from 'qr-scanner'; |
| 3 | +import { useTicketQrCodeValidate } from '../hooks/useOrderHook'; |
| 4 | +import Button from '../../../../design-system/ui/Button'; |
5 | 5 |
|
6 | 6 | const QrScannerComponent = () => { |
7 | | - const videoRef = useRef<HTMLVideoElement>(null); |
8 | | - const [isScanning, setIsScanning] = useState(false); |
9 | | - const qrScannerRef = useRef<QrScanner | null>(null); |
| 7 | + const videoRef = useRef<HTMLVideoElement>(null); |
| 8 | + const [isScanning, setIsScanning] = useState(false); |
| 9 | + const qrScannerRef = useRef<QrScanner | null>(null); |
10 | 10 |
|
11 | | - const { mutate: validateQr } = useTicketQrCodeValidate(); |
| 11 | + const { mutate: validateQr } = useTicketQrCodeValidate(); |
12 | 12 |
|
13 | | - useEffect(() => { |
14 | | - if (!videoRef.current) return; |
| 13 | + useEffect(() => { |
| 14 | + if (!videoRef.current) return; |
15 | 15 |
|
16 | | - const scanner = new QrScanner( |
17 | | - videoRef.current, |
18 | | - (decoded) => { |
19 | | - const data = decoded.data; |
20 | | - setIsScanning(false); |
21 | | - scanner.stop(); |
22 | | - checkInApiCall(data); |
23 | | - }, |
24 | | - { |
25 | | - onDecodeError: (err) => { |
26 | | - console.warn("QR decode error:", err); |
27 | | - }, |
28 | | - highlightScanRegion: true, |
29 | | - maxScansPerSecond: 5, |
30 | | - } |
31 | | - ); |
32 | | - |
33 | | - qrScannerRef.current = scanner; |
| 16 | + const scanner = new QrScanner( |
| 17 | + videoRef.current, |
| 18 | + decoded => { |
| 19 | + const data = decoded.data; |
| 20 | + setIsScanning(false); |
| 21 | + scanner.stop(); |
| 22 | + checkInApiCall(data); |
| 23 | + }, |
| 24 | + { |
| 25 | + onDecodeError: err => { |
| 26 | + console.warn('QR decode error:', err); |
| 27 | + }, |
| 28 | + highlightScanRegion: true, |
| 29 | + maxScansPerSecond: 5, |
| 30 | + } |
| 31 | + ); |
34 | 32 |
|
35 | | - return () => { |
36 | | - scanner.destroy(); |
37 | | - qrScannerRef.current = null; |
38 | | - }; |
39 | | - }, []); |
| 33 | + qrScannerRef.current = scanner; |
40 | 34 |
|
41 | | - const handleStartScan = async () => { |
42 | | - try { |
43 | | - await navigator.mediaDevices.getUserMedia({ video: true }); |
44 | | - qrScannerRef.current?.start(); |
45 | | - setIsScanning(true); |
46 | | - } catch (e) { |
47 | | - alert( |
48 | | - "카메라 접근이 차단되어 있어 스캔할 수 없습니다.\n시스템 설정 또는 브라우저 설정에서 권한을 허용해 주세요." |
49 | | - ); |
50 | | - } |
| 35 | + return () => { |
| 36 | + scanner.destroy(); |
| 37 | + qrScannerRef.current = null; |
51 | 38 | }; |
| 39 | + }, []); |
52 | 40 |
|
53 | | - const checkInApiCall = async (qrData: string) => { |
54 | | - try { |
55 | | - const params = new URLSearchParams(qrData.replace(/-/g, '=')); |
56 | | - const orderIdStr = params.get('orderId'); |
57 | | - const sig = params.get('sig'); |
58 | | - const orderId = orderIdStr ? parseInt(orderIdStr, 10) : NaN; |
| 41 | + const handleStartScan = async () => { |
| 42 | + try { |
| 43 | + await navigator.mediaDevices.getUserMedia({ video: true }); |
| 44 | + qrScannerRef.current?.start(); |
| 45 | + setIsScanning(true); |
| 46 | + } catch { |
| 47 | + alert( |
| 48 | + '카메라 접근이 차단되어 있어 스캔할 수 없습니다.\n시스템 설정 또는 브라우저 설정에서 권한을 허용해 주세요.' |
| 49 | + ); |
| 50 | + } |
| 51 | + }; |
59 | 52 |
|
60 | | - if (!isNaN(orderId) && sig) { |
61 | | - validateQr({ orderId, sig }); |
62 | | - } else { |
63 | | - alert("QR 코드 데이터 형식이 올바르지 않습니다."); |
64 | | - } |
65 | | - } catch { |
66 | | - alert("QR 코드 데이터를 파싱할 수 없습니다."); |
67 | | - } |
68 | | - }; |
| 53 | + const checkInApiCall = async (qrData: string) => { |
| 54 | + try { |
| 55 | + const params = new URLSearchParams(qrData.replace(/-/g, '=')); |
| 56 | + const orderIdStr = params.get('orderId'); |
| 57 | + const sig = params.get('sig'); |
| 58 | + const orderId = orderIdStr ? parseInt(orderIdStr, 10) : NaN; |
69 | 59 |
|
70 | | - return ( |
71 | | - <div className="p-4"> |
72 | | - <video |
73 | | - ref={videoRef} |
74 | | - className="border rounded w-full max-w-md" |
75 | | - style={{ aspectRatio: "3 / 4" }} |
76 | | - /> |
77 | | - {!isScanning && ( |
78 | | - <Button label="스캔하기" onClick={handleStartScan} className="w-full h-12 rounded-full mt-10 px-4 py-2" /> |
79 | | - )} |
80 | | - </div> |
81 | | - ); |
| 60 | + if (!isNaN(orderId) && sig) { |
| 61 | + validateQr({ orderId, sig }); |
| 62 | + } else { |
| 63 | + alert('QR 코드 데이터 형식이 올바르지 않습니다.'); |
| 64 | + } |
| 65 | + } catch { |
| 66 | + alert('QR 코드 데이터를 파싱할 수 없습니다.'); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + return ( |
| 71 | + <div className="p-4"> |
| 72 | + <video ref={videoRef} className="border rounded w-full max-w-md" style={{ aspectRatio: '3 / 4' }} /> |
| 73 | + {!isScanning && ( |
| 74 | + <Button label="스캔하기" onClick={handleStartScan} className="w-full h-12 rounded-full mt-10 px-4 py-2" /> |
| 75 | + )} |
| 76 | + </div> |
| 77 | + ); |
82 | 78 | }; |
83 | 79 | export default QrScannerComponent; |
0 commit comments