Skip to content

Commit d873e57

Browse files
committed
♻️ Integrate category step with controller
1 parent 2a813fc commit d873e57

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

src/hooks/controller.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useReducer, useMemo, useCallback } from "react";
22
import { Action, createActions } from "./actions";
3-
import { Category } from "../assets/categories";
3+
import { Category, categories } from "../assets/categories";
44
import questions, { Question } from "../assets/questions";
55
import { fetchValues } from "./datenguide";
66
import { GraphQLRequest } from "apollo-link";
@@ -191,6 +191,12 @@ const useGameController = (restoreState?: GameState) => {
191191
...actions
192192
} = useMemo(() => createActions(dispatch), [dispatch]);
193193

194+
const randomCategory = useCallback(() => {
195+
const catNum = Math.floor(Math.random() * Object.keys(categories).length);
196+
const cat = Object.keys(categories)[catNum] as Category;
197+
actions.setCategory(cat);
198+
}, [actions.setCategory]);
199+
194200
const findOpponent = useCallback(() => {
195201
if (!user) {
196202
return;
@@ -229,6 +235,7 @@ const useGameController = (restoreState?: GameState) => {
229235
findOpponent,
230236
findQuestion,
231237
calculateResult,
238+
randomCategory,
232239
...state,
233240
};
234241
}

src/stories/controller.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import { action } from "@storybook/addon-actions";
44
import { GameState, initialState, GameController } from "../hooks/controller";
55

66
export const mockState = (state: Partial<GameState>): GameState => {
7-
return {
8-
...initialState,
9-
...state,
10-
}
7+
return {
8+
...initialState,
9+
...state,
10+
}
1111
}
1212

1313
export const mockGameController: GameController = (state: Partial<GameState> = {}) => {
14-
const actions = useMemo(() => createActions(action("action")), [])
14+
const actions = useMemo(() => createActions(action("action")), [])
1515

16-
return {
17-
...actions,
18-
findOpponent: action("findOpponent"),
19-
findQuestion: action("findQuestion"),
20-
calculateResult: (...args) => Promise.resolve(action("calculateResult")(...args)),
21-
...mockState(state),
22-
}
16+
return {
17+
...actions,
18+
findOpponent: action("findOpponent"),
19+
findQuestion: action("findQuestion"),
20+
calculateResult: (...args) => Promise.resolve(action("calculateResult")(...args)),
21+
randomCategory: action("randomCategory"),
22+
...mockState(state),
23+
}
2324
}
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from "react";
1+
import React, { useEffect } from "react";
22
import PropTypes from "prop-types";
33

44
import Typography from "@material-ui/core/es/Typography";
@@ -9,42 +9,40 @@ import CountDown from "../../components/countDown";
99
import { categoryIcons, categories } from "../../assets/categories";
1010
import styles from "./categories.module.scss";
1111

12-
const Categories = ({ choosing = true }) => {
13-
const [timeLeft, setTimeLeft] = useState(true);
12+
const Categories = ({ choosing = true, category, setCategory, randomCategory, nextStep }) => {
1413
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+
2023
const categoryEntries = Object.entries(categories);
24+
2125
return (
2226
<>
2327
<CountDown />
2428
<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>
3934
{categoryEntries.map(([cat, name]) => {
4035
const Icon = categoryIcons[cat];
36+
const selected = cat === category;
37+
const hasChoosen = !!category;
4138
return (
4239
<Grid item xs={12} key={cat} >
4340
<Button
44-
color={!timeLeft ? "secondary" : "primary"}
41+
color={selected ? "secondary" : "primary"}
4542
variant="contained"
46-
disabled={!choosing && timeLeft}
43+
disabled={(!choosing || hasChoosen) && !selected}
4744
fullWidth
45+
onClick={() => setCategory(cat)}
4846
>
4947
<Icon className={styles.buttonIcon} />
5048
{name}
@@ -58,7 +56,11 @@ const Categories = ({ choosing = true }) => {
5856
};
5957

6058
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,
6264
};
6365

6466
export default Categories;

0 commit comments

Comments
 (0)