-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.jsx
178 lines (159 loc) · 4 KB
/
index.jsx
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { css, styled } from "uebersicht";
// ============================================
// Calendar Settings
// ============================================
const calendarStartDay = 0; //set 0 - 6 for the days of the week. Default is 0 for Sunday.
const color = "white"; //set color for all calendar days
const todaysColor = "black"; //set color for todays date
export const refreshFrequency = 3600000; // app refresh time in ms, Update every hour
// ============================================
// CSS Styles
// ============================================
export const className = `
left: 35px;
bottom: 35px;
color: ${color};
font-family: Helvetica;
font-weight: bold;
z-index: 1;
`;
const Error = styled("h2")`
font-size: 1.5em;
color: red;
text-align: center;
`;
const Headline = styled("h1")`
font-size: 20px;
margin-bottom: 15px;
text-align: center;
`;
const Calendar = styled("div")`
display: grid;
grid-template-columns: repeat(7, 25px);
grid-template-rows: 25px repeat(5, 25px);
`;
const Weekday = styled("div")`
font-weight: normal;
font-size: 12px;
text-align: center;
`;
const Day = styled("div")`
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-family: Helvetica;
`;
const today = css`
background: ${color};
color: ${todaysColor};
border-radius: 50%;
`;
// ============================================
// JS Logic
// ============================================
function addCalendarGaps(daysToSkip, daysOfTheMonth) {
let clonedCalendarArray = JSON.parse(JSON.stringify(daysOfTheMonth));
//add blank indexes to clonedCalendar Array for Calendar gaps
for (let i = 1; i < daysToSkip; i++) {
clonedCalendarArray.unshift("");
}
return clonedCalendarArray;
}
function reorderCalendar(startDay, calendarWeekArray) {
let sendToBack = calendarWeekArray.slice(0, startDay);
let sendToFront = calendarWeekArray.slice(startDay, calendarWeekArray.length);
return sendToFront.concat(sendToBack);
}
export const command = dispatch => {
let months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
let sundayWeek = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
let week;
calendarStartDay > 0
? (week = reorderCalendar(calendarStartDay, sundayWeek))
: (week = sundayWeek);
let todaysDate = new Date().getDate(); // todays number calendar day
let year = new Date().getFullYear(); // 20XX
let month = new Date().getMonth() + 1; // 1 - 12
let monthString = month < 10 ? `0${month}` : month; // 01 - 12
let FirstOfMonthString = `${year}-${monthString}-01`;
let weekdayFirstOfMonth = new Date(FirstOfMonthString).getDay() + 1; //first day of the month
let daysToSkip = week.indexOf(sundayWeek[weekdayFirstOfMonth]) + 1;
let currentMonthDays = new Date(year, month, 0).getDate(); //get number of days in todays month
//create an array with the number of days of todays month
let daysOfTheMonth = [];
for (let i = 1; i <= currentMonthDays; i++) {
daysOfTheMonth.push(i);
}
dispatch({
type: "DATA",
data: {
year,
months,
month,
week,
todaysDate,
daysToSkip,
daysOfTheMonth
}
});
};
export const updateState = (event, previousState) => {
switch (event.type) {
case "DATA":
console.log("data:", event.data);
return event.data;
default:
console.log("previous:", previousState);
return previousState;
}
};
export const render = ({
error,
year,
months,
month,
week,
todaysDate,
daysToSkip,
daysOfTheMonth
}) => {
return error ? (
<Error>
Something went wrong: <strong>{String(error)}</strong>
</Error>
) : (
<main>
<header>
<Headline>{`${months[month - 1]} ${year}`}</Headline>
</header>
<Calendar>
{week.map(day => (
<Weekday key={day}>{day}</Weekday>
))}
{addCalendarGaps(daysToSkip, daysOfTheMonth).map((day, i) =>
day == todaysDate ? (
<Day className={today} key={i}>
{day}
</Day>
) : (
<Day key={i}>{day}</Day>
)
)}
</Calendar>
</main>
);
};