|
@@ -7,6 +7,8 @@ import PlayerNameInput from "../../util/PlayerNameInput";
|
|
|
import LobbyPlayerList from "./LobbyPlayerList";
|
|
|
import { joinGame } from "../../../domain/GGSHService";
|
|
|
import DelayedButton from "../../util/DelayedButton";
|
|
|
+import useObservable from "../../../hooks/useObservable";
|
|
|
+import { playerNameStore, gameIdStore } from "../../../domain/store";
|
|
|
|
|
|
const InfoContainer = styled.div`
|
|
|
flex: 3;
|
|
@@ -48,24 +50,30 @@ const getUrl = gameId => {
|
|
|
return u.href;
|
|
|
}
|
|
|
|
|
|
-const LobbyInfo = ({ gameId, playerName, onStart }) => (
|
|
|
- <InfoContainer>
|
|
|
- <Label>Playing as {playerName}</Label>
|
|
|
- <Label>Use this link to invite other players:</Label>
|
|
|
- <Label><ClickToCopy text={getUrl(gameId)} /></Label>
|
|
|
- <StyledDelayButton
|
|
|
- onEnd={onStart}
|
|
|
- countDownFormatter={rem => `Click to cancel, ${rem}s...`}
|
|
|
- >
|
|
|
- Start Game
|
|
|
- </StyledDelayButton>
|
|
|
- </InfoContainer>
|
|
|
-);
|
|
|
+const LobbyInfo = ({ onStart }) => {
|
|
|
+ const gameId = useObservable(gameIdStore);
|
|
|
+ const playerName = useObservable(playerNameStore);
|
|
|
+
|
|
|
+ return (
|
|
|
+ <InfoContainer>
|
|
|
+ <Label>Playing as {playerName}</Label>
|
|
|
+ <Label>Use this link to invite other players:</Label>
|
|
|
+ <Label><ClickToCopy text={getUrl(gameId)} /></Label>
|
|
|
+ <StyledDelayButton
|
|
|
+ onEnd={onStart}
|
|
|
+ countDownFormatter={rem => `Click to cancel, ${rem}s...`}
|
|
|
+ >
|
|
|
+ Start Game
|
|
|
+ </StyledDelayButton>
|
|
|
+ </InfoContainer>
|
|
|
+ )
|
|
|
+};
|
|
|
|
|
|
-const JoinForm = ({ gameId, onGameJoined }) => {
|
|
|
+const JoinForm = ({ onGameJoined }) => {
|
|
|
+ const gameId = useObservable(gameIdStore);
|
|
|
+ const playerName = useObservable(playerNameStore);
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
const [failed, setFailed] = useState(false);
|
|
|
- const [playerName, setPlayerName] = useState(null);
|
|
|
|
|
|
if (loading) {
|
|
|
return <InfoContainer><Loading/></InfoContainer>
|
|
@@ -75,7 +83,7 @@ const JoinForm = ({ gameId, onGameJoined }) => {
|
|
|
// TODO would like to support re-joining a game you left
|
|
|
setLoading(true);
|
|
|
try {
|
|
|
- await joinGame(gameId, playerName);
|
|
|
+ await joinGame();
|
|
|
} catch (err) {
|
|
|
// failed to join the game
|
|
|
setFailed(true);
|
|
@@ -83,7 +91,8 @@ const JoinForm = ({ gameId, onGameJoined }) => {
|
|
|
return;
|
|
|
}
|
|
|
setFailed(false);
|
|
|
- onGameJoined({ gameId, playerName });
|
|
|
+ playerNameStore.set(playerName);
|
|
|
+ onGameJoined();
|
|
|
};
|
|
|
const cannotJoinGame = !playerName || playerName.length === 0;
|
|
|
|
|
@@ -91,20 +100,20 @@ const JoinForm = ({ gameId, onGameJoined }) => {
|
|
|
<InfoContainer>
|
|
|
{failed && <Label>Failed to join the game! Maybe try a different name?</Label>}
|
|
|
<Label>Joining {gameId}...</Label>
|
|
|
- <PlayerNameInput playerName={playerName} onChangePlayerName={setPlayerName}/>
|
|
|
+ <PlayerNameInput/>
|
|
|
<FormButton onClick={onJoinGame} disabled={cannotJoinGame}>Join Game</FormButton>
|
|
|
</InfoContainer>
|
|
|
);
|
|
|
}
|
|
|
|
|
|
-const PreRound = ({ gameId, playerName, joined, onGameJoined, onStart }) => (
|
|
|
+const PreRound = ({ joined, onGameJoined, onStart }) => (
|
|
|
<PageContainer>
|
|
|
{
|
|
|
joined
|
|
|
- ? <LobbyInfo gameId={gameId} playerName={playerName} onStart={onStart}/>
|
|
|
- : <JoinForm gameId={gameId} onGameJoined={onGameJoined}/>
|
|
|
+ ? <LobbyInfo onStart={onStart}/>
|
|
|
+ : <JoinForm onGameJoined={onGameJoined}/>
|
|
|
}
|
|
|
- <LobbyPlayerList gameId={gameId} />
|
|
|
+ <LobbyPlayerList/>
|
|
|
</PageContainer>
|
|
|
);
|
|
|
|