22
33import { useState , useEffect } from "react" ;
44
5- export default function Countdown ( ) {
5+ export default function Countdown ( { targetDate } ) {
66 const [ timeLeft , setTimeLeft ] = useState ( {
77 Days : "00" ,
88 Hours : "00" ,
@@ -11,8 +11,14 @@ export default function Countdown() {
1111 } ) ;
1212
1313 useEffect ( ( ) => {
14- // Set countdown to 1 hour from now ON THE CLIENT
15- const target = Date . now ( ) + 60 * 60 * 1000 ;
14+ if ( ! targetDate ) return ;
15+
16+ const target = new Date ( targetDate ) . getTime ( ) ;
17+
18+ if ( isNaN ( target ) ) {
19+ console . error ( "Invalid targetDate:" , targetDate ) ;
20+ return ;
21+ }
1622
1723 const timer = setInterval ( ( ) => {
1824 const now = Date . now ( ) ;
@@ -33,19 +39,21 @@ export default function Countdown() {
3339 const hours = Math . floor (
3440 ( diff % ( 1000 * 60 * 60 * 24 ) ) / ( 1000 * 60 * 60 )
3541 ) ;
36- const minutes = Math . floor ( ( diff % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 ) ) ;
42+ const minutes = Math . floor (
43+ ( diff % ( 1000 * 60 * 60 ) ) / ( 1000 * 60 )
44+ ) ;
3745 const seconds = Math . floor ( ( diff % ( 1000 * 60 ) ) / 1000 ) ;
3846
3947 setTimeLeft ( {
40- Days : days . toString ( ) . padStart ( 2 , "0" ) ,
41- Hours : hours . toString ( ) . padStart ( 2 , "0" ) ,
42- Minutes : minutes . toString ( ) . padStart ( 2 , "0" ) ,
43- Seconds : seconds . toString ( ) . padStart ( 2 , "0" ) ,
48+ Days : String ( days ) . padStart ( 2 , "0" ) ,
49+ Hours : String ( hours ) . padStart ( 2 , "0" ) ,
50+ Minutes : String ( minutes ) . padStart ( 2 , "0" ) ,
51+ Seconds : String ( seconds ) . padStart ( 2 , "0" ) ,
4452 } ) ;
4553 } , 1000 ) ;
4654
4755 return ( ) => clearInterval ( timer ) ;
48- } , [ ] ) ;
56+ } , [ targetDate ] ) ;
4957
5058 return (
5159 < div className = "mb-8 text-center" >
@@ -57,7 +65,7 @@ export default function Countdown() {
5765 { Object . entries ( timeLeft ) . map ( ( [ label , value ] ) => (
5866 < div
5967 key = { label }
60- className = "bg-gray-800 w-40 h-40 md:w-24 md:h-24 rounded-lg flex flex-col items-center justify-center"
68+ className = "bg-gray-800 w-20 h-20 md:w-24 md:h-24 rounded-lg flex flex-col items-center justify-center"
6169 >
6270 < span className = "text-2xl md:text-3xl font-bold text-blue-500" >
6371 { value }
0 commit comments