-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPassScanner.tsx
More file actions
210 lines (196 loc) · 5.32 KB
/
PassScanner.tsx
File metadata and controls
210 lines (196 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"use client";
import { useState, useEffect } from "react";
import { Scanner } from "@yudiel/react-qr-scanner";
import superjson from "superjson";
import { createScan } from "@/actions/admin/scanner-admin-actions";
import { useAction } from "next-safe-action/hooks";
import { type QRDataInterface } from "@/lib/utils/shared/qr";
import type { Scan, Event, Hacker } from "db/types";
import c from "config";
import {
Drawer,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
} from "ui/components/drawer";
import { Button } from "ui/components/button";
import Link from "next/link";
import { useRouter, usePathname, useSearchParams } from "next/navigation";
import { toast } from "sonner";
/*
Pass Scanner Props:
eventName: name of the event that the user is scanning into
hasScanned: if the state has eventered one in which a QR has been scanned (whether that scan has scanned before or not)
scan: the scan object that has been scanned. If they have not scanned before scan will be null leading to a new record or if they have then it will incriment the scan count.
*/
interface PassScannerProps {
event: Event;
hasScanned: boolean;
scan: Scan | null;
scanUser: Hacker | null;
}
export default function PassScanner({
event,
hasScanned,
scan,
scanUser,
}: PassScannerProps) {
const [scanLoading, setScanLoading] = useState(false);
const { execute: runScanAction } = useAction(createScan, {});
useEffect(() => {
if (hasScanned) {
setScanLoading(false);
}
}, [hasScanned]);
const searchParams = useSearchParams();
const path = usePathname();
const router = useRouter();
const register = scanUser?.checkinTimestamp
? "Checked in!"
: "Not Checked In";
const guild =
Object.keys(c.groups)[scanUser?.hackerData.group || 0] ?? "None";
const role = scanUser?.role?.name ? scanUser?.role?.name : "Not Found";
function handleScanCreate() {
const params = new URLSearchParams(searchParams.toString());
const timestamp = parseInt(params.get("createdAt") as string);
if (isNaN(timestamp)) {
return alert("Invalid QR Code Data (Field: createdAt)");
}
if (scan) {
runScanAction({
eventID: event.id,
userID: scan.userID,
countToSet: scan.count + 1,
alreadyExists: true,
creationTime: new Date(timestamp),
});
} else {
// TODO: make this a little more typesafe
runScanAction({
eventID: event.id,
userID: scanUser?.clerkID as string,
countToSet: 1,
alreadyExists: false,
creationTime: new Date(timestamp),
});
}
toast.success("Successfully Scanned User In");
router.replace(`${path}`);
}
return (
<>
<div className="flex h-dvh flex-col items-center justify-center pt-32">
<div className="flex w-screen flex-col items-center justify-center gap-5">
<div className="mx-auto aspect-square w-screen max-w-[500px] overflow-hidden">
<Scanner
onScan={(result) => {
const params = new URLSearchParams(
searchParams.toString(),
);
if (!params.has("user")) {
setScanLoading(true);
const qrParsedData =
superjson.parse<QRDataInterface>(
result[0].rawValue,
);
params.set("user", qrParsedData.userID);
params.set(
"createdAt",
qrParsedData.createdAt
.getTime()
.toString(),
);
router.replace(
`${path}?${params.toString()}`,
);
}
}}
onError={(error) => console.log(error)}
styles={{
container: {
width: "100vw",
maxWidth: "500px",
margin: "0",
},
}}
/>
</div>
<div className="mx-auto flex w-screen max-w-[500px] justify-center gap-x-2 overflow-hidden">
<Link href={"/admin/events"}>
<Button>Return To Events</Button>
</Link>
</div>
</div>
</div>
<Drawer
onClose={() => router.replace(path)}
open={hasScanned || scanLoading}
>
<DrawerContent>
{scanLoading ? (
<>
<DrawerHeader>
<DrawerTitle>Loading Scan...</DrawerTitle>
<DrawerDescription></DrawerDescription>
</DrawerHeader>
<DrawerFooter>
<Button
onClick={() => router.replace(path)}
variant="outline"
>
Cancel
</Button>
</DrawerFooter>
</>
) : (
<>
<DrawerHeader>
<DrawerTitle>
New Scan for {event.title}
</DrawerTitle>
<DrawerDescription className="flex flex-col">
<>
{scanUser?.firstName}{" "}
{scanUser?.lastName}
</>
<h2>
<span className="font-bold">Role:</span>{" "}
{role}
</h2>
<h2>
<span className="font-bold">
Status:
</span>{" "}
{register}
</h2>
<h2>
<span className="font-bold">
Guild:
</span>{" "}
{guild}
</h2>
</DrawerDescription>
</DrawerHeader>
<DrawerFooter>
<Button onClick={() => handleScanCreate()}>
{scan
? "Add Additional Scan"
: "Scan User In"}
</Button>
<Button
onClick={() => router.replace(path)}
variant="outline"
>
Cancel
</Button>
</DrawerFooter>
</>
)}
</DrawerContent>
</Drawer>
</>
);
}