-
Notifications
You must be signed in to change notification settings - Fork 3
/
MakeCalendar.tsx
142 lines (130 loc) · 3.96 KB
/
MakeCalendar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use client";
import { ChevronLeftIcon, ChevronRightIcon } from "@radix-ui/react-icons";
import {
addDays,
addMonths,
differenceInCalendarDays,
endOfMonth,
endOfWeek,
format,
getMonth,
startOfMonth,
startOfWeek,
subMonths,
} from "date-fns";
import { useCallback, useMemo, useState } from "react";
import styles from "./Calendar.module.scss";
import { useCalendarDocument } from "@/hooks/use-calendar-document";
import { CalendarDay } from "./CalendarDay";
interface CalendarProps {
initialContent?: string;
onChange: (value: string) => void;
}
const MakeCalendar = ({ initialContent, onChange }: CalendarProps) => {
const [currentDate, setCurrentDate] = useState(new Date());
const [showButton, setShowButton] = useState<number>();
const [clickedButton, setClickedButton] = useState<boolean>(false);
//const createCalendarDocument = useMutation(api.calendars.update);
const editor = useCalendarDocument({
initialContent: initialContent ? JSON.parse(initialContent) : undefined,
onCalendarChanged: (calendar) => {
onChange(JSON.stringify(calendar, null, 2));
},
});
const handleCalendarDocument = (index: number) => {
if (!clickedButton) {
setClickedButton(true);
editor.onNewElement(index, getMonth(currentDate) + 1);
} else {
editor.onNewElement(index, getMonth(currentDate) + 1);
}
};
const handleMouseEnter = (index: number) => {
setShowButton(index);
};
const handleMouseLeave = () => {
setShowButton(undefined);
};
const monthStart = startOfMonth(currentDate);
const monthEnd = endOfMonth(currentDate);
const startDate = startOfWeek(monthStart);
const endDate = endOfWeek(monthEnd);
//const weekMock = ["일", "월", "화", "수", "목", "금", "토"];
const [weekMock, setWeekMock] = useState([
"일",
"월",
"화",
"수",
"목",
"금",
"토",
]);
const nextMonthHandler = useCallback(() => {
setCurrentDate(addMonths(currentDate, 1));
}, [currentDate]);
const prevMonthHandler = useCallback(() => {
setCurrentDate(subMonths(currentDate, 1));
}, [currentDate]);
const createMonth = useMemo(() => {
const monthArray = [];
let day = startDate;
while (differenceInCalendarDays(endDate, day) >= 0) {
monthArray.push(day);
day = addDays(day, 1);
}
return monthArray;
}, [startDate, endDate]);
return (
<section className={styles.calendar}>
<div className={styles.yearTitle}>{format(currentDate, "yyyy년")}</div>
<div className={styles.monthTitle}>
<button className={styles.prevButton} onClick={prevMonthHandler}>
<ChevronLeftIcon />
</button>
<div className={styles.month}>{format(currentDate, "M월")}</div>
<button className={styles.nextButton} onClick={nextMonthHandler}>
<ChevronRightIcon />
</button>
</div>
<div className={styles.dayContainer}>
{weekMock.map((v, i) => {
let style;
if (i === 0) {
style = {
color: "red",
};
} else if (i === 6) {
style = {
color: "blue",
};
}
return (
<div key={`day${i}`} style={style}>
{v}
</div>
);
})}
</div>
<div className={styles.dateContainer}>
{createMonth.map((v, i) => {
const content = editor.content && editor.content.filter((v) => v._id);
return (
<CalendarDay
day={v}
today={currentDate}
initialContent={initialContent}
onMouseEnter={() => handleMouseEnter(i)}
onMouseLeave={handleMouseLeave}
highlighted={showButton === i}
addDocument={() => handleCalendarDocument(i)}
index={i}
editor={editor}
content={content}
/>
);
})}
</div>
</section>
);
};
export default MakeCalendar;