Skip to content

Commit

Permalink
fix(Scanner): lock scanner after first scan to avoid duplicate orders
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianAndersen committed Aug 24, 2023
1 parent f139891 commit a552b43
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/components/matches/Scanner/Scanner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useRef, useState } from "react";
import { AlertColor, Button } from "@mui/material";
import { addWithEndpoint } from "../../../api/api";
import QrCodeScannerIcon from "@mui/icons-material/QrCodeScanner";
Expand Down Expand Up @@ -34,6 +34,7 @@ const Scanner = ({ forceUpdate }: { forceUpdate: () => void }) => {
const [feedbackSeverity, setFeedbackSeverity] =
useState<AlertColor>("success");
const [feedbackVisible, setFeedbackVisible] = useState(false);
const scannerLocked = useRef(false);

const displayFeedback = (feedback: string, severity: AlertColor) => {
setFeedback(feedback);
Expand All @@ -60,6 +61,10 @@ const Scanner = ({ forceUpdate }: { forceUpdate: () => void }) => {
return false;
}

if (scannerLocked.current) {
return false;
}
scannerLocked.current = true;
try {
const response = await addWithEndpoint(
"matches",
Expand All @@ -78,6 +83,7 @@ const Scanner = ({ forceUpdate }: { forceUpdate: () => void }) => {
displayFeedback(String(error), "error");
return false;
} finally {
scannerLocked.current = false;
// TODO: test with serveo, do we need forceUpdate?
forceUpdate();
}
Expand Down

0 comments on commit a552b43

Please sign in to comment.