-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #878 from cornell-dti/countdown
Wrapped Countdown Component
- Loading branch information
Showing
8 changed files
with
543 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import "../../styles/WrappedCountdown.scss"; | ||
import ConfettiExplosion from "react-confetti-explosion"; | ||
import cone from "../../media/wrapped/cone.svg"; | ||
import ribbonBall from "../../media/wrapped/ribbonBall.svg"; | ||
|
||
type WrappedDate = { | ||
launchDate: Date; | ||
startDate: Date; | ||
}; | ||
type RemainingTime = { | ||
days: number; | ||
hours: number; | ||
minutes: number; | ||
total: number | ||
}; | ||
// Declare interface to takes in the setter setDisplayWrapped as a prop | ||
interface WrappedCountdownProps { | ||
setDisplayWrapped: React.Dispatch<React.SetStateAction<boolean>>; // Define prop type for setter | ||
wrappedDate: WrappedDate; | ||
} | ||
const WrappedCountdown: React.FC<WrappedCountdownProps> = ({ setDisplayWrapped, wrappedDate }) => { | ||
const handleButtonClick = () => { | ||
// Call the setter function to change the state in the parent | ||
setDisplayWrapped(true); // Set to true to show the modal | ||
}; | ||
// Helper function to calculate the remaining time in days, hours, and minutes from start date to launch date | ||
const calculateTimeRemaining = (dateProps: WrappedDate): RemainingTime => { | ||
// Calculate the time remaining (in milliseconds) between the launch date and start date | ||
const time = dateProps.launchDate.getTime() - new Date().getTime(); | ||
const gap = time < 0 ? 0 : time; | ||
// Calculate days, hours, and minutes remaining | ||
const days = Math.floor(gap / (1000 * 60 * 60 * 24)); | ||
const hours = Math.floor((gap / (1000 * 60 * 60)) % 24); | ||
const minutes = Math.floor((gap / (1000 * 60)) % 60); | ||
return { days, hours, minutes, total: gap }; | ||
}; | ||
// Initialize countdown state using the calculateTimeRemaining function | ||
const [timeRemaining, setTimeRemaining] = useState<RemainingTime>(calculateTimeRemaining(wrappedDate)); | ||
const [countDownClicked, setCountDownClicked] = useState<boolean>(false); | ||
const [confettiShown, setConfettiShown] = useState<boolean>(false); | ||
const [isZeroCounter, setIsZeroCounter] = useState<boolean>(false); | ||
|
||
// Countdown timer effect | ||
useEffect(() => { | ||
const updatedTime = calculateTimeRemaining(wrappedDate); | ||
setTimeRemaining(updatedTime); | ||
|
||
// Stop countdown and set `isZeroCounter` when time reaches zero | ||
if (updatedTime.total <= 0) { | ||
setIsZeroCounter(true); | ||
} | ||
}, [wrappedDate, timeRemaining]); | ||
|
||
// Trigger confetti with a delay only the first time `isZeroCounter` becomes true | ||
useEffect(() => { | ||
if (isZeroCounter && !confettiShown) { | ||
setTimeout(() => { | ||
setConfettiShown(true); // Show confetti after a delay | ||
}, 1000); // Delay of 1 second | ||
} | ||
}, [isZeroCounter, confettiShown]); | ||
|
||
// Prepend the days, hours, and minutes if they're single digits | ||
const prependZero = (num: number): string => (num < 10 ? `0${num}` : `${num}`); | ||
|
||
// Check that today is the start date or dates after the start date, then render the countdown if true | ||
const isStartDate = () => { | ||
const today = new Date(); | ||
// To get the Date object with respect to Eastern Time, we must offset | ||
return today.getTime() >= wrappedDate.startDate.getTime(); | ||
}; | ||
|
||
return !isStartDate() ? null : countDownClicked ? ( | ||
<div onClick={() => setCountDownClicked(false)}> | ||
<div className="countdownUpdates"> | ||
<div className="countdownContainer"> | ||
{!isZeroCounter ? ( | ||
<> | ||
<div> | ||
<div className="countdownContainer_boxes">{prependZero(timeRemaining.days)}</div> | ||
<p className="counter_sub">DAYS</p> | ||
</div> | ||
<div> | ||
<div className="countdownContainer_boxes">{prependZero(timeRemaining.hours)}</div> | ||
<p className="counter_sub">HOURS</p> | ||
</div> | ||
<div> | ||
<div className="countdownContainer_boxes">{prependZero(timeRemaining.minutes)}</div> | ||
<p className="counter_sub">MINUTES</p> | ||
</div> | ||
<div className="textContainer"> | ||
<p className="top">Queue Me In</p> | ||
<p className="bottom">WRAPPED</p> | ||
</div> | ||
</> | ||
) : ( | ||
<div className="launch"> | ||
<div className="post"> | ||
<div className="textContainer"> | ||
<p className="top">Queue Me In</p> | ||
<p className="bottom">WRAPPED</p> | ||
</div> | ||
<div className="viewWrap" onClick={handleButtonClick}> | ||
View Now | ||
</div> | ||
</div> | ||
{!confettiShown && <ConfettiExplosion duration={2800} force={0.6} particleCount={200} />} | ||
<img className="cone" src={cone} alt="icon" /> | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
) : ( | ||
<img className="ribbonBall" src={ribbonBall} alt="icon" onClick={() => setCountDownClicked(true)} /> | ||
); | ||
}; | ||
export default WrappedCountdown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
.countdownUpdates { | ||
position: fixed; | ||
bottom: 35px; | ||
right: 41px; | ||
} | ||
.countdownContainer { | ||
padding: 20px; | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
align-items: center; | ||
width: 413px; | ||
height: 139px; | ||
border-radius: 12px; | ||
background: linear-gradient(63deg, #668ae9 16.65%, #6db9ea 83.35%); /* Gradient background */ | ||
} | ||
.countdownContainer_boxes { | ||
color: #5599db; | ||
font-family: Roboto; | ||
font-size: 50px; | ||
font-style: normal; | ||
font-weight: 400; | ||
line-height: normal; | ||
justify-content: center; | ||
align-items: center; | ||
text-align: center; | ||
display: flex; | ||
width: 63px; | ||
height: 76px; | ||
border-radius: 5px; | ||
background: #fff; | ||
} | ||
.textContainer { | ||
align-items: center; | ||
} | ||
|
||
.ribbonBall { | ||
position: fixed; | ||
bottom: 36px; | ||
right: 46.23px; | ||
width: 68px; | ||
height: 68px; | ||
border-width: 100px; | ||
cursor: pointer; | ||
animation: rotation 1.3s ease-in-out infinite; | ||
} | ||
|
||
@keyframes rotation { | ||
|
||
0% { | ||
transform: rotate(15deg); | ||
} | ||
15% { | ||
transform: rotate(-15deg); | ||
} | ||
25% { | ||
transform: rotate(0deg); | ||
} | ||
} | ||
|
||
.top { | ||
margin: 0; | ||
color: #fff; | ||
font-family: Roboto; | ||
font-size: 24.079px; | ||
font-style: normal; | ||
font-weight: 700; | ||
line-height: normal; | ||
} | ||
.bottom { | ||
margin: 0; | ||
font-family: Roboto; | ||
font-size: 24.079px; | ||
font-style: normal; | ||
font-weight: 400; | ||
line-height: normal; | ||
background: linear-gradient(180deg, #fff 0%, #fff 100%); | ||
background-clip: text; | ||
-webkit-background-clip: text; | ||
-webkit-text-fill-color: transparent; | ||
} | ||
.counter_sub { | ||
margin-top: 10px; | ||
color: #fff; | ||
font-family: Roboto; | ||
font-size: 12px; | ||
font-style: normal; | ||
font-weight: 500; | ||
line-height: normal; | ||
letter-spacing: -0.6px; | ||
} | ||
.viewWrap { | ||
cursor: pointer; | ||
margin-top: 7px; | ||
width: 154.619px; | ||
height: 37.669px; | ||
border-radius: 5px; | ||
background: #fff; | ||
color: #5599db; | ||
font-family: Roboto; | ||
font-size: 20px; | ||
font-style: normal; | ||
font-weight: 500; | ||
line-height: normal; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
text-align: center; | ||
} | ||
|
||
.post { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.cone { | ||
z-index:10; | ||
margin-top: 40px; | ||
margin-right: 25px; | ||
} | ||
|
||
.launch{ | ||
margin-left: 20px; | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: center; | ||
align-items: center; | ||
} |
Oops, something went wrong.