Skip to content

Commit

Permalink
[FIX] useCountdownTimer 서버 시간 오차 이슈 해결 (#48)
Browse files Browse the repository at this point in the history
* fix: useCountdownTimer 서버 시간 오차 이슈 해결

* fix: lint 에러 수정

* feat: api params 수정 대응
  • Loading branch information
haejinyun authored Oct 10, 2024
1 parent e74c4bc commit f207280
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/apis/types/basicAuction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface PostBasicAuctionResponse extends CommonResponse {
// -----
export interface GetBasicAuctionListParams {
title?: string;
sort?: string[];
sort?: string;
page?: number;
size?: number;
}
2 changes: 1 addition & 1 deletion src/app/basicauction/basicauction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function BasicAuction() {

const { data } = useGetBasicAuctionList({
title: searchKeywordParam || '',
sort: ['endDate,DESC', 'startDate,ASC'],
// sort: '', // ['endDate,DESC', 'startDate,ASC'],
page: 0,
size: 10,
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import AuctionList from '@/components/AuctionList';
export default function Home() {
const { data } = useGetBasicAuctionList({
title: '',
sort: ['endDate,DESC', 'startDate,ASC'],
// sort: '', // ['endDate,DESC', 'startDate,ASC'],
page: 0,
size: 4,
});
Expand Down
4 changes: 1 addition & 3 deletions src/components/AuctionInfo/AuctionCountdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ function AuctionCountdown({ endTime, setExpired }: AuctionCountdownProps) {
const { isTimeout, day, hour, minute, second } = useCountdownTimer({ endTime });

useEffect(() => {
if (isTimeout) {
setExpired(true);
}
setExpired(isTimeout);
}, [isTimeout, setExpired]);

return <div className={S.countdownStyle}>{`${day}${hour}시간 ${minute}${second}초`}</div>;
Expand Down
29 changes: 10 additions & 19 deletions src/hooks/useCountdownTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { useEffect, useState } from 'react';

import calcRemainingTime from '@/utils/calcRemainingTime';

interface UseCountdownTimerProps {
endTime: Date | string;
}
Expand All @@ -11,23 +13,12 @@ const HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
const DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;

function useCountdownTimer({ endTime }: UseCountdownTimerProps) {
const countRemainingTime = (endTimeInCountRemainingTime: Date) => {
const nowTime = new Date();

if (endTimeInCountRemainingTime.getTime() < nowTime.getTime()) {
return 0;
}

return endTimeInCountRemainingTime.getTime() - nowTime.getTime();
};

const [remainingTime, setRemainingTime] = useState<number>(() =>
countRemainingTime(new Date(endTime)),
);
const [remainingTime, setRemainingTime] = useState<number>(0);

useEffect(() => {
setRemainingTime(calcRemainingTime(new Date(endTime)));
const intervalID = setInterval(() => {
const remainingTimeInUseEffect = countRemainingTime(new Date(endTime));
const remainingTimeInUseEffect = calcRemainingTime(new Date(endTime));

if (remainingTimeInUseEffect <= 0) {
clearInterval(intervalID);
Expand All @@ -41,12 +32,12 @@ function useCountdownTimer({ endTime }: UseCountdownTimerProps) {
return () => {
clearInterval(intervalID);
};
});
}, []);

const day = Math.floor(remainingTime / DAY_IN_MILLIS);
const hour = Math.floor((remainingTime % DAY_IN_MILLIS) / HOUR_IN_MILLIS);
const minute = Math.floor((remainingTime % HOUR_IN_MILLIS) / MINUTE_IN_MILLIS);
const second = Math.floor((remainingTime % MINUTE_IN_MILLIS) / 1000);
const day = Math.floor(remainingTime / DAY_IN_MILLIS) || '-';
const hour = Math.floor((remainingTime % DAY_IN_MILLIS) / HOUR_IN_MILLIS) || '-';
const minute = Math.floor((remainingTime % HOUR_IN_MILLIS) / MINUTE_IN_MILLIS) || '-';
const second = Math.floor((remainingTime % MINUTE_IN_MILLIS) / 1000) || '-';
const isTimeout = remainingTime <= 0;

return { isTimeout, remainingTime, day, hour, minute, second };
Expand Down
11 changes: 11 additions & 0 deletions src/utils/calcRemainingTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function calcRemainingTime(endTimeInCountRemainingTime: Date) {
const nowTime = new Date();

if (endTimeInCountRemainingTime.getTime() < nowTime.getTime()) {
return 0;
}

return endTimeInCountRemainingTime.getTime() - nowTime.getTime();
}

export default calcRemainingTime;

0 comments on commit f207280

Please sign in to comment.