-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
333 additions
and
215 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
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 |
---|---|---|
@@ -1,73 +1,14 @@ | ||
import React, { useState } from 'react'; | ||
import React from 'react'; | ||
import { Route, Switch, useRouteMatch } from 'react-router-dom'; | ||
import PlayerGame from './PlayerGame'; | ||
import { joinGame } from './db/game'; | ||
|
||
export default function PlayDash({ setDashState, user }) { | ||
const [participantId, setParticipantId] = useState(null); | ||
const [gameCode, setGameCode] = useState(null); | ||
const [game, setGame] = useState(null); | ||
const [error, setError] = useState(null); | ||
|
||
function loadGame() { | ||
if (gameCode) { | ||
joinGame(user.id, gameCode) | ||
.then((resp) => { | ||
if (resp.ok) { | ||
return resp.json(); | ||
} else { | ||
throw new Error('There was an error loading your game'); | ||
} | ||
}) | ||
.then(([{ participantId: pId, game }]) => { | ||
setGame(game.quiz); | ||
setParticipantId(pId); | ||
}) | ||
.catch((e) => setError(e.message)); | ||
} else { | ||
setError('Missing game code'); | ||
} | ||
} | ||
|
||
return game ? ( | ||
<PlayerGame | ||
game={game} | ||
key={`${gameCode}-${participantId}`} | ||
participantId={participantId} | ||
/> | ||
) : ( | ||
<> | ||
{error ? <div className="bg-danger">{error}</div> : null} | ||
<form> | ||
<div className="form-group"> | ||
<label>Game Code</label> | ||
<input | ||
className="form-control" | ||
type="text" | ||
onChange={(e) => { | ||
setError(null); | ||
setGameCode(e.target.value); | ||
}} | ||
/> | ||
</div> | ||
<div> | ||
<button | ||
className="btn btn-dark" | ||
type="button" | ||
onClick={() => loadGame()} | ||
> | ||
Play | ||
</button> | ||
</div> | ||
</form> | ||
<div> | ||
<button | ||
className="btn btn-dark btn-sm" | ||
type="button" | ||
onClick={() => setDashState(null)} | ||
> | ||
Back | ||
</button> | ||
</div> | ||
</> | ||
export default function PlayDash({ user }) { | ||
const match = useRouteMatch(); | ||
return ( | ||
<Switch> | ||
<Route path={`${match.path}/:gameCode`}> | ||
<PlayerGame user={user} /> | ||
</Route> | ||
</Switch> | ||
); | ||
} |
Oops, something went wrong.