Skip to content

Commit 50c6bf1

Browse files
committed
Modify Calendar implementation
1 parent 2b3100a commit 50c6bf1

2 files changed

Lines changed: 93 additions & 22 deletions

File tree

src/routes/Events/index.tsx

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,64 @@
1-
import { useState } from "react";
2-
import { changeMonth, dayNames, months } from "./utils.ts";
3-
import times from "lodash/times";
1+
import { useEffect, useState } from "react";
2+
import { changeMonth, getViewRange, dayNames, months } from "./utils.ts";
43
import DayCard from "./DayCard.tsx";
4+
import { Event, JsonData } from "./types.ts";
55

66
export default function Events() {
7-
// const today = new Date();
7+
const today = new Date();
88
const [currentDate, setCurrentDate] = useState<Date>(new Date());
9+
const [data, setData] = useState<Event[]>();
10+
const [viewRange, setViewRange] = useState<[Date, Date]>(
11+
getViewRange(today.getFullYear(), today.getMonth()),
12+
);
13+
14+
function RenderDays(startDate: Date, endDate: Date) {
15+
const totalDays =
16+
Math.ceil(
17+
(endDate.valueOf() - startDate.valueOf()) / (1000 * 60 * 60 * 24),
18+
) + 1;
19+
20+
return (
21+
<>
22+
{Array.from({ length: totalDays }, (_, index) => {
23+
const currentDate = new Date(startDate);
24+
currentDate.setDate(startDate.getDate() + index);
25+
let events: Event[] = [];
26+
27+
if (data) {
28+
events = data.filter(
29+
(event) => event.date === currentDate.getDate(),
30+
);
31+
}
32+
33+
return (
34+
<DayCard key={index} date={currentDate.getDate()} events={events} />
35+
);
36+
})}
37+
</>
38+
);
39+
}
40+
41+
useEffect(() => {
42+
setViewRange(
43+
getViewRange(currentDate.getFullYear(), currentDate.getMonth()),
44+
);
45+
46+
const fetchData = async (): Promise<JsonData> => {
47+
const eventData = (await import(
48+
`../../data/events/${currentDate.getFullYear().toString()}.json`
49+
)) as { default: JsonData };
50+
51+
return eventData.default;
52+
};
53+
54+
const currentMonth = months[currentDate.getMonth()];
55+
56+
fetchData()
57+
.then((json) => {
58+
setData(json[currentMonth]);
59+
})
60+
.catch(console.error);
61+
}, [currentDate]);
962

1063
return (
1164
<main className="flex h-full w-full flex-col">
@@ -22,8 +75,13 @@ export default function Events() {
2275
>
2376
&#60;
2477
</button>
25-
<span className="mx-12 text-3xl">
26-
{months[currentDate.getMonth()][0]}
78+
<span
79+
className="mx-12 text-3xl"
80+
onClick={() => {
81+
console.log(data);
82+
}}
83+
>
84+
{`${months[currentDate.getMonth()]} ${currentDate.getFullYear().toString()}`}
2785
</span>
2886
<button
2987
className="text-3xl"
@@ -43,9 +101,7 @@ export default function Events() {
43101
))}
44102
</div>
45103
<div className="grid w-full grid-cols-7 gap-4">
46-
{times(months[currentDate.getMonth()][1], (i) => (
47-
<DayCard key={i} date={i + 1} />
48-
))}
104+
{RenderDays(viewRange[0], viewRange[1])}
49105
</div>
50106
</div>
51107
</section>

src/routes/Events/utils.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
export const months: [string, number][] = [
2-
["January", 31],
3-
["February", 28],
4-
["March", 31],
5-
["April", 30],
6-
["May", 31],
7-
["June", 30],
8-
["July", 31],
9-
["August", 31],
10-
["September", 30],
11-
["October", 31],
12-
["November", 30],
13-
["December", 31],
1+
export const months = [
2+
"January",
3+
"February",
4+
"March",
5+
"April",
6+
"May",
7+
"June",
8+
"July",
9+
"August",
10+
"September",
11+
"October",
12+
"November",
13+
"December",
1414
];
1515
export const dayNames = [
1616
"Sunday",
@@ -27,3 +27,18 @@ export const changeMonth = (value: number, currentDate: Date): Date => {
2727
newDate.setMonth(currentDate.getMonth() + value);
2828
return newDate;
2929
};
30+
31+
export const getViewRange = (year: number, month: number): [Date, Date] => {
32+
const startDate = new Date(year, month, 1);
33+
const endDate = new Date(year, month + 1, 0);
34+
35+
while (startDate.getDay() !== 0) {
36+
startDate.setDate(startDate.getDate() - 1);
37+
}
38+
39+
while (endDate.getDay() !== 6) {
40+
endDate.setDate(endDate.getDate() + 1);
41+
}
42+
43+
return [startDate, endDate];
44+
};

0 commit comments

Comments
 (0)