Lobby.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { Fragment, useRef } from "react";
  2. import Colors from "./util/colors";
  3. import deck from './img/deck.svg';
  4. import { Commands } from "./StateRouter";
  5. import Pawn from "./Pawn";
  6. import { useDebounce } from "./util/useDebounce";
  7. export default function Lobby({ message, pickColor, deal, setName }) {
  8. const { color, options, state, game_code, others, name, ...args } = message;
  9. const pickableColors = options.filter((o) => o.code === Commands.PICK_COLOR).map((o) => o.color);
  10. const scheduleSetName = useDebounce(setName, 500)
  11. const circle_args = { state, myColor: color, myName: name, setMyName: scheduleSetName, others, pickableColors, pickColor };
  12. const canDeal = options.find((o) => o.code === Commands.DEAL);
  13. return <Fragment>
  14. <div className="top-bar">
  15. <span>Je kan meedoen door naar <a
  16. href={`/${game_code}`}>{window.location.origin}/{game_code}</a> te gaan</span>
  17. </div>
  18. <div className="content">
  19. <div className="container">
  20. <div className="row justify-content-center my-2">
  21. <PawnWrapper color={Colors.RED} {...circle_args}/>
  22. <PawnWrapper color={Colors.GREEN} {...circle_args}/>
  23. </div>
  24. <div className="row justify-content-center my2">
  25. <PawnWrapper color={Colors.BLUE} {...circle_args}/>
  26. <PawnWrapper color={Colors.YELLOW} {...circle_args}/>
  27. </div>
  28. </div>
  29. </div>
  30. {
  31. canDeal &&
  32. <div className="row justify-content-center my-2">
  33. <div className="btn btn-link" onClick={deal}>
  34. <img src={deck} alt="kaartenstapel"/>
  35. <br/>
  36. <span>Delen</span>
  37. </div>
  38. </div>
  39. }
  40. </Fragment>
  41. }
  42. function PawnWrapper({ color, myColor, myName, setMyName, others, pickableColors, pickColor }) {
  43. const isMine = color === myColor;
  44. const playerInfo = isMine ?
  45. { color, name: myName } :
  46. others.find((o) => o.color === color);
  47. const isSelectable = pickableColors.includes(color);
  48. const isSelected = !isSelectable;
  49. const name = (playerInfo && playerInfo.name) || "\u00a0";
  50. return <div className="col col-lg-3 col-md-4">
  51. <Pawn onClick={pickColor} size="large" {...{color, isMine, isSelectable, isSelected}} />
  52. {isMine ?
  53. <form className="color-pick-name">
  54. <input className="form-control"
  55. type="text"
  56. defaultValue={myName}
  57. placeholder="Naam"
  58. onChange={(e) => {
  59. e.preventDefault();
  60. setMyName(e.target.value);
  61. }}/>
  62. </form> :
  63. <div className="color-pick-name">{name}</div>
  64. }
  65. </div>
  66. }