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" ;
43import DayCard from "./DayCard.tsx" ;
4+ import { Event , JsonData } from "./types.ts" ;
55
66export 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 <
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 >
0 commit comments