1+ import { Duration , intervalToDuration } from 'date-fns' ;
12import { useEffect , useState } from 'react' ;
23import ConfettiExplosion from 'react-confetti-explosion' ;
34
45import Button from '#components/Button' ;
6+ import Countdown from '#components/CountDown' ;
57
68const Weeding = ( ) => {
7- const [ password , setPassword ] = useState ( '' ) ;
9+ const [ password , setPassword ] = useState ( '280795 ' ) ;
810 const [ isSecret , setIsSecret ] = useState ( false ) ;
911 const [ errorMessage , setErrorMessage ] = useState ( '' ) ;
10- const [ daysLeft , setDaysLeft ] = useState ( 0 ) ;
12+ const [ duration , setDuration ] = useState < Duration | null > ( null ) ;
13+ const [ weedingDate ] = useState ( new Date ( import . meta. env . VITE_WEEDING_DATE ) ) ;
14+
15+ useEffect ( ( ) => {
16+ const timer = setInterval ( ( ) => {
17+ const now = new Date ( ) ;
18+
19+ const duration = intervalToDuration ( {
20+ start : weedingDate ,
21+ end : now ,
22+ } ) ;
23+
24+ setDuration ( duration ) ;
25+ } , 1000 ) ;
26+
27+ return ( ) => clearInterval ( timer ) ;
28+ } , [ weedingDate ] ) ;
1129
1230 const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
1331 e . preventDefault ( ) ;
1432 setErrorMessage ( '' ) ;
1533 const secret = import . meta. env . VITE_SECRET ;
1634 if ( password === secret ) {
1735 setIsSecret ( true ) ;
18- const days =
19- new Date ( import . meta. env . VITE_WEEDING_DATE ) . getTime ( ) -
20- new Date ( ) . getTime ( ) ;
21- setDaysLeft ( Math . floor ( days / ( 1000 * 60 * 60 * 24 ) ) ) ;
2236 } else {
2337 setErrorMessage ( 'Oh no! Wrong key. Try again!' ) ;
2438 setIsSecret ( false ) ;
@@ -34,7 +48,7 @@ const Weeding = () => {
3448
3549 return (
3650 < div className = "grid h-dvh w-screen place-items-center bg-purple-400 p-6" >
37- < div className = "w-full max-w-md rounded-xl bg-purple-500 p-6 shadow-xl" >
51+ < div className = "w-full max-w-screen-sm rounded-xl bg-purple-500 p-6 shadow-xl" >
3852 { ! isSecret ? (
3953 < form className = "space-y-6" onSubmit = { handleSubmit } >
4054 < input
@@ -57,10 +71,14 @@ const Weeding = () => {
5771 ) }
5872 </ form >
5973 ) : (
60- < div className = "grid h-[300px] w-full place-content-center overflow-hidden text-center text-5xl font-bold text-purple-800" >
74+ < div className = "grid h-[300px] text-5xl font-bold text-purple-800" >
6175 < ConfettiExplosion particleCount = { 500 } duration = { 5000 } force = { 1 } />
62- < span className = "text-9xl drop-shadow-lg" > { daysLeft } </ span >
63- days left
76+ { duration && (
77+ < div className = "text-center" >
78+ < h3 className = "mb-5 text-5xl text-white" > Days Past</ h3 >
79+ < Countdown duration = { duration } />
80+ </ div >
81+ ) }
6482 </ div >
6583 ) }
6684 </ div >
0 commit comments