diff --git a/src/pages/express.tsx b/src/pages/express.tsx new file mode 100644 index 00000000..3beb1291 --- /dev/null +++ b/src/pages/express.tsx @@ -0,0 +1,96 @@ +import { SignInButton, SignInFormItem, SignInTitle } from '@/components/auth'; +import { VerticalForm } from '@/components/common'; +import { config, showToast } from '@/lib'; +import { EventManager } from '@/lib/managers'; +import { CookieService, ValidationService } from '@/lib/services'; +import type { ExpressCheckInRequest } from '@/lib/types/apiRequests'; +import { CookieType } from '@/lib/types/enums'; +import { getMessagesFromError } from '@/lib/utils'; +import type { GetServerSideProps, NextPage } from 'next'; +import { SubmitHandler, useForm } from 'react-hook-form'; +import { AiOutlineKey, AiOutlineMail } from 'react-icons/ai'; + +interface CheckinProps {} + +const ExpressCheckInPage: NextPage = () => { + const { + register, + handleSubmit, + formState: { errors }, + } = useForm(); + + const onSubmit: SubmitHandler = ({ attendanceCode, email }) => { + EventManager.expressCheckIn({ + attendanceCode, + email, + onSuccessCallback: response => { + const title = `Checked in to ${response.title}!`; + const subtitle = `Thanks for checking in! You earned ${response.pointValue} points.`; + showToast(title, subtitle); + }, + onFailCallback: error => { + showToast('Unable to express check in', getMessagesFromError(error)[0]); + }, + }); + }; + + return ( + + + } + element="input" + name="email" + type="email" + placeholder="Email (user@ucsd.edu)" + formRegister={register('email', { + validate: email => { + const validation = ValidationService.isValidEmail(email); + return validation.valid || validation.error; + }, + })} + error={errors.email} + /> + } + element="input" + name="checkin code" + type="checkin code" + placeholder="Checkin Code" + formRegister={register('attendanceCode', { + required: 'Required', + })} + error={errors.attendanceCode} + /> + + + ); +}; + +export default ExpressCheckInPage; + +export const getServerSideProps: GetServerSideProps = async ({ req, res, query }) => { + const user = CookieService.getServerCookie(CookieType.USER, { req, res }); + const authToken = CookieService.getServerCookie(CookieType.ACCESS_TOKEN, { req, res }); + + const route = query?.destination ? decodeURIComponent(query?.destination as string) : null; + if (user && authToken) { + return { + redirect: { + destination: route || config.homeRoute, + permanent: false, + }, + }; + } + + return { + props: { + destination: route || config.homeRoute, + }, + }; +};