Skip to content

Commit

Permalink
feat: fetch checked in rpkm
Browse files Browse the repository at this point in the history
  • Loading branch information
PhorDotC committed Jul 24, 2024
1 parent 4b3568b commit 2ddca7a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 14 deletions.
32 changes: 28 additions & 4 deletions src/app/(main)/home/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -36,12 +36,35 @@ export default function Home() {
>('first-date');
const [joinModal, setJoinModal] = useState<boolean>(false);
const [announce, setAnnounce] = useState<boolean>(false);
const [Isjoined, setIsJoined] = useState<boolean>(false);
const [isCheckedIn, setIsCheckedIn] = useState<boolean>(false);
const [isJoined, setIsJoined] = useState<boolean>(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 () => {
Expand Down Expand Up @@ -132,6 +155,7 @@ export default function Home() {
registered={!!user && isUserRegistered(user)}
setWaitModal={setWaitModal}
setEvent={setInterestedEvent}
isCheckedIn={isCheckedIn}
setJoinModal={setJoinModal}
setAnnounce={setAnnounce}
>
Expand Down Expand Up @@ -195,7 +219,7 @@ export default function Home() {
modal={joinModal}
setModal={setJoinModal}
announce={announce}
isJoined={Isjoined}
isJoined={isJoined}
checkInConfirm={checkInConfirm}
/>
</>
Expand Down
9 changes: 3 additions & 6 deletions src/components/(main)/home/CustomButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useAuth } from '@/context/AuthContext';
import { useBaan } from '@/context/BaanContext';
import { cn } from '@/lib/utils';
import { createEbookCount } from '@/utils/count';
Expand All @@ -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;
Expand All @@ -23,13 +23,13 @@ const CustomButton: React.FC<CustomButtonProps> = ({
children,
registered,
currentDate,
isCheckedIn,
setWaitModal,
setEvent,
setJoinModal,
setAnnounce,
}) => {
const router = useRouter();
const { user } = useAuth();
const { isConfirmed } = useBaan();
const firstdate = async () => {
let firstDateDate = currentDate;
Expand Down Expand Up @@ -73,10 +73,7 @@ const CustomButton: React.FC<CustomButtonProps> = ({
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);
Expand Down
24 changes: 23 additions & 1 deletion src/dtos/checkInsDTO.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CheckIn, ChildCheckIn } from '@/types/checkIn';
import { CheckIn, ChildCheckIn, GetCheckIn } from '@/types/checkIn';

export type ChildCheckInDTO = {
email: string;
Expand All @@ -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 => {
Expand All @@ -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,
}));
};
9 changes: 9 additions & 0 deletions src/types/checkIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
33 changes: 30 additions & 3 deletions src/utils/checkin.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -34,3 +38,26 @@ export const createCheckIn = async (
return null;
}
};

export const fetchCheckIn = async (): Promise<GetCheckIn[] | null> => {
const accessToken = await getAccessToken();
const userId = await getUserId();

if (!accessToken || !userId) {
return null;
}

try {
const res: AxiosResponse<GetCheckInDTO> = await apiClient.get(
`/checkin/${userId}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
return GetCheckInParser(res.data);
} catch (error) {
return null;
}
};

0 comments on commit 2ddca7a

Please sign in to comment.