From a2b9923789a99af067ac8f5299b48850041b580f Mon Sep 17 00:00:00 2001 From: teegoood Date: Tue, 30 Jul 2024 16:25:05 +0700 Subject: [PATCH 01/10] feat: redirect staff to rpkm page base on time --- src/context/AuthContext.tsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index 7142ed1d..044cbf83 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -67,11 +67,16 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ const isStaffPage = path.includes('/staff'); const isRegistered = isUserRegistered(userObj); const isRegisterPage = path.includes('register'); + const currentTime = (await getCurrentTime()).currentTime; if (isStaff) { // staff route protection + const isRpkm = + new Date(process.env.NEXT_PUBLIC_RPKM_DAY_1 as string) < currentTime; + const homePage = isRpkm ? '/rpkm/staff/home' : '/firstdate/staff/home'; + if (path == '/') { - return router.push('firstdate/staff/home'); + return router.push(homePage); } if (!isRegistered && !isRegisterPage) { @@ -79,7 +84,14 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ } if (!isStaffPage) { - return router.push('/firstdate/staff/home'); + return router.push(homePage); + } + + if ( + (isRpkm && !path.includes('rpkm')) || + (!isRpkm && !path.includes('firstdate')) + ) { + return router.push(homePage); } } else { // user route protection @@ -120,8 +132,6 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ process.env.NEXT_PUBLIC_FRESHY_NIGHT_DATE as string ); - const currentTime = (await getCurrentTime()).currentTime; - //firstdate if (path.includes('firstdate')) { if (currentTime < firstdate) { From 6c908d7bf74ee52bbf633d77fcf582cc55238fa6 Mon Sep 17 00:00:00 2001 From: teegoood Date: Tue, 30 Jul 2024 17:18:37 +0700 Subject: [PATCH 02/10] add: staff rpkm home update event every second --- src/app/rpkm/staff/home/page.tsx | 27 ++++-------- .../rpkm/staff/home/qrscanner/QRScanner.tsx | 41 ++++++++++++++----- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/app/rpkm/staff/home/page.tsx b/src/app/rpkm/staff/home/page.tsx index 4ebfad47..cedabccc 100644 --- a/src/app/rpkm/staff/home/page.tsx +++ b/src/app/rpkm/staff/home/page.tsx @@ -6,10 +6,9 @@ import React, { useEffect, useState } from 'react'; function Page() { const [eventText, setEventText] = useState(''); - const [event, setEvent] = useState(''); useEffect(() => { - const checkEvent = async () => { + const intervalId = setInterval(async () => { const currentTime = (await getCurrentTime()).currentTime; const freshy_night_time = new Date( process.env.NEXT_PUBLIC_FRESHY_NIGHT_EVENT as string @@ -21,26 +20,18 @@ function Page() { process.env.NEXT_PUBLIC_RPKM_DAY_2 as string ); - if (currentTime > freshy_night_time) { + if (currentTime >= freshy_night_time) { setEventText('Freshy Night'); - setEvent('freshy-night'); - return; - } - - if (currentTime > rpkm_day_2_time) { + } else if (currentTime >= rpkm_day_2_time) { setEventText('Onsite 4 สิงหาคม 2567'); - setEvent('rpkm-day-2'); - return; - } - - if (currentTime > rpkm_day_1_time) { + } else if (currentTime >= rpkm_day_1_time) { setEventText('Onsite 3 สิงหาคม 2567'); - setEvent('rpkm-day-1'); - return; } - }; + }, 1000); - checkEvent(); + return () => { + clearInterval(intervalId); + }; }, []); return ( @@ -74,7 +65,7 @@ function Page() { {eventText}
- +
QR-Reader
diff --git a/src/components/rpkm/staff/home/qrscanner/QRScanner.tsx b/src/components/rpkm/staff/home/qrscanner/QRScanner.tsx index a5e6a368..2fc6fe7f 100644 --- a/src/components/rpkm/staff/home/qrscanner/QRScanner.tsx +++ b/src/components/rpkm/staff/home/qrscanner/QRScanner.tsx @@ -8,10 +8,9 @@ import FailureModal from './failureModal'; import { createCheckIn } from '@/utils/checkin'; import { CheckIn } from '@/types/checkIn'; import dayjs from 'dayjs'; -interface ScanProp { - event: string; -} -const Scan: React.FC = ({ event }) => { +import { getCurrentTime } from '@/utils/time'; + +const Scan: React.FC = () => { const [checkInData, setCheckInData] = useState(null); const [taken, setTaken] = useState(false); const [status, setStatus] = useState<'success' | 'error' | 'idle'>('idle'); @@ -25,6 +24,28 @@ const Scan: React.FC = ({ event }) => { return; } + let event = ''; + const currentTime = (await getCurrentTime()).currentTime; + + //need to check date every time because qr component don't update function when function re-render + const freshy_night_time = new Date( + process.env.NEXT_PUBLIC_FRESHY_NIGHT_EVENT as string + ); + const rpkm_day_1_time = new Date( + process.env.NEXT_PUBLIC_RPKM_DAY_1 as string + ); + const rpkm_day_2_time = new Date( + process.env.NEXT_PUBLIC_RPKM_DAY_2 as string + ); + + if (currentTime > freshy_night_time) { + event = 'freshy-night'; + } else if (currentTime > rpkm_day_2_time) { + event = 'rpkm-day-2'; + } else if (currentTime > rpkm_day_1_time) { + event = 'rpkm-day-1'; + } + const userId = scanRawData.text; const newCheckInData: CheckIn | null = await createCheckIn( userId, @@ -65,13 +86,11 @@ const Scan: React.FC = ({ event }) => { return (
- {event != '' && ( - - )} + Date: Tue, 30 Jul 2024 17:40:29 +0700 Subject: [PATCH 03/10] feat: handle end freshynight register --- .../freshy-night/confirm-register/page.tsx | 29 +++++++++++++++++-- .../rpkm/Sidebar/FreshyNightLink.tsx | 21 ++++++++++++-- src/components/rpkm/Sidebar/Menu.tsx | 7 ++--- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/app/rpkm/freshy-night/confirm-register/page.tsx b/src/app/rpkm/freshy-night/confirm-register/page.tsx index 360bd5b4..3fab94ea 100644 --- a/src/app/rpkm/freshy-night/confirm-register/page.tsx +++ b/src/app/rpkm/freshy-night/confirm-register/page.tsx @@ -1,6 +1,6 @@ 'use client'; -import { useState, ChangeEvent } from 'react'; +import { useState, ChangeEvent, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { useAuth } from '@/context/AuthContext'; import { getAccessToken } from '@/utils/auth'; @@ -16,6 +16,7 @@ import Spinner from '@/components/firstdate/Spinner'; import UserCard from '@/components/UserCard'; import ConfirmRegisterModal from '@/components/rpkm/freshy-night/ConfirmRegisterModal'; import { createCheckIn } from '@/utils/checkin'; +import { getCurrentTime } from '@/utils/time'; type RegisterUser = Pick< UserDTO, @@ -54,6 +55,16 @@ export default function Page() { }); const [errors, setErrors] = useState([]); const [isModalOpen, setIsModalOpen] = useState(false); + const [currentTime, setCurrentTime] = useState(null); + + useEffect(() => { + const initialize = async () => { + const now = (await getCurrentTime()).currentTime; + setCurrentTime(now); + }; + + initialize(); + }, []); const handleInputChange = ( e: ChangeEvent @@ -108,8 +119,20 @@ export default function Page() { } } - const handleConfirmBtn = () => { - if (!validateForm()) return; + const handleConfirmBtn = async () => { + if (!validateForm()) { + return; + } + + const freshyNightEndDate = new Date( + process.env.NEXT_PUBLIC_END_FRESHY_NIGHT_DATE as string + ); + + if (currentTime && currentTime > freshyNightEndDate) { + toast.error('หมดเวลาลงทะเบียน'); + return; + } + setIsModalOpen(true); }; diff --git a/src/components/rpkm/Sidebar/FreshyNightLink.tsx b/src/components/rpkm/Sidebar/FreshyNightLink.tsx index f7161941..01e672ad 100644 --- a/src/components/rpkm/Sidebar/FreshyNightLink.tsx +++ b/src/components/rpkm/Sidebar/FreshyNightLink.tsx @@ -1,3 +1,4 @@ +import { cn } from '@/lib/utils'; import { GetCheckIn } from '@/types/checkIn'; import { fetchCheckIn } from '@/utils/checkin'; import { getCurrentTime } from '@/utils/time'; @@ -25,10 +26,14 @@ const FreshyNightLink = ({ children }: FreshyNightLinkProps) => { initialize(); }, []); - const freshyNightDate = useMemo(() => { + const freshyNightStartDate = useMemo(() => { return new Date(process.env.NEXT_PUBLIC_FRESHY_NIGHT_DATE as string); }, []); + const freshyNightEndDate = useMemo(() => { + return new Date(process.env.NEXT_PUBLIC_END_FRESHY_NIGHT_DATE as string); + }, []); + const isRegistered = useMemo(() => { return ( !!checkins && @@ -37,7 +42,7 @@ const FreshyNightLink = ({ children }: FreshyNightLinkProps) => { }, [checkins]); const handleOnClick = () => { - if (currentTime && currentTime < freshyNightDate) { + if (currentTime && currentTime < freshyNightStartDate) { router.push('/rpkm/freshy-night/coming-soon'); return; } @@ -50,7 +55,17 @@ const FreshyNightLink = ({ children }: FreshyNightLinkProps) => { router.push('/rpkm/freshy-night/register-done'); }; - return
{children}
; + return ( +
freshyNightEndDate), + })} + > + {children} +
+ ); }; export default FreshyNightLink; diff --git a/src/components/rpkm/Sidebar/Menu.tsx b/src/components/rpkm/Sidebar/Menu.tsx index 85129214..dbd4ee0a 100644 --- a/src/components/rpkm/Sidebar/Menu.tsx +++ b/src/components/rpkm/Sidebar/Menu.tsx @@ -10,12 +10,9 @@ function Menu() { return (
- +
ลงทะเบียนเลือกบ้าน - +
ลงทะเบียน Freshy Night
From a55c98696306043b2657c4fd901ea6a0c62b0e4b Mon Sep 17 00:00:00 2001 From: teegoood Date: Tue, 30 Jul 2024 17:55:13 +0700 Subject: [PATCH 04/10] lint fix --- src/components/rpkm/Sidebar/UserInfo.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/components/rpkm/Sidebar/UserInfo.tsx b/src/components/rpkm/Sidebar/UserInfo.tsx index 7a13adf3..806ce80a 100644 --- a/src/components/rpkm/Sidebar/UserInfo.tsx +++ b/src/components/rpkm/Sidebar/UserInfo.tsx @@ -11,10 +11,12 @@ import { useAuth } from '@/context/AuthContext'; import Link from 'next/link'; import { baanInfos } from '../Baan/baanInfos'; import { getCurrentTime } from '@/utils/time'; +import QrCodeModal from '../freshy-night/QrCodeModal'; function UserInfo() { const { user } = useAuth(); const [currentTime, setCurrentTime] = useState(null); + const [qrModal, setQrModal] = useState(false); const findBaan = (result: string) => { return baanInfos.find((baan) => baan.name.en === result); }; @@ -103,8 +105,8 @@ function UserInfo() {
+
); } From 8b6a2d8f2f46ed8e4a3b9c8391cf4d811ded27d9 Mon Sep 17 00:00:00 2001 From: teegoood Date: Tue, 30 Jul 2024 17:57:12 +0700 Subject: [PATCH 05/10] change: text in freshy night coming soon page --- src/app/rpkm/freshy-night/coming-soon/page.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/rpkm/freshy-night/coming-soon/page.tsx b/src/app/rpkm/freshy-night/coming-soon/page.tsx index fe66915a..1dd4e645 100644 --- a/src/app/rpkm/freshy-night/coming-soon/page.tsx +++ b/src/app/rpkm/freshy-night/coming-soon/page.tsx @@ -22,7 +22,7 @@ export default function ComingSoon() { ตั้งแต่วันที่ 3 สิงหาคม 2567

- เวลา 00:00 เป็นต้นไป + เวลา 12:00 เป็นต้นไป

From cb2087c8a80cf35fe321852f84310339b372f5f7 Mon Sep 17 00:00:00 2001 From: teegoood Date: Tue, 30 Jul 2024 18:07:28 +0700 Subject: [PATCH 06/10] fix: freshy-night comming soon bus image position --- src/app/rpkm/freshy-night/register/page.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/rpkm/freshy-night/register/page.tsx b/src/app/rpkm/freshy-night/register/page.tsx index a4d0403d..ff12f663 100644 --- a/src/app/rpkm/freshy-night/register/page.tsx +++ b/src/app/rpkm/freshy-night/register/page.tsx @@ -8,7 +8,7 @@ export default function Register() { const { button } = modalStyles['red']; return ( -
+
Date: Tue, 30 Jul 2024 18:28:31 +0700 Subject: [PATCH 07/10] fix: rules style --- src/app/rpkm/freshy-night/rules/page.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/rpkm/freshy-night/rules/page.tsx b/src/app/rpkm/freshy-night/rules/page.tsx index bbab0e1b..6715e6a9 100644 --- a/src/app/rpkm/freshy-night/rules/page.tsx +++ b/src/app/rpkm/freshy-night/rules/page.tsx @@ -22,7 +22,7 @@ export default function Rules() { const { button } = modalStyles['red']; return ( -
+
Date: Tue, 30 Jul 2024 19:17:42 +0700 Subject: [PATCH 08/10] config: strict mode false --- next.config.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/next.config.mjs b/next.config.mjs index fa141206..a65756c2 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,7 +1,7 @@ /** @type {import('next').NextConfig} */ const nextConfig = { - reactStrictMode: true, + reactStrictMode: false, compress: true, compiler: { removeConsole: process.env.APP_ENV == 'production', From 5a0ed09ddad19daa81133ddd6a2f0a63dffecc64 Mon Sep 17 00:00:00 2001 From: teegoood Date: Tue, 30 Jul 2024 19:18:08 +0700 Subject: [PATCH 09/10] change: remove interval staff topic --- src/app/rpkm/staff/home/page.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app/rpkm/staff/home/page.tsx b/src/app/rpkm/staff/home/page.tsx index cedabccc..bf854902 100644 --- a/src/app/rpkm/staff/home/page.tsx +++ b/src/app/rpkm/staff/home/page.tsx @@ -8,8 +8,9 @@ function Page() { const [eventText, setEventText] = useState(''); useEffect(() => { - const intervalId = setInterval(async () => { + const initialize = async () => { const currentTime = (await getCurrentTime()).currentTime; + const freshy_night_time = new Date( process.env.NEXT_PUBLIC_FRESHY_NIGHT_EVENT as string ); @@ -27,11 +28,9 @@ function Page() { } else if (currentTime >= rpkm_day_1_time) { setEventText('Onsite 3 สิงหาคม 2567'); } - }, 1000); - - return () => { - clearInterval(intervalId); }; + + initialize(); }, []); return ( From 122b839c9cfbd9a30a81276a2b294399b0c36b5e Mon Sep 17 00:00:00 2001 From: teegoood Date: Tue, 30 Jul 2024 19:21:24 +0700 Subject: [PATCH 10/10] fix: disable qr scanner on modal --- .../rpkm/staff/home/qrscanner/QRScanner.tsx | 13 ++++++++++++- .../rpkm/staff/home/qrscanner/confirmationModal.tsx | 7 ++++++- .../rpkm/staff/home/qrscanner/failureModal.tsx | 9 +++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/components/rpkm/staff/home/qrscanner/QRScanner.tsx b/src/components/rpkm/staff/home/qrscanner/QRScanner.tsx index 2fc6fe7f..5ac7b404 100644 --- a/src/components/rpkm/staff/home/qrscanner/QRScanner.tsx +++ b/src/components/rpkm/staff/home/qrscanner/QRScanner.tsx @@ -1,5 +1,5 @@ 'use client'; -import React, { ReactNode, useState } from 'react'; +import React, { ReactNode, useEffect, useState } from 'react'; import { QrReader } from 'react-qr-reader'; import { motion } from 'framer-motion'; import { useAuth } from '@/context/AuthContext'; @@ -46,6 +46,11 @@ const Scan: React.FC = () => { event = 'rpkm-day-1'; } + //need to use localstorage to prevent user scan qr code multiple time + //i don't useState because it not work with qrscanner + const enable = localStorage.getItem('enable') === 'true'; + if (!enable) return; + const userId = scanRawData.text; const newCheckInData: CheckIn | null = await createCheckIn( userId, @@ -53,6 +58,8 @@ const Scan: React.FC = () => { event ); + localStorage.setItem('enable', 'false'); + if (newCheckInData) { if (newCheckInData.checkIn.isDuplicate) { const date = dayjs(newCheckInData.checkIn.timestamp); @@ -83,6 +90,10 @@ const Scan: React.FC = () => { setStatus('idle'); }; + useEffect(() => { + localStorage.setItem('enable', 'true'); + }, []); + return (
diff --git a/src/components/rpkm/staff/home/qrscanner/confirmationModal.tsx b/src/components/rpkm/staff/home/qrscanner/confirmationModal.tsx index aea851b5..bbd68c8d 100644 --- a/src/components/rpkm/staff/home/qrscanner/confirmationModal.tsx +++ b/src/components/rpkm/staff/home/qrscanner/confirmationModal.tsx @@ -15,6 +15,11 @@ const ConfirmModal: React.FC = ({ onClose, userData, }) => { + const handleOnClose = () => { + onClose(); + localStorage.setItem('enable', 'true'); + }; + const modalClasses = `fixed inset-0 z-50 overflow-y-auto bg-gray-500 bg-opacity-75 transition-all ease-in-out duration-300 ${ isOpen ? 'opacity-100' : 'opacity-0 pointer-events-none' }`; @@ -45,7 +50,7 @@ const ConfirmModal: React.FC = ({
) : (