Skip to content

Commit

Permalink
[feat/#59]: 5시에 시간 자동 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
Kong-E committed Aug 27, 2024
1 parent 35e4643 commit 5ea64eb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions components/study/Timer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function Timer({ maxTime, currentTime, onSave }: ITimer) {

const startTimeRef = useRef<number | null>(null);
const animationFrameRef = useRef<number | null>(null);
const lastSaveDateRef = useRef<string | null>(null); // 마지막 저장 날짜 -> 중복 저장 방지

const outerRadius = isMobile ? 45 : 51.7;
const innerRadius = isMobile ? 41 : 49.2;
Expand All @@ -44,6 +45,27 @@ export default function Timer({ maxTime, currentTime, onSave }: ITimer) {
}
};

const checkAndSaveAt5AM = () => {
const now = new Date();
const currentDate = now.toDateString();
const hours = now.getHours();
const minutes = now.getMinutes();

if (hours === 5 && (minutes === 0 || minutes === 1) && currentDate !== lastSaveDateRef.current) {
const currentTimeInSeconds = Math.floor((Date.now() - startTimeRef.current!) / 1000);

handleTimer(currentTimeInSeconds, selectedSubjects);
lastSaveDateRef.current = currentDate;

setTime(0);
setProgress(0);
startTimeRef.current = Date.now();

console.log('Saved at 5AM');
alert('새로운 날이 시작되었습니다. 기록이 저장되었습니다.');
}
};

const handleToggle = () => {
setIsActive((prev) => !prev);

Expand All @@ -62,11 +84,14 @@ export default function Timer({ maxTime, currentTime, onSave }: ITimer) {
const newProgress = ((elapsedTime % maxTime) / maxTime) * 100; // 진행률

setTime(newTime);
// Cookies.set('time', newTime.toString());
setProgress(newProgress);

setRemainingTime(maxTime - (newTime % maxTime));

if (newTime % 30 === 0) {
// 30초마다 체크
checkAndSaveAt5AM();
}

animationFrameRef.current = requestAnimationFrame(animate);
};
animationFrameRef.current = requestAnimationFrame(animate);
Expand Down
2 changes: 1 addition & 1 deletion types/commonType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export interface ErrorResponse {
error: {
status: number;
message: string;
errors: Record<string, string>;
errors: Record<string, string>[] | null;
};
}

0 comments on commit 5ea64eb

Please sign in to comment.