2024-05-16 13:00:18 +02:00
|
|
|
import { FC, useEffect, useState } from "react";
|
|
|
|
|
import QRCodeInternal from "react-qr-code";
|
|
|
|
|
import { Box } from "@mui/material";
|
|
|
|
|
import { colors } from "../styles/theme";
|
|
|
|
|
|
|
|
|
|
type QRCodeProps = {
|
|
|
|
|
name: string;
|
|
|
|
|
label: string;
|
2024-05-16 18:22:10 +02:00
|
|
|
token: string;
|
|
|
|
|
verified: boolean;
|
2024-05-16 13:00:18 +02:00
|
|
|
helperText?: string;
|
|
|
|
|
getValue?: (id: string) => Promise<string>;
|
|
|
|
|
refreshInterval?: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const QRCode: FC<QRCodeProps> = ({
|
|
|
|
|
name,
|
|
|
|
|
label,
|
2024-05-16 18:22:10 +02:00
|
|
|
token,
|
|
|
|
|
verified,
|
2024-05-16 13:00:18 +02:00
|
|
|
helperText,
|
|
|
|
|
getValue,
|
|
|
|
|
refreshInterval,
|
|
|
|
|
}) => {
|
|
|
|
|
const [value, setValue] = useState("");
|
|
|
|
|
const { white } = colors;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-16 18:22:10 +02:00
|
|
|
if (!verified && getValue && refreshInterval) {
|
2024-05-16 13:00:18 +02:00
|
|
|
const interval = setInterval(async () => {
|
2024-05-16 18:22:10 +02:00
|
|
|
const result = await getValue(token);
|
2024-05-16 13:00:18 +02:00
|
|
|
setValue(result);
|
2024-05-16 18:22:10 +02:00
|
|
|
}, refreshInterval * 1000);
|
2024-05-16 13:00:18 +02:00
|
|
|
return () => clearInterval(interval);
|
|
|
|
|
}
|
|
|
|
|
}, [getValue, refreshInterval]);
|
|
|
|
|
|
2024-05-16 18:22:10 +02:00
|
|
|
return !verified ? (
|
2024-05-16 13:00:18 +02:00
|
|
|
<Box sx={{ backgroundColor: white, m: 2 }}>
|
|
|
|
|
<QRCodeInternal value={value} />
|
|
|
|
|
<Box>{helperText}</Box>
|
|
|
|
|
</Box>
|
2024-05-16 18:22:10 +02:00
|
|
|
) : null;
|
2024-05-16 13:00:18 +02:00
|
|
|
};
|