From 2ddca7a9377c4acefa828b0abd2a36dd62c797e1 Mon Sep 17 00:00:00 2001 From: PhorDotC Date: Thu, 25 Jul 2024 02:06:22 +0700 Subject: [PATCH] feat: fetch checked in rpkm --- src/app/(main)/home/page.tsx | 32 +++++++++++++++++--- src/components/(main)/home/CustomButton.tsx | 9 ++---- src/dtos/checkInsDTO.ts | 24 ++++++++++++++- src/types/checkIn.ts | 9 ++++++ src/utils/checkin.ts | 33 +++++++++++++++++++-- 5 files changed, 93 insertions(+), 14 deletions(-) diff --git a/src/app/(main)/home/page.tsx b/src/app/(main)/home/page.tsx index 8a2f719f..95a42f6e 100644 --- a/src/app/(main)/home/page.tsx +++ b/src/app/(main)/home/page.tsx @@ -21,8 +21,8 @@ import CustomButton from '@/components/(main)/home/CustomButton'; import Link from 'next/link'; import { getMajorNameById } from '@/utils/register'; import { getCurrentTime } from '@/utils/time'; -import { createCheckIn } from '@/utils/checkin'; -import { CheckIn } from '@/types/checkIn'; +import { createCheckIn, fetchCheckIn } from '@/utils/checkin'; +import { CheckIn, GetCheckIn } from '@/types/checkIn'; import toast from 'react-hot-toast'; export default function Home() { @@ -36,12 +36,35 @@ export default function Home() { >('first-date'); const [joinModal, setJoinModal] = useState(false); const [announce, setAnnounce] = useState(false); - const [Isjoined, setIsJoined] = useState(false); + const [isCheckedIn, setIsCheckedIn] = useState(false); + const [isJoined, setIsJoined] = useState(false); useEffect(() => { getCurrentTime().then((res) => { setClientTime(res.currentTime); }); + + const checkedIn = async () => { + if (!user) { + return; + } + + try { + const checkedIns: GetCheckIn[] | null = await fetchCheckIn(); + if (checkedIns) { + const findEvent = !!checkedIns.find( + (checkIn) => checkIn.event === 'confirm-rpkm' + ); + setIsCheckedIn(findEvent); + } else { + throw new Error(''); + } + } catch (e) { + console.log('fetch check in', e); + } + }; + + checkedIn(); }, []); const checkInConfirm = async () => { @@ -132,6 +155,7 @@ export default function Home() { registered={!!user && isUserRegistered(user)} setWaitModal={setWaitModal} setEvent={setInterestedEvent} + isCheckedIn={isCheckedIn} setJoinModal={setJoinModal} setAnnounce={setAnnounce} > @@ -195,7 +219,7 @@ export default function Home() { modal={joinModal} setModal={setJoinModal} announce={announce} - isJoined={Isjoined} + isJoined={isJoined} checkInConfirm={checkInConfirm} /> diff --git a/src/components/(main)/home/CustomButton.tsx b/src/components/(main)/home/CustomButton.tsx index 28a5d48a..c5aa82e3 100644 --- a/src/components/(main)/home/CustomButton.tsx +++ b/src/components/(main)/home/CustomButton.tsx @@ -1,4 +1,3 @@ -import { useAuth } from '@/context/AuthContext'; import { useBaan } from '@/context/BaanContext'; import { cn } from '@/lib/utils'; import { createEbookCount } from '@/utils/count'; @@ -11,6 +10,7 @@ interface CustomButtonProps { children: React.ReactNode; registered?: boolean; currentDate: Date; + isCheckedIn?: boolean; setWaitModal?: (value: boolean) => void; setEvent?: (value: 'first-date' | 'rup-peun') => void; setJoinModal?: (value: boolean) => void; @@ -23,13 +23,13 @@ const CustomButton: React.FC = ({ children, registered, currentDate, + isCheckedIn, setWaitModal, setEvent, setJoinModal, setAnnounce, }) => { const router = useRouter(); - const { user } = useAuth(); const { isConfirmed } = useBaan(); const firstdate = async () => { let firstDateDate = currentDate; @@ -73,10 +73,7 @@ const CustomButton: React.FC = ({ if (!isConfirmed) { router.push('/rpkm/activities/home'); } else if (setJoinModal && setAnnounce) { - const checkedIn = user?.checkIns.find( - (checkIn) => checkIn.event === 'confirm-rpkm' - ); - if (checkedIn) { + if (isCheckedIn) { router.push('/rpkm/activities/home'); } else { setJoinModal(true); diff --git a/src/dtos/checkInsDTO.ts b/src/dtos/checkInsDTO.ts index c6e3a768..1a56ee14 100644 --- a/src/dtos/checkInsDTO.ts +++ b/src/dtos/checkInsDTO.ts @@ -1,4 +1,4 @@ -import { CheckIn, ChildCheckIn } from '@/types/checkIn'; +import { CheckIn, ChildCheckIn, GetCheckIn } from '@/types/checkIn'; export type ChildCheckInDTO = { email: string; @@ -20,6 +20,17 @@ export type CheckInDTO = { lastname: string; }; +export type GetCheckInDTO = { + checkins: { + email: string; + event: string; + id: string; + user_id: string; + timestamp: string; + is_duplicate: boolean; + }[]; +}; + export const ChildCheckInParser = ( checkinDTO: ChildCheckInDTO ): ChildCheckIn => { @@ -45,3 +56,14 @@ export const CheckInParser = (checkinDTO: CheckInDTO): CheckIn => { lastName: checkinDTO.lastname, }; }; + +export const GetCheckInParser = (checkinDTO: GetCheckInDTO): GetCheckIn[] => { + return checkinDTO.checkins.map((checkin) => ({ + email: checkin.email, + event: checkin.event, + id: checkin.id, + userId: checkin.user_id, + timestamp: checkin.timestamp, + isDuplicate: checkin.is_duplicate, + })); +}; diff --git a/src/types/checkIn.ts b/src/types/checkIn.ts index 1a2f00be..b5179782 100644 --- a/src/types/checkIn.ts +++ b/src/types/checkIn.ts @@ -17,3 +17,12 @@ export type CheckIn = { firstName: string; lastName: string; }; + +export type GetCheckIn = { + email: string; + event: string; + id: string; + userId: string; + timestamp: string; + isDuplicate: boolean; +}; diff --git a/src/utils/checkin.ts b/src/utils/checkin.ts index 0e843d98..222e8266 100644 --- a/src/utils/checkin.ts +++ b/src/utils/checkin.ts @@ -1,8 +1,12 @@ import { AxiosResponse } from 'axios'; import { apiClient } from './axios'; -import { getAccessToken } from './auth'; -import { CheckIn } from '@/types/checkIn'; -import { CheckInParser } from '@/dtos/checkInsDTO'; +import { getAccessToken, getUserId } from './auth'; +import { CheckIn, GetCheckIn } from '@/types/checkIn'; +import { + CheckInParser, + GetCheckInDTO, + GetCheckInParser, +} from '@/dtos/checkInsDTO'; export const createCheckIn = async ( userID: string, @@ -34,3 +38,26 @@ export const createCheckIn = async ( return null; } }; + +export const fetchCheckIn = async (): Promise => { + const accessToken = await getAccessToken(); + const userId = await getUserId(); + + if (!accessToken || !userId) { + return null; + } + + try { + const res: AxiosResponse = await apiClient.get( + `/checkin/${userId}`, + { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + } + ); + return GetCheckInParser(res.data); + } catch (error) { + return null; + } +};