-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
707 additions
and
235 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
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,40 @@ | ||
@import "./colors.scss"; | ||
@import "./responsive.scss"; | ||
|
||
.asmmLive { | ||
background-color: $primary; | ||
color: $light-text; | ||
|
||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
padding: 2rem; | ||
gap: 0.5rem; | ||
|
||
.miniTagline { | ||
font-size: 1rem; | ||
font-style: italic; | ||
} | ||
|
||
.mainAmount { | ||
font-size: 1.5rem; | ||
|
||
.total { | ||
font-weight: bold; | ||
font-size: 2rem; | ||
} | ||
} | ||
|
||
.pledge { | ||
font-size: 1.5rem; | ||
|
||
.pledgeTotal { | ||
font-weight: bold; | ||
} | ||
} | ||
|
||
.learnMore { | ||
margin: 1rem; | ||
} | ||
} |
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,98 @@ | ||
import anime from "animejs"; | ||
import { RefObject, useEffect, useRef, useState } from "react"; | ||
import { useAuth } from "../auth"; | ||
import Button from "../Button/Button"; | ||
|
||
import styles from "./asmm-live.module.scss"; | ||
|
||
function useOnScreen(ref: RefObject<HTMLElement>) { | ||
const [isIntersecting, setIntersecting] = useState(false); | ||
|
||
let observer: IntersectionObserver | null = null; | ||
|
||
useEffect(() => { | ||
if (ref.current) { | ||
observer = new IntersectionObserver(([entry]) => setIntersecting(entry.isIntersecting)); | ||
observer.observe(ref.current); | ||
} | ||
return () => observer?.disconnect(); | ||
}, []); | ||
|
||
return isIntersecting; | ||
} | ||
|
||
const ASMMLive = () => { | ||
const [pledgeAmount, setPledgeAmount] = useState(-1); | ||
const [walkAmount, setWalkAmount] = useState(0); | ||
const amountRef = useRef<HTMLSpanElement>(null); | ||
const amountVisible = useOnScreen(amountRef); | ||
const [amountAnimated, setAmountAnimated] = useState(false); | ||
const auth = useAuth(); | ||
|
||
const username = auth.ready ? auth.sessionData?.username : undefined; | ||
|
||
useEffect(() => { | ||
if (auth.ready) { | ||
fetch(`/api/asmm/total`).then((res) => { | ||
res.json().then((data) => { | ||
setWalkAmount(data.KmCount ?? 0); | ||
}); | ||
}); | ||
|
||
fetch(`/api/asmm/pledge?username=${username}`, { method: "GET" }).then((res) => { | ||
res.json().then((data) => { | ||
setPledgeAmount(data.pledge); | ||
}); | ||
}); | ||
} | ||
}, [auth]); | ||
|
||
useEffect(() => { | ||
if (!amountAnimated && amountVisible) { | ||
setAmountAnimated(true); | ||
anime({ | ||
targets: amountRef.current, | ||
innerText: [0, walkAmount], | ||
easing: "easeOutQuart", | ||
round: true, | ||
delay: 100, | ||
duration: 3000, | ||
update: function (a) { | ||
if (a.animations.length > 0 && amountRef.current) { | ||
const value = a.animations[0].currentValue; | ||
amountRef.current.innerText = value; | ||
} | ||
}, | ||
}); | ||
} | ||
}, [amountVisible, amountAnimated]); | ||
|
||
const amountToPay = Math.round((pledgeAmount * (walkAmount / 10) + Number.EPSILON) * 100) / 100; | ||
|
||
const hasPledged = pledgeAmount > 10; | ||
|
||
return ( | ||
<div className={styles.asmmLive}> | ||
<span className={styles.miniTagline}>Australian Speedrun Marathon Marathon</span> | ||
<span className={styles.mainAmount}> | ||
The AusSpeedruns community has walked{" "} | ||
<span ref={amountRef} className={styles.total}> | ||
{walkAmount} | ||
</span>{" "} | ||
KM. | ||
</span> | ||
{hasPledged ? ( | ||
<span className={styles.pledge}> | ||
You are pledging ${pledgeAmount} per 10km which totals to{" "} | ||
<span className={styles.pledgeTotal}>${amountToPay}</span> | ||
</span> | ||
) : ( | ||
<span className={styles.learnMore}> | ||
<Button colorScheme="secondary inverted" link="/asmm" actionText="Pledge and learn more" /> | ||
</span> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ASMMLive; |
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
Oops, something went wrong.