added new validation to scanner
This commit is contained in:
@@ -9,6 +9,10 @@ interface BarcodeScannerProps {
|
||||
onScan: (code: string) => void;
|
||||
}
|
||||
|
||||
function isValidCode(code: string) {
|
||||
return /^[0-9E]+$/.test(code);
|
||||
}
|
||||
|
||||
export default function BarcodeScanner({ onScan }: BarcodeScannerProps) {
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
const lastScan = useRef<string | null>(null);
|
||||
@@ -28,7 +32,7 @@ export default function BarcodeScanner({ onScan }: BarcodeScannerProps) {
|
||||
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getUserMedia({
|
||||
video: { facingMode: { ideal: "environment" } }, // 👈 fuerza cámara trasera
|
||||
video: { facingMode: { ideal: "environment" } },
|
||||
});
|
||||
|
||||
if (!videoRef.current) return;
|
||||
@@ -64,6 +68,13 @@ export default function BarcodeScanner({ onScan }: BarcodeScannerProps) {
|
||||
if (raw && raw !== lastScan.current) {
|
||||
lastScan.current = raw;
|
||||
const processed = raw.startsWith("0") ? raw.slice(1) : raw;
|
||||
|
||||
if (!isValidCode(processed)) {
|
||||
toast.error("Intente de nuevo.");
|
||||
lastScan.current = null;
|
||||
return;
|
||||
}
|
||||
|
||||
onScan(processed);
|
||||
}
|
||||
}
|
||||
@@ -116,7 +127,15 @@ export default function BarcodeScanner({ onScan }: BarcodeScannerProps) {
|
||||
const code = result.getText().trim();
|
||||
if (code && code !== lastScan.current) {
|
||||
lastScan.current = code;
|
||||
|
||||
const processed = code.startsWith("0") ? code.slice(1) : code;
|
||||
|
||||
if (!isValidCode(processed)) {
|
||||
toast.error("Intente de nuevo");
|
||||
lastScan.current = null;
|
||||
return;
|
||||
}
|
||||
|
||||
onScan(processed);
|
||||
}
|
||||
} else if (err && !(err instanceof NotFoundException)) {
|
||||
@@ -163,4 +182,4 @@ export default function BarcodeScanner({ onScan }: BarcodeScannerProps) {
|
||||
);
|
||||
}
|
||||
//Chatgpt
|
||||
//Gemini
|
||||
//Gemini
|
||||
|
||||
Reference in New Issue
Block a user