From 0ff957d543b0f9ee959355b9abdb12b1ac3e6ae6 Mon Sep 17 00:00:00 2001 From: Adrian Andersen Date: Mon, 17 Jun 2024 20:14:15 +0200 Subject: [PATCH 1/2] refactor(ScannerModal): simplify some code --- .../matches/Scanner/ScannerModal.tsx | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/components/matches/Scanner/ScannerModal.tsx b/src/components/matches/Scanner/ScannerModal.tsx index e0f2bf03..959f0d1e 100644 --- a/src/components/matches/Scanner/ScannerModal.tsx +++ b/src/components/matches/Scanner/ScannerModal.tsx @@ -2,6 +2,7 @@ import { Close, InputRounded } from "@mui/icons-material"; import { AlertColor, Box, Button, Card, Modal, Stack } from "@mui/material"; import Typography from "@mui/material/Typography"; import { Scanner } from "@yudiel/react-qr-scanner"; +import { AxiosResponse } from "axios"; import React, { useEffect, useState } from "react"; import { addWithEndpoint } from "@/api/api"; @@ -13,17 +14,12 @@ import ScannerFeedback from "@/components/matches/Scanner/ScannerFeedback"; import { ScannedTextType, TextType } from "@/utils/types"; function determineScannedTextType(scannedText: string): ScannedTextType { - if (Number.isNaN(Number(scannedText))) { - if (scannedText.length === 12) { - return TextType.BLID; - } - } else { - if (scannedText.length === 8) { - return TextType.BLID; - } else if (scannedText.length === 13) { - return TextType.ISBN; - } + if (/^[\da-f]{12}$|^\d{8}$/.test(scannedText)) { + return TextType.BLID; + } else if (/^\d{13}$/.test(scannedText)) { + return TextType.ISBN; } + return TextType.UNKNOWN; } @@ -77,7 +73,9 @@ const ScannerModal = ({ } try { - const response = await addWithEndpoint( + const response: AxiosResponse = await addWithEndpoint( "matches", "transfer-item", JSON.stringify({ blid: scannedText }), @@ -87,7 +85,7 @@ const ScannerModal = ({ } catch { // Some browsers or devices may not have implemented the vibrate function } - const feedback = response.data?.data?.[0]?.feedback; + const feedback = response.data?.data[0]?.feedback; setFeedback({ text: feedback ?? "Boken har blitt registrert!", severity: feedback ? "info" : "success", From fe3ada672ea0fe78d6f7b75de015c9d85763d9b3 Mon Sep 17 00:00:00 2001 From: Adrian Andersen Date: Mon, 17 Jun 2024 21:18:17 +0200 Subject: [PATCH 2/2] fix(ScannerModal): fix regex scope --- src/components/matches/Scanner/ScannerModal.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/matches/Scanner/ScannerModal.tsx b/src/components/matches/Scanner/ScannerModal.tsx index 959f0d1e..862ef62d 100644 --- a/src/components/matches/Scanner/ScannerModal.tsx +++ b/src/components/matches/Scanner/ScannerModal.tsx @@ -14,7 +14,7 @@ import ScannerFeedback from "@/components/matches/Scanner/ScannerFeedback"; import { ScannedTextType, TextType } from "@/utils/types"; function determineScannedTextType(scannedText: string): ScannedTextType { - if (/^[\da-f]{12}$|^\d{8}$/.test(scannedText)) { + if (/^[\dA-Za-z]{12}$|^\d{8}$/.test(scannedText)) { return TextType.BLID; } else if (/^\d{13}$/.test(scannedText)) { return TextType.ISBN;