Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Address Comments From Other PR (#437)
Browse files Browse the repository at this point in the history
* refactor(ScannerModal): simplify some code

* fix(ScannerModal): fix regex scope
  • Loading branch information
AdrianAndersen authored Jun 17, 2024
1 parent 64b75aa commit da2fb5a
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/components/matches/Scanner/ScannerModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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-Za-z]{12}$|^\d{8}$/.test(scannedText)) {
return TextType.BLID;
} else if (/^\d{13}$/.test(scannedText)) {
return TextType.ISBN;
}

return TextType.UNKNOWN;
}

Expand Down Expand Up @@ -77,7 +73,9 @@ const ScannerModal = ({
}

try {
const response = await addWithEndpoint(
const response: AxiosResponse<null | {
data: { feedback: string }[];
}> = await addWithEndpoint(
"matches",
"transfer-item",
JSON.stringify({ blid: scannedText }),
Expand All @@ -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",
Expand Down

0 comments on commit da2fb5a

Please sign in to comment.