Skip to content

Commit

Permalink
Merge pull request #186 from ugahacks/feature/scanner-additions
Browse files Browse the repository at this point in the history
fuck
  • Loading branch information
shawnpradeep authored Feb 9, 2024
2 parents d5083ec + 748c7af commit 89bbff9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 59 deletions.
98 changes: 51 additions & 47 deletions projects/mybyte/components/Html5QrcodePlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,63 @@
// Html5QrcodePlugin.jsx

import { Html5QrcodeScanner } from "html5-qrcode";
import React from 'react';
import React from "react";

const qrcodeRegionId = "html5qr-code-full-region";

class Html5QrcodePlugin extends React.Component<any> {
html5QrcodeScanner: Html5QrcodeScanner | undefined = undefined;

render() {
return <div id={qrcodeRegionId} />;
html5QrcodeScanner: Html5QrcodeScanner | undefined = undefined;

render() {
return <div id={qrcodeRegionId} />;
}

componentWillUnmount() {
// TODO(mebjas): See if there is a better way to handle
// promise in `componentWillUnmount`.
if (this.html5QrcodeScanner == undefined) return;
this.html5QrcodeScanner.clear().catch((error) => {
console.error("Failed to clear html5QrcodeScanner. ", error);
});
}

componentDidMount() {
// Creates the configuration object for Html5QrcodeScanner.
function createConfig(props: any) {
var config: any = {};
if (props.fps) {
config.fps = props.fps;
}
if (props.qrbox) {
config.qrbox = props.qrbox;
}
if (props.aspectRatio) {
config.aspectRatio = props.aspectRatio;
}
if (props.disableFlip !== undefined) {
config.disableFlip = props.disableFlip;
}
return config;
}

componentWillUnmount() {
// TODO(mebjas): See if there is a better way to handle
// promise in `componentWillUnmount`.
if (this.html5QrcodeScanner == undefined) return;
this.html5QrcodeScanner.clear().catch(error => {
console.error("Failed to clear html5QrcodeScanner. ", error);
});
}
var config = createConfig(this.props);
var verbose = this.props.verbose === true;

componentDidMount() {
// Creates the configuration object for Html5QrcodeScanner.
function createConfig(props: any) {
var config: any = {};
if (props.fps) {
config.fps = props.fps;
}
if (props.qrbox) {
config.qrbox = props.qrbox;
}
if (props.aspectRatio) {
config.aspectRatio = props.aspectRatio;
}
if (props.disableFlip !== undefined) {
config.disableFlip = props.disableFlip;
}
return config;
}

var config = createConfig(this.props);
var verbose = this.props.verbose === true;

// Suceess callback is required.
if (!(this.props.qrCodeSuccessCallback )) {
throw "qrCodeSuccessCallback is required callback.";
}

this.html5QrcodeScanner = new Html5QrcodeScanner(
qrcodeRegionId, config, verbose);
this.html5QrcodeScanner.render(
this.props.qrCodeSuccessCallback,
this.props.qrCodeErrorCallback);
// Suceess callback is required.
if (!this.props.qrCodeSuccessCallback) {
throw "qrCodeSuccessCallback is required callback.";
}
};

export default Html5QrcodePlugin;
this.html5QrcodeScanner = new Html5QrcodeScanner(
qrcodeRegionId,
config,
verbose
);
this.html5QrcodeScanner.render(
this.props.qrCodeSuccessCallback,
this.props.qrCodeErrorCallback
);
}
}

export default Html5QrcodePlugin;
3 changes: 1 addition & 2 deletions projects/mybyte/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,7 @@ export const AuthContextProvider = ({
*/
const checkinUser = async (userid: string) => {
try {
const docRef = doc(registerRef, userid);
await updateDoc(docRef, {
await updateDoc(doc(registerRef, user.uid ? user.uid : ""), {
checkedIn: true,
});
setUserInformation(userid);
Expand Down
26 changes: 16 additions & 10 deletions projects/mybyte/pages/qrRead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import OrganizerRoute from "../components/OrganizerRoute";
import { useAuth } from "../context/AuthContext";

export default function QrRead(props: any) {
const {isUserCheckedIn, checkoutUser, checkinUser, removePoints, givePoints } = useAuth();
const {
isUserCheckedIn,
checkoutUser,
checkinUser,
removePoints,
givePoints,
} = useAuth();
const [data, setData] = useState("No result");
const [status, setStatus] = useState("Waiting for scan");
const ref = React.useRef<Html5QrcodePlugin | null>(null);
Expand Down Expand Up @@ -47,22 +53,22 @@ export default function QrRead(props: any) {
switch (val) {
case "checkin-first-day":
if (await isUserCheckedIn(uid)) {
setStatus("User is already checked in!")
setStatus("User is already checked in!");
return;
}

await checkinUser(uid)
givePoints(uid, 100).then(callback)
await checkinUser(uid);
givePoints(uid, 100).then(callback);
break;
case "checkin-other":
if (await isUserCheckedIn(uid)) {
setStatus("User is already checked in!")
setStatus("User is already checked in!");
return;
}
checkinUser(uid)
checkinUser(uid);
break;
case "checkout":
checkoutUser(uid)
checkoutUser(uid);
break;
case "side-event":
givePoints(uid, 250);
Expand Down Expand Up @@ -110,13 +116,13 @@ export default function QrRead(props: any) {
givePoints(uid, 0);
break;
}
setStatus("Successfully completed " + val)
setStatus("Successfully completed " + val);
} catch (error) {
if (error instanceof Error) {
setStatus(error.message)
setStatus(error.message);
}
}
callback()
callback();
};
return (
<OrganizerRoute>
Expand Down

0 comments on commit 89bbff9

Please sign in to comment.