Skip to content

Commit

Permalink
Implement checkin page with manual entry
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorkw7 committed Jan 16, 2024
1 parent 8bf036e commit cbc090b
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/pages/express.tsx
Original file line number Diff line number Diff line change
@@ -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<CheckinProps> = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<ExpressCheckInRequest>();

const onSubmit: SubmitHandler<ExpressCheckInRequest> = ({ 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 (
<VerticalForm onEnterPress={handleSubmit(onSubmit)}>
<SignInTitle text="Express Check-In" />
<SignInFormItem
icon={<AiOutlineMail />}
element="input"
name="email"
type="email"
placeholder="Email ([email protected])"
formRegister={register('email', {
validate: email => {
const validation = ValidationService.isValidEmail(email);
return validation.valid || validation.error;
},
})}
error={errors.email}
/>
<SignInFormItem
icon={<AiOutlineKey />}
element="input"
name="checkin code"
type="checkin code"
placeholder="Checkin Code"
formRegister={register('attendanceCode', {
required: 'Required',
})}
error={errors.attendanceCode}
/>
<SignInButton
type="button"
display="button1"
text="Submit"
onClick={handleSubmit(onSubmit)}
/>
</VerticalForm>
);
};

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,
},
};
};

0 comments on commit cbc090b

Please sign in to comment.