-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCheckinScanner.tsx
More file actions
230 lines (217 loc) · 5.65 KB
/
CheckinScanner.tsx
File metadata and controls
230 lines (217 loc) · 5.65 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use client";
import { useState, useEffect } from "react";
import { Scanner } from "@yudiel/react-qr-scanner";
import superjson from "superjson";
import { checkInUserToHackathon } from "@/actions/admin/scanner-admin-actions";
import { type QRDataInterface } from "@/lib/utils/shared/qr";
import type { User } from "db/types";
import clsx from "clsx";
import { useAction } from "next-safe-action/hooks";
import {
Drawer,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
} from "ui/components/drawer";
import { Button } from "ui/components/button";
import { useRouter, usePathname, useSearchParams } from "next/navigation";
import { toast } from "sonner";
import { FIVE_MINUTES_IN_MILLISECONDS } from "@/lib/constants";
import { ValidationErrors } from "next-safe-action";
interface CheckinScannerProps {
hasScanned: boolean;
checkedIn: boolean | null;
scanUser: User | null;
hasRSVP: boolean | null;
}
export default function CheckinScanner({
hasScanned,
checkedIn,
scanUser,
hasRSVP,
}: CheckinScannerProps) {
console.log("scanner props is: ", hasScanned, checkedIn, scanUser, hasRSVP);
const [scanLoading, setScanLoading] = useState(false);
useEffect(() => {
if (hasScanned) {
setScanLoading(false);
}
}, [hasScanned]);
const searchParams = useSearchParams();
const path = usePathname();
const router = useRouter();
function handleUseActionFeedback(hasErrored = false, message = "") {
console.log("called");
toast.dismiss();
hasErrored
? toast.error(message || "Failed to Check User Into Hackathon")
: toast.success(
message || "Successfully Checked User Into Hackathon!",
);
router.replace(`${path}`);
}
const { execute: runCheckInUserToHackathon } = useAction(
checkInUserToHackathon,
{
onSuccess: () => {
handleUseActionFeedback();
},
onError: ({ error, input }) => {
console.log("error is: ", error);
console.log("input is: ", input);
if (error.validationErrors?.QRTimestamp?._errors) {
handleUseActionFeedback(
true,
error.validationErrors.QRTimestamp._errors[0],
);
} else {
handleUseActionFeedback(true);
}
},
},
);
function handleScanCreate() {
const params = new URLSearchParams(searchParams.toString());
const timestamp = parseInt(params.get("createdAt") as string);
if (!scanUser) {
return alert("User Not Found");
}
if (isNaN(timestamp)) {
return alert("Invalid QR Code Data (Field: createdAt)");
}
if (Date.now() - timestamp > FIVE_MINUTES_IN_MILLISECONDS) {
return alert(
"QR Code has expired. Please tell user to refresh the QR Code",
);
}
if (checkedIn) {
return alert("User Already Checked in!");
} else {
toast.loading("Checking User In");
runCheckInUserToHackathon({
userID: scanUser.clerkID,
QRTimestamp: timestamp,
});
}
router.replace(path);
}
const drawerTitle = checkedIn
? "User Already Checked In"
: !hasRSVP
? "Warning!"
: "New Scan";
const drawerDescription = checkedIn
? "If this is a mistake, please talk to an admin"
: !hasRSVP
? `${scanUser?.firstName} ${scanUser?.lastName} Is not RSVP'd`
: `New scan for ${scanUser?.firstName} ${scanUser?.lastName}`;
const drawerFooterButtonText = checkedIn
? "Close"
: !hasRSVP
? "Check In Anyways"
: "Scan User In";
return (
<>
<div className="flex h-full flex-col items-center justify-center pt-32">
<div className="flex w-full flex-col items-center justify-center gap-5">
<div className="mx-auto aspect-square w-full 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: "100%",
maxWidth: "500px",
margin: "0",
},
}}
/>
</div>
</div>
</div>
<Drawer
onClose={() => router.replace(path)}
open={hasScanned || scanLoading}
>
<DrawerContent>
{scanLoading ? (
<>
<DrawerHeader>
<DrawerTitle>Loading Scan...</DrawerTitle>
</DrawerHeader>
<DrawerFooter>
<Button
onClick={() => router.replace(path)}
variant="outline"
>
Cancel
</Button>
</DrawerFooter>
</>
) : (
<>
<DrawerHeader>
<DrawerTitle
className={clsx("mx-auto", {
"text-red-500": !hasRSVP || checkedIn,
})}
>
{drawerTitle}
</DrawerTitle>
</DrawerHeader>
<DrawerDescription className="mx-auto">
{drawerDescription}
</DrawerDescription>
<DrawerFooter>
{!hasRSVP && !checkedIn && (
<div className="mx-auto">
Do you wish to proceed?
</div>
)}
{!checkedIn && (
<Button
onClick={() => {
handleScanCreate();
}}
variant="outline"
>
{drawerFooterButtonText}
</Button>
)}
<Button
onClick={() => router.replace(path)}
variant="outline"
>
Cancel
</Button>
</DrawerFooter>
</>
)}
</DrawerContent>
</Drawer>
</>
);
}