38 |
Create a new game!
39 |
40 | { apiError &&
41 |
42 |
Error: {apiError}
43 |
44 | }
45 |
46 | { !apiError &&
47 |
50 | }
51 |
52 | );
53 | };
54 |
55 | export default CreateGame;
56 |
--------------------------------------------------------------------------------
/client/src/pages/JoinGame.tsx:
--------------------------------------------------------------------------------
1 | import { useMutation } from "@apollo/client";
2 | import React, { useEffect, useState } from "react";
3 | import { useNavigate, useParams } from "react-router-dom";
4 | import { AddPlayerScreenDocument } from "../generated";
5 |
6 | const JoinGame: React.FC = () => {
7 | const { id } = useParams() as { id: string };
8 | const navigate = useNavigate();
9 | const [name, setName] = useState("");
10 | const [addPlayerToGame, { loading, data }] = useMutation(
11 | AddPlayerScreenDocument
12 | );
13 |
14 | useEffect(() => {
15 | if (data) {
16 | navigate(`/game/play/${id}/${data.addPlayerToGame.id}`);
17 | }
18 | }, [data, id, navigate]);
19 |
20 | return (
21 |
22 |
Join the game: {id}
23 |
24 |
25 | setName(e.target.value)}
30 | />
31 |
32 |
33 |
43 |
44 |
45 | );
46 | };
47 |
48 | export default JoinGame;
49 |
--------------------------------------------------------------------------------
/client/src/pages/PlayGame.tsx:
--------------------------------------------------------------------------------
1 | import { useMutation, useQuery } from "@apollo/client";
2 | import React, { useEffect, useState } from "react";
3 | import { useNavigate, useParams } from "react-router-dom";
4 | import { GetGameDocument, Question, SubmitAnswerDocument } from "../generated";
5 | import useInterval from "../useInterval";
6 |
7 | const PlayGame: React.FC = () => {
8 | const [apiError, setApiError] = useState("");
9 | const { id, playerId } = useParams() as { id: string; playerId: string };
10 | const navigate = useNavigate();
11 | const [timeRemaining, setTimeRemaining] = useState(30);
12 | const [answer, setAnswer] = useState("");
13 | const { loading, data } = useQuery(GetGameDocument, { variables: { id } });
14 | const [question, setQuestion] = useState<{
15 | question: string;
16 | answers: string[];
17 | id: string;
18 | }>();
19 | const [submitAnswer, { loading: mutationLoading }] = useMutation(
20 | SubmitAnswerDocument
21 | );
22 | const [questions, setQuestions] = useState<
23 | Pick