"use client";
import { useEffect, useState } from "react";
interface CountdownTimerProps {
endDate: string;
}
// endDate를 props로 받고 그 날 까지 남은 시간 계산
const CountdownTimer: React.FC<CountdownTimerProps> = ({ endDate }) => {
const parseDate = (date: string) => {
const [year, month, day] = date.split(".").map(Number);
return new Date(year, month - 1, day, 23, 59, 59);
};
const calculateTimeLeft = () => {
const difference = +parseDate(endDate) - +new Date();
let timeLeft = {
days: 0,
hours: 0,
minutes: 0,
seconds: 0
};
if (difference > 0) {
timeLeft = {
days: Math.floor(difference / (1000 * 60 * 60 * 24)),
hours: Math.floor((difference / (1000 * 60 * 60)) % 24),
minutes: Math.floor((difference / 1000 / 60) % 60),
seconds: Math.floor((difference / 1000) % 60)
};
}
return timeLeft;
};
const [timeLeft, setTimeLeft] = useState(calculateTimeLeft());
useEffect(() => {
const timer = setTimeout(() => {
setTimeLeft(calculateTimeLeft());
setIsLoading(false);
}, 1000);
return () => clearTimeout(timer);
}, [timeLeft]);
return (...)
}
export default CountdownTimer;
// zustand 상태관리
import { create } from "zustand";
interface ModalState {
modals: Record<string, boolean>;
toggleModal: (id: string) => void;
}
const useModalStore = create<ModalState>((set) => ({
modals: {},
toggleModal: (id: string) =>
set((state) => ({
modals: {
...state.modals,
[id]: !state.modals[id]
}
}))
}));
export default useModalStore;
today = new Date()
.
.
.
<input
type="date"
value={date}
onChange={(e) => setDate(e.target.value)}
min={today}
max={datas?.prfpdto[0].replaceAll(".", "-")}
/>
자잘자잘한 기능들을 많이 만들어보고 해야 나중에 필요할 때가 있으면 한번 만들었던 기억에 조금이라도 덜 망설여서 만들 수 있다.
이강민 튜터님께서 모달, 드롭다운, 콜랩스는 한번쯤 라이브러리 없이 구현해보라고 하셨는데 이번 프로젝트에서 모달을 했으니 다음에 다른 것을 도전해 봐야겠다.
❓콜랩스 : 메뉴 - 접기 / 펼치기 기능
무엇인가 정신없는 하루 였지만 오늘도 운동은 해야한다.
반드시