52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
import React, { useEffect, useRef, useState } from "react";
|
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
import QRCodeMin from "!!raw-loader!../../utils/qrcode.min.js";
|
|
|
|
import { getDeviceAdditionInfo } from "../../actions/deviceActions";
|
|
import { scaleH, scaleW } from "../../utils/helperMethods";
|
|
|
|
let script = document.createElement("script");
|
|
script.innerText = QRCodeMin;
|
|
document.body.appendChild(script);
|
|
|
|
export default function TQRCode({
|
|
ariaLabel,
|
|
text,
|
|
width = "128",
|
|
height = "128",
|
|
}) {
|
|
const qrcodeRef = useRef(null);
|
|
const deviceInfo = useSelector((state) => state.device.deviceInfo);
|
|
const { entryMenu, nowMenu } = useSelector((state) => state.common.menu);
|
|
const dispatch = useDispatch();
|
|
|
|
useEffect(() => {
|
|
if (!deviceInfo) {
|
|
dispatch(getDeviceAdditionInfo());
|
|
}
|
|
}, [deviceInfo, dispatch]);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === "object" && deviceInfo && entryMenu && nowMenu) {
|
|
if (qrcodeRef.current) {
|
|
while (qrcodeRef.current.firstChild) {
|
|
qrcodeRef.current.removeChild(qrcodeRef.current.firstChild);
|
|
}
|
|
}
|
|
|
|
// nowMenu 데이터를 Base64로 인코딩
|
|
const encodedNowMenu = encodeURIComponent(nowMenu);
|
|
const encodeEntryMenu = encodeURIComponent(entryMenu);
|
|
|
|
const qrcode = new window.QRCode(qrcodeRef.current, {
|
|
text: `${text}&entryMenu=${encodeEntryMenu}&nowMenu=${encodedNowMenu}&idx=${deviceInfo.dvcIndex}`,
|
|
width: scaleW(width),
|
|
height: scaleH(height),
|
|
correctLevel: window.QRCode.CorrectLevel.L,
|
|
});
|
|
}
|
|
}, [text, deviceInfo]);
|
|
return <div aria-label={ariaLabel} ref={qrcodeRef} />;
|
|
}
|