1+ import { useEffect , useState } from 'react' ;
12import './Countdown.css' ;
23import giphy1 from '../assets/img/giphy1.gif' ;
34import giphy2 from '../assets/img/giphy2.gif' ;
45
5- export const Countdown = ( { memberCount} ) => {
6-
7- const images = [ giphy1 , giphy2 ] ;
8-
9- return (
10- < main className = "container has-text-centered" >
11- < h1 className = "is-size-1" > Codecademy Discord Server</ h1 >
12- < h2 className = "is-size-2" > Count to 60k:</ h2 >
13-
14- { memberCount >= 60000 &&
15- < figure className = "image-is-square" >
16- < img src = { images [ Math . floor ( Math . random ( ) * images . length ) ] } />
17- </ figure >
18- }
19-
20- < div className = "blob" >
21- < p id = "members" > { memberCount } </ p >
22- </ div >
23-
24- < p className = "milestone" id = "milestone50k" > Feb 3rd '22 50k</ p >
25- < p className = "milestone" id = "milestone40k" > Nov 7th '21 40k</ p >
26- < p className = "milestone" id = "milestone30k" > Aug 11th '21 30k</ p >
27- < p className = "milestone" id = "milestone20k" > Jun 9th '21: 20k</ p >
28- < p className = "milestone" id = "milestone10k" > Mar 25th '21: 10k</ p >
29- </ main >
30- ) ;
31- } ;
32-
6+ function calculateGoal ( memberCount ) {
7+ const previousGoal = Math . floor ( memberCount / 10000 ) * 10000 ;
8+ const newGoal = Math . ceil ( memberCount / 10000 ) * 10000 ;
9+ return memberCount > previousGoal + 100 ? newGoal : previousGoal ;
10+ }
11+
12+ function kFormatter ( num ) {
13+ return Math . abs ( num ) > 999
14+ ? Math . sign ( num ) * ( Math . abs ( num ) / 1000 ) . toFixed ( 1 ) + 'k'
15+ : Math . sign ( num ) * Math . abs ( num ) ;
16+ }
17+
18+ export const Countdown = ( { memberCount } ) => {
19+ const images = [ giphy1 , giphy2 ] ;
20+ const [ goal , setGoal ] = useState ( calculateGoal ( memberCount ) ) ;
21+
22+ useEffect ( ( ) => {
23+ setGoal ( calculateGoal ( memberCount ) ) ;
24+ } , [ memberCount ] ) ;
25+
26+ return (
27+ < main className = "container has-text-centered" >
28+ < h1 className = "is-size-1" > Codecademy Discord Server</ h1 >
29+ < h2 className = "is-size-2" > Count to { kFormatter ( goal ) } </ h2 >
30+
31+ { memberCount >= goal && (
32+ < figure className = "image-is-square" >
33+ < img src = { images [ Math . floor ( Math . random ( ) * images . length ) ] } />
34+ </ figure >
35+ ) }
36+
37+ < div className = "blob" >
38+ < p id = "members" > { memberCount } </ p >
39+ </ div >
40+
41+ < p className = "milestone" id = "milestone50k" >
42+ Feb 3rd '22 50k
43+ </ p >
44+ < p className = "milestone" id = "milestone40k" >
45+ Nov 7th '21 40k
46+ </ p >
47+ < p className = "milestone" id = "milestone30k" >
48+ Aug 11th '21 30k
49+ </ p >
50+ < p className = "milestone" id = "milestone20k" >
51+ Jun 9th '21: 20k
52+ </ p >
53+ < p className = "milestone" id = "milestone10k" >
54+ Mar 25th '21: 10k
55+ </ p >
56+ </ main >
57+ ) ;
58+ } ;
0 commit comments