Skip to content

Commit

Permalink
Work on event scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmonisit committed Mar 19, 2024
1 parent 2d4524b commit 7cd38e5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
6 changes: 2 additions & 4 deletions app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default function Dashboard() {
);
}

if (userData?.role['organizer']) {
if (!userData?.role['organizer']) {
return (<OrganizerView />)
}
else if (userData?.role['director']) {
Expand Down Expand Up @@ -203,17 +203,15 @@ export default function Dashboard() {
</div>
</CardHeader>
<CardContent>
<div className="flex flex-col items-center justify-center space-y-4">
<div className="flex flex-col items-center justify-center space-y-4 bg-white p-4 rounded-md">
<QRCode value={userData?.email} size={256} />
</div>
</CardContent>
</Card>

<Card className="w-full max-w-2xl">
<form onSubmit={handleSubmit(onSubmit)}>

<CardHeader>

<div className="flex flex-row items-center justify-center">
<div className='flex flex-col'>
<CardTitle>Profile</CardTitle>
Expand Down
17 changes: 15 additions & 2 deletions app/dashboard/views/organizerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function ScanStatus(props: { status: STATUS, scanType: ScannerTab }) {
}
{
status === "PENDING" &&
<p className="text-orange-400">Come again later at ${timeWhenAllHackersCanComeThrough.toString()}</p>
<p className="text-orange-400">Come again later at {timeWhenAllHackersCanComeThrough.toString()}</p>
}
{
status === "FAILED" &&
Expand All @@ -61,8 +61,10 @@ function OrganizerView() {
const [openScanner, setOpenScanner] = useState<boolean>(false);
const [scannerTab, setScannerTab] = useState<ScannerTab>("CHECK IN");
const [selectedEvent, setSelectedEvent] = useState<string>("");
const [scanResponse, setScanResponse] = useState<string>("");

const handleOnScan = async (result: string) => {
setStatus("AWAITING RESPONSE");
if (scannerTab === "CHECK IN") {
const resp = await GetUser(result);
if (typeof resp.response === 'string') {
Expand All @@ -84,7 +86,15 @@ function OrganizerView() {
alert("Please select an event first!");
}

AttendEventScan(selectedEvent, result);
const resp = await AttendEventScan(result, selectedEvent);
if (resp.error !== '') {
setStatus("FAILED");
setScanResponse(resp.error);
return;
}

setScanResponse(resp.response);
setStatus("SUCCESSFUL");
}
};

Expand Down Expand Up @@ -127,6 +137,9 @@ function OrganizerView() {
onChange={handleEventSelectChange}
/>
}
<p className="text-center">
{scanResponse}
</p>
<button
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mt-10"
onClick={() => setOpenScanner(!openScanner)}
Expand Down
29 changes: 23 additions & 6 deletions app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,13 @@ export async function UploadResume(file: FormData) {
export async function AttendEventScan(
scannedEmail: string,
event: string,
): Promise<string> {
): Promise<{
error: string;
response: string;
}> {
const session = await auth();
let response_message = '';
let error_message = '';

if (session?.user) {
const { email, name } = session.user;
Expand All @@ -546,7 +551,7 @@ export async function AttendEventScan(
token: name,
qr: scannedEmail,
event: event,
again: '',
again: false,
};

const resp = await fetch(ENDPOINTS.attend, {
Expand All @@ -557,15 +562,27 @@ export async function AttendEventScan(
body: JSON.stringify(content),
});

const json = await resp.json();

const status = resp.status;
if (status === 200) {
return `${scannedEmail} successfully logged in to {event}!`;
if (!json.body || !('email' in json.body) || 'new_count' in json.body) {
error_message = `An error occured when attempting to attend event. Invalid response.`;
} else {
response_message = `${json.body['email']} successfully logged in to ${event}!
Count: ${json.body['new_count']}`;
}
} else if (status === 402) {
return `User ${scannedEmail} is already checked in to {event}!`;
error_message = `User ${scannedEmail} is already checked in to ${event}!`;
} else if (status === 404) {
return `USER not FOUND. Please try again.`;
error_message = `USER not FOUND. Please try again.`;
}
} else {
error_message = 'Invalid user session. Please login.';
}

return 'Invalid user session. Please login.';
return {
error: error_message,
response: response_message,
};
}

0 comments on commit 7cd38e5

Please sign in to comment.