Skip to content

Commit

Permalink
fix: ntp time
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickChoDev committed Jul 22, 2024
1 parent 8762012 commit 803b65b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
4 changes: 0 additions & 4 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const nextConfig = {
contentDispositionType: 'attachment',
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;",
remotePatterns: [
{
protocol: 'https',
hostname: process.env.BASE_DOMAIN_IMAGES ?? '*',
},
{
protocol: 'https',
hostname: 'rpkm67.spg1.cdn.digitaloceanspaces.com',
Expand Down
18 changes: 16 additions & 2 deletions src/app/(main)/home/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import contactIcon from '@public/home/icon/contactlist.svg';
import qrcodeIcon from '@public/home/icon/qrcode.svg';
import editIcon from '@public/home/icon/edit.svg';
import logoutIcon from '@public/home/icon/logout.svg';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import QrCodeModal from '@/components/(main)/home/QrCodeModal';
import { useAuth } from '@/context/AuthContext';
import { useRouter } from 'next/navigation';
Expand All @@ -19,16 +19,24 @@ import Border from '@/components/firstdate/Border';
import CustomButton from '@/components/(main)/home/CustomButton';
import Link from 'next/link';
import { getMajorNameById } from '@/utils/register';
import { getCurrentTime } from '@/utils/time';

export default function Home() {
const router = useRouter();
const { user, logout } = useAuth();
const [clientTime, setClientTime] = useState(new Date('1980-01-01'));
const [qrModal, setQrModal] = useState<boolean>(false);
const [waitModal, setWaitModal] = useState<boolean>(false);
const [interestedEvent, setInterestedEvent] = useState<
'first-date' | 'rup-peun'
>('first-date');

useEffect(() => {
getCurrentTime().then((res) => {
setClientTime(res.currentTime);
});
}, []);

return (
<>
<Border
Expand Down Expand Up @@ -80,6 +88,7 @@ export default function Home() {

<div className="w-full flex items-center flex-col h-[26.34vh] justify-between font-medium text-[2.2vh]">
<CustomButton
currentDate={clientTime}
varient="first-date"
registered={!!user && isUserRegistered(user)}
setWaitModal={setWaitModal}
Expand All @@ -88,14 +97,18 @@ export default function Home() {
<div>CU First Date 2024</div>
</CustomButton>
<CustomButton
currentDate={clientTime}
varient="rub-peun"
registered={!!user && isUserRegistered(user)}
setWaitModal={setWaitModal}
setEvent={setInterestedEvent}
>
<div>Rub Peun Kao Mai 2024</div>
</CustomButton>
<CustomButton varient="e-book">
<CustomButton
currentDate={clientTime}
varient="e-book"
>
<Image
src={eBookIcon}
alt="E-book Icon"
Expand All @@ -104,6 +117,7 @@ export default function Home() {
<div>E-Book</div>
</CustomButton>
<CustomButton
currentDate={clientTime}
varient="contact-list"
className="text-white"
>
Expand Down
13 changes: 2 additions & 11 deletions src/components/(main)/home/CustomButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { cn } from '@/lib/utils';
import { createEbookCount } from '@/utils/count';
import { useRouter } from 'next/navigation';
import React from 'react';
import { Client as NTPClient } from 'ntp-time';

interface CustomButtonProps {
varient: 'first-date' | 'rub-peun' | 'e-book' | 'contact-list';
className?: string;
children: React.ReactNode;
registered?: boolean;
currentDate: Date;
setWaitModal?: (value: boolean) => void;
setEvent?: (value: 'first-date' | 'rup-peun') => void;
}
Expand All @@ -18,16 +18,12 @@ const CustomButton: React.FC<CustomButtonProps> = ({
className,
children,
registered,
currentDate,
setWaitModal,
setEvent,
}) => {
const router = useRouter();
const firstdate = async () => {
const ntpClient = new NTPClient();
const currentDate = await ntpClient.syncTime().then((time) => {
console.log(time);
return time.time;
});
console.log(currentDate);
let firstDateDate = currentDate;
const firstDate = process.env.NEXT_PUBLIC_FIRST_DATE_DATE;
Expand All @@ -46,11 +42,6 @@ const CustomButton: React.FC<CustomButtonProps> = ({
}
};
const rubpeun = async () => {
const ntpClient = new NTPClient();
const currentDate = await ntpClient.syncTime().then((time) => {
console.log(time);
return time.time;
});
console.log(currentDate);
let rupPeunDate = currentDate;
const rupPeun = process.env.NEXT_PUBLIC_RUP_PEUN_DATE;
Expand Down
13 changes: 13 additions & 0 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use server';
import { Client as NTPClient } from 'ntp-time';

export async function getCurrentTime() {
try {
const ntpClient = new NTPClient();
const currentTime = await ntpClient.syncTime();
return { currentTime: currentTime.time };
} catch (err: unknown) {
console.log(err);
}
return { currentTime: new Date() };
}

0 comments on commit 803b65b

Please sign in to comment.