1
+ import { Duration , intervalToDuration } from 'date-fns' ;
1
2
import { useEffect , useState } from 'react' ;
2
3
import ConfettiExplosion from 'react-confetti-explosion' ;
3
4
4
5
import Button from '#components/Button' ;
6
+ import Countdown from '#components/CountDown' ;
5
7
6
8
const Weeding = ( ) => {
7
- const [ password , setPassword ] = useState ( '' ) ;
9
+ const [ password , setPassword ] = useState ( '280795 ' ) ;
8
10
const [ isSecret , setIsSecret ] = useState ( false ) ;
9
11
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 ] ) ;
11
29
12
30
const handleSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
13
31
e . preventDefault ( ) ;
14
32
setErrorMessage ( '' ) ;
15
33
const secret = import . meta. env . VITE_SECRET ;
16
34
if ( password === secret ) {
17
35
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 ) ) ) ;
22
36
} else {
23
37
setErrorMessage ( 'Oh no! Wrong key. Try again!' ) ;
24
38
setIsSecret ( false ) ;
@@ -34,7 +48,7 @@ const Weeding = () => {
34
48
35
49
return (
36
50
< 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" >
38
52
{ ! isSecret ? (
39
53
< form className = "space-y-6" onSubmit = { handleSubmit } >
40
54
< input
@@ -57,10 +71,14 @@ const Weeding = () => {
57
71
) }
58
72
</ form >
59
73
) : (
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" >
61
75
< 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
+ ) }
64
82
</ div >
65
83
) }
66
84
</ div >
0 commit comments