fix(frontend): useCalendar 및 Schedule 날짜 처리 버그 수정
- useCalendar: initialDate가 문자열일 경우 Date 객체로 변환 - useCalendar: days 배열 추가 (캘린더 날짜 목록) - useCalendar: canGoPrev 별칭 추가 - Schedule: currentDate가 Date 객체가 아닐 경우 안전하게 변환 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
64fc07044d
commit
81b78be010
2 changed files with 45 additions and 4 deletions
|
|
@ -5,10 +5,17 @@ import { getTodayKST } from '@/utils';
|
|||
/**
|
||||
* 캘린더 훅
|
||||
* 날짜 선택, 월 이동 등 캘린더 로직 제공
|
||||
* @param {Date} initialDate - 초기 날짜
|
||||
* @param {Date|string} initialDate - 초기 날짜
|
||||
*/
|
||||
export function useCalendar(initialDate = new Date()) {
|
||||
const [currentDate, setCurrentDate] = useState(initialDate);
|
||||
// initialDate가 Date 객체가 아니면 변환
|
||||
const ensureDate = (date) => {
|
||||
if (date instanceof Date) return date;
|
||||
if (typeof date === 'string') return new Date(date);
|
||||
return new Date();
|
||||
};
|
||||
|
||||
const [currentDate, setCurrentDate] = useState(() => ensureDate(initialDate));
|
||||
const [selectedDate, setSelectedDate] = useState(getTodayKST());
|
||||
|
||||
const year = currentDate.getFullYear();
|
||||
|
|
@ -20,6 +27,37 @@ export function useCalendar(initialDate = new Date()) {
|
|||
const daysInMonth = new Date(year, month + 1, 0).getDate();
|
||||
const prevMonthDays = new Date(year, month, 0).getDate();
|
||||
|
||||
// 캘린더에 표시할 날짜 배열 생성
|
||||
const days = [];
|
||||
|
||||
// 이전 달 날짜
|
||||
for (let i = firstDay - 1; i >= 0; i--) {
|
||||
days.push({
|
||||
day: prevMonthDays - i,
|
||||
isCurrentMonth: false,
|
||||
date: new Date(year, month - 1, prevMonthDays - i),
|
||||
});
|
||||
}
|
||||
|
||||
// 현재 달 날짜
|
||||
for (let i = 1; i <= daysInMonth; i++) {
|
||||
days.push({
|
||||
day: i,
|
||||
isCurrentMonth: true,
|
||||
date: new Date(year, month, i),
|
||||
});
|
||||
}
|
||||
|
||||
// 다음 달 날짜 (6주 채우기)
|
||||
const remaining = 42 - days.length; // 6주 * 7일 = 42
|
||||
for (let i = 1; i <= remaining; i++) {
|
||||
days.push({
|
||||
day: i,
|
||||
isCurrentMonth: false,
|
||||
date: new Date(year, month + 1, i),
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
year,
|
||||
month,
|
||||
|
|
@ -28,6 +66,7 @@ export function useCalendar(initialDate = new Date()) {
|
|||
daysInMonth,
|
||||
prevMonthDays,
|
||||
weekdays: WEEKDAYS,
|
||||
days,
|
||||
};
|
||||
}, [year, month]);
|
||||
|
||||
|
|
@ -93,6 +132,7 @@ export function useCalendar(initialDate = new Date()) {
|
|||
...calendarData,
|
||||
currentDate,
|
||||
selectedDate,
|
||||
canGoPrev: canGoPrevMonth,
|
||||
canGoPrevMonth,
|
||||
goToPrevMonth,
|
||||
goToNextMonth,
|
||||
|
|
|
|||
|
|
@ -143,11 +143,12 @@ function Schedule() {
|
|||
const isMobile = useIsMobile();
|
||||
|
||||
// Zustand store
|
||||
const { currentDate, setCurrentDate, selectedDate, setSelectedDate } = useScheduleStore();
|
||||
const { currentDate: storedCurrentDate, setCurrentDate, selectedDate, setSelectedDate } = useScheduleStore();
|
||||
|
||||
// 초기값 설정
|
||||
// 초기값 설정 - currentDate가 Date 객체가 아닐 수 있으므로 안전하게 변환
|
||||
const today = getTodayKST();
|
||||
const actualSelectedDate = selectedDate || today;
|
||||
const currentDate = storedCurrentDate instanceof Date ? storedCurrentDate : new Date(storedCurrentDate || today);
|
||||
|
||||
// 데이터 로드
|
||||
const year = currentDate.getFullYear();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue