1
- import React , { useState , useEffect } from "react" ;
1
+ import React , { useEffect } from "react" ;
2
2
import PropTypes from "prop-types" ;
3
3
4
4
import Typography from "@material-ui/core/es/Typography" ;
@@ -9,42 +9,40 @@ import CountDown from "../../components/countDown";
9
9
import { categoryIcons , categories } from "../../assets/categories" ;
10
10
import styles from "./categories.module.scss" ;
11
11
12
- const Categories = ( { choosing = true } ) => {
13
- const [ timeLeft , setTimeLeft ] = useState ( true ) ;
12
+ const Categories = ( { choosing = true , category, setCategory, randomCategory, nextStep } ) => {
14
13
useEffect ( ( ) => {
15
- const timer = setTimeout ( ( ) => setTimeLeft ( false ) , 10000 ) ;
16
- return ( ) => {
17
- clearTimeout ( timer ) ;
18
- } ;
19
- } , [ ] ) ;
14
+ if ( ! category ) {
15
+ const timer = setTimeout ( ( ) => randomCategory ( ) , 10000 ) ;
16
+ return ( ) => clearTimeout ( timer ) ;
17
+ } else {
18
+ const timer = setTimeout ( ( ) => nextStep ( ) , 3000 ) ;
19
+ return ( ) => clearTimeout ( timer ) ;
20
+ }
21
+ } , [ category ] ) ;
22
+
20
23
const categoryEntries = Object . entries ( categories ) ;
24
+
21
25
return (
22
26
< >
23
27
< CountDown />
24
28
< Grid container spacing = { 3 } >
25
- { choosing && (
26
- < Grid item xs = { 12 } >
27
- < Typography variant = "h3" component = "h2" >
28
- Wähle eine Kategorie:
29
- </ Typography >
30
- </ Grid >
31
- ) }
32
- { ! choosing && (
33
- < Grid item xs = { 12 } >
34
- < Typography variant = "h3" component = "h2" >
35
- Dein Gegner wählt eine Kategorie!
36
- </ Typography >
37
- </ Grid >
38
- ) }
29
+ < Grid item xs = { 12 } >
30
+ < Typography variant = "h3" component = "h2" >
31
+ { choosing ? "Wähle eine Kategorie:" : "Dein Gegner wählt eine Kategorie!" }
32
+ </ Typography >
33
+ </ Grid >
39
34
{ categoryEntries . map ( ( [ cat , name ] ) => {
40
35
const Icon = categoryIcons [ cat ] ;
36
+ const selected = cat === category ;
37
+ const hasChoosen = ! ! category ;
41
38
return (
42
39
< Grid item xs = { 12 } key = { cat } >
43
40
< Button
44
- color = { ! timeLeft ? "secondary" : "primary" }
41
+ color = { selected ? "secondary" : "primary" }
45
42
variant = "contained"
46
- disabled = { ! choosing && timeLeft }
43
+ disabled = { ( ! choosing || hasChoosen ) && ! selected }
47
44
fullWidth
45
+ onClick = { ( ) => setCategory ( cat ) }
48
46
>
49
47
< Icon className = { styles . buttonIcon } />
50
48
{ name }
@@ -58,7 +56,11 @@ const Categories = ({ choosing = true }) => {
58
56
} ;
59
57
60
58
Categories . propTypes = {
61
- choosing : PropTypes . bool
59
+ choosing : PropTypes . bool ,
60
+ category : PropTypes . string ,
61
+ setCategory : PropTypes . func . isRequired ,
62
+ randomCategory : PropTypes . func . isRequired ,
63
+ nextStep : PropTypes . func . isRequired ,
62
64
} ;
63
65
64
66
export default Categories ;
0 commit comments