-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calendar.js
154 lines (142 loc) · 5.16 KB
/
Calendar.js
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
143
144
145
146
147
148
149
150
151
152
153
154
import React, { useEffect, useState, useContext } from "react";
import createYearArray from "../1-utils/createYearArray";
import DateContext from "../2-context/DateContext";
import EventsContext from "../2-context/EventsContext";
import ThemeContext from "../2-context/ThemeContext";
import View from "./View";
import ModalContainer from "./ModalContainer";
import Alert from "./Alert";
import MyAccount from "./MyAccount";
const Calendar = () => {
const { dateObj, setDateObj } = useContext(DateContext);
const { alertInfo } = useContext(EventsContext);
const [showDateSelect, setShowDateSelect] = useState(false);
const [showModalContainer, setShowModalContainer] = useState(false);
const [showMyAccount, setShowMyAccount] = useState(false);
const [showMyEvents, setShowMyEvents] = useState(false);
const [showSettings, setShowSettings] = useState(false);
const [showWholeYear, setShowWholeYear] = useState(true);
const [yearArray, setYearArray] = useState(null);
const { theme } = useContext(ThemeContext);
//generate calendar once dateObj is passed down from DateContext
useEffect(() => {
if (dateObj) {
const yearArray = createYearArray(dateObj.year);
setYearArray(yearArray);
}
//eslint-disable-next-line
}, [dateObj]);
const closeModals = e => {
if (
e.target.classList.contains("modal") ||
e.target.classList.contains("settings__ok-button") ||
e.target.classList.contains("modal__close-button")
) {
setShowModalContainer(false);
setShowMyEvents(false);
setShowSettings(false);
}
};
const setMonthView = monthIndex => {
setDateObj({ ...dateObj, month: monthIndex });
setShowWholeYear(false);
};
const setMonth = monthIndex => {
//these checks apply if user is changing month with arrows in MonthHeader
if (monthIndex === 12) {
setDateObj({ ...dateObj, year: dateObj.year + 1, month: 0 });
return;
}
if (monthIndex === -1) {
setDateObj({ ...dateObj, year: dateObj.year - 1, month: 11 });
return;
}
setDateObj({ ...dateObj, month: monthIndex });
};
const setYear = year => {
setDateObj({ ...dateObj, year });
};
//when user clicks on day cell
const handleShowModalContainer = e => {
const selectedDate = parseInt(
e.currentTarget.firstChild.firstChild.textContent
);
const newDateObj = new Date(dateObj.year, dateObj.month, selectedDate);
const dayIndex = newDateObj.getDay();
setDateObj({ ...dateObj, date: selectedDate, dayIndex });
setShowModalContainer(true);
};
//MyEvents and Settings modals are opened outside of ModalContainer (via HeaderLinks) so handlers are here rather than in ModalContainer
const handleShowMyEvents = () => {
setShowMyEvents(true);
setShowModalContainer(true);
};
const handleShowSettings = () => {
setShowSettings(true);
setShowModalContainer(true);
};
//close DateSelect/MyAccount by clicking anywhere outside them
//there has to be a better way to do this!
const handleCalendarClick = e => {
if (
e.target.classList.contains("month-header__date-info") ||
e.target.classList.contains("month-header__month-name") ||
e.target.classList.contains("month-header__year-name") ||
e.target.classList.contains("my-account") ||
e.target.classList.contains("header-links__my-account-button") ||
e.target.classList.contains("fa-user-circle") ||
e.target.parentNode.classList.contains("fa-user-circle") ||
e.target.classList.contains("mobile-menu__my-account-button")
) {
return;
} else {
setShowDateSelect(false);
setShowMyAccount(false);
}
};
if (dateObj && yearArray) {
return (
<>
<div
className={`calendar ${
theme === "Dark" ? "calendar--dark" : "calendar--light"
}`}
onClick={handleCalendarClick}
>
{alertInfo && <Alert alertInfo={alertInfo} />}
{showMyAccount && <MyAccount setShowMyAccount={setShowMyAccount} />}
<View
handleShowModalContainer={handleShowModalContainer}
handleShowMyEvents={handleShowMyEvents}
handleShowSettings={handleShowSettings}
setMonth={setMonth}
setMonthView={setMonthView}
setShowDateSelect={setShowDateSelect}
setShowMyAccount={setShowMyAccount}
setShowWholeYear={setShowWholeYear}
setYear={setYear}
showDateSelect={showDateSelect}
showMyAccount={showMyAccount}
showWholeYear={showWholeYear}
yearArray={yearArray}
/>
{showModalContainer && (
<ModalContainer
closeModals={closeModals}
setShowMyEvents={setShowMyEvents}
showMyEvents={showMyEvents}
showSettings={showSettings}
/>
)}
</div>
</>
);
} else {
return (
<div className="loader__wrapper">
<div className="loader"></div>
</div>
);
}
};
export default Calendar;