1
- import React , { useState } from "react" ;
1
+ import React , { useEffect , useState } from "react" ;
2
2
import { useThemeHooks } from "@redocly/theme/core/hooks" ;
3
3
import moment from "moment" ;
4
4
import { Link } from "@redocly/theme/components/Link/Link" ;
@@ -17,29 +17,9 @@ const hackathon = require("../static/img/events/Hackathons.png");
17
17
const conference = require ( "../static/img/events/Conference.png" ) ;
18
18
const zone = require ( "../static/img/events/XRPLZone.png" ) ;
19
19
const brazil = require ( "../static/img/events/event-meetup-brazil.png" ) ;
20
- const korea = require ( "../static/img/events/SouthKoreaMeetup.png" ) ;
21
20
const infoSession2 = require ( "../static/img/events/xrpl-builder-office-hours-02.png" ) ;
22
21
const infoSession3 = require ( "../static/img/events/xrpl-builder-office-hours-03.png" ) ;
23
22
const infoSession4 = require ( "../static/img/events/xrpl-builder-office-hours-04.png" ) ;
24
- const findNearestUpcomingEvent = ( events ) => {
25
- let nearestEvent = null ;
26
- let nearestDateDiff = Infinity ;
27
- let index ;
28
- events . forEach ( ( event , i ) => {
29
- const eventStartDate = moment ( event . start_date , "MMMM DD, YYYY" ) ;
30
- const currentDate = moment ( ) ;
31
- const diff = eventStartDate . diff ( currentDate , "days" ) ;
32
-
33
- if ( diff >= 0 && diff < nearestDateDiff ) {
34
- nearestEvent = event ;
35
- nearestDateDiff = diff ;
36
- index = i
37
- }
38
- } ) ;
39
-
40
- return { nearestEvent, nearestDateDiff, index } ;
41
- } ;
42
-
43
23
const events = [
44
24
{
45
25
name : "New Horizon: Innovate Without Limits: New Horizons Await" ,
@@ -354,11 +334,46 @@ const events = [
354
334
end_date : "November 22, 2024" ,
355
335
} ,
356
336
] ;
357
- const { nearestDateDiff, nearestEvent, index } = findNearestUpcomingEvent ( events ) ;
337
+
338
+ const findNearestUpcomingEvent = ( events ) => {
339
+ let nearestEvent = null ;
340
+ let nearestDateDiff = Infinity ;
341
+ let index = 0 ;
342
+ events . forEach ( ( event , i ) => {
343
+ const eventStartDate = moment ( event . start_date , "MMMM DD, YYYY" ) ;
344
+ const currentDate = moment ( ) ;
345
+ const diff = eventStartDate . diff ( currentDate , "days" ) ;
346
+
347
+ if ( diff >= 0 && diff < nearestDateDiff ) {
348
+ nearestEvent = event ;
349
+ nearestDateDiff = diff ;
350
+ index = i ;
351
+ }
352
+ } ) ;
353
+
354
+ return { nearestEvent, nearestDateDiff, index } ;
355
+ } ;
356
+
358
357
const XrplEventsAndCarouselSection = ( { events } ) => {
359
358
const { useTranslate } = useThemeHooks ( ) ;
360
359
const { translate } = useTranslate ( ) ;
361
- const [ currentIndex , setCurrentIndex ] = useState ( index ) ; // use nearest event's index as init state
360
+
361
+ // State variables for the nearest event
362
+ const [ nearestEventInfo , setNearestEventInfo ] = useState ( {
363
+ nearestEvent : null ,
364
+ nearestDateDiff : null ,
365
+ index : 0 ,
366
+ } ) ;
367
+
368
+ // State for the current index in the carousel
369
+ const [ currentIndex , setCurrentIndex ] = useState ( 0 ) ;
370
+
371
+ useEffect ( ( ) => {
372
+ const { nearestEvent, nearestDateDiff, index } = findNearestUpcomingEvent ( events ) ;
373
+ setNearestEventInfo ( { nearestEvent, nearestDateDiff, index } ) ;
374
+ setCurrentIndex ( index ) ;
375
+ } , [ events ] ) ;
376
+
362
377
const updateCarousel = ( ) => {
363
378
const prevEvent = events [ currentIndex - 1 ] || null ;
364
379
const currentEvent = events [ currentIndex ] ;
@@ -373,17 +388,18 @@ const XrplEventsAndCarouselSection = ({ events }) => {
373
388
374
389
const handlePrev = ( ) => {
375
390
if ( currentIndex > 0 ) {
376
- setCurrentIndex ( currentIndex - 1 ) ;
391
+ setCurrentIndex ( ( prevIndex ) => prevIndex - 1 ) ;
377
392
}
378
393
} ;
379
394
380
395
const handleNext = ( ) => {
381
396
if ( currentIndex < events . length - 1 ) {
382
- setCurrentIndex ( currentIndex + 1 ) ;
397
+ setCurrentIndex ( ( prevIndex ) => prevIndex + 1 ) ;
383
398
}
384
399
} ;
385
400
386
401
const { prevEvent, currentEvent, nextEvent } = updateCarousel ( ) ;
402
+
387
403
return (
388
404
< >
389
405
< section className = "xrpl-events-section" >
@@ -415,24 +431,24 @@ const XrplEventsAndCarouselSection = ({ events }) => {
415
431
{ translate ( "View All Events" ) }
416
432
</ Link >
417
433
</ div >
418
- { ! ! nearestEvent && (
434
+ { ! ! nearestEventInfo . nearestEvent && (
419
435
< div className = "upcoming-event" id = "upcoming-events-section" >
420
436
< p className = "upcoming-label" > { translate ( "UPCOMING EVENT" ) } </ p >
421
437
< div id = "days-count" className = "days-count" >
422
- { nearestDateDiff }
438
+ { nearestEventInfo . nearestDateDiff }
423
439
</ div >
424
440
< div className = "days-word" > { translate ( "days" ) } </ div >
425
441
< div className = "num-separator" > </ div >
426
442
< h5 id = "upcoming-event-name" className = "event-name" >
427
- { translate ( nearestEvent ?. name ) }
443
+ { translate ( nearestEventInfo . nearestEvent ?. name ) }
428
444
</ h5 >
429
445
< p className = "mb-2 event-details d-flex icon" >
430
446
< span className = "icon-location" > </ span >
431
- < span id = "upcoming-event-date" > { nearestEvent ?. date } </ span >
447
+ < span id = "upcoming-event-date" > { nearestEventInfo . nearestEvent ?. date } </ span >
432
448
</ p >
433
449
< p className = "event-location d-flex icon" >
434
450
< span className = "icon-date" id = "upcoming-event-location" > </ span >
435
- < span id = "location-tag" > { nearestEvent ?. location } </ span >
451
+ < span id = "location-tag" > { nearestEventInfo . nearestEvent ?. location } </ span >
436
452
</ p >
437
453
</ div >
438
454
) }
0 commit comments