GameCreationForm.jsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React, { useState } from "react";
  2. import styled from "styled-components";
  3. import ms from "pretty-ms";
  4. import Loading from "./Loading";
  5. import Button from "./Button";
  6. import { createGame } from "../../domain/apiMethods";
  7. import Dropdown from "./Dropdown";
  8. const Container = styled.div`
  9. display: flex;
  10. flex-flow: column-reverse nowrap;
  11. justify-content: space-between;
  12. width: 50%;
  13. align-self: center;
  14. `
  15. const DropdownContainer = styled.div`
  16. display: flex;
  17. flex-flow: row nowrap;
  18. justify-content: center;
  19. align-items: center;
  20. width: 100%;
  21. `;
  22. const StartButton = styled(Button)`
  23. margin-bottom: 5px;
  24. width: 100%;
  25. `
  26. export default ({ afterCreate }) => {
  27. const [loading, setLoading] = useState(false);
  28. const [timer, setTimer] = useState(300);
  29. const [rounds, setRounds] = useState(5);
  30. const [onlyAmerica, setOnlyAmerica] = useState(false);
  31. if (loading) {
  32. return <Loading/>
  33. }
  34. const onCreateGame = async () => {
  35. setLoading(true);
  36. const gameId = await createGame(timer, rounds, onlyAmerica);
  37. if (afterCreate) {
  38. afterCreate(gameId);
  39. }
  40. };
  41. return (
  42. <Container>
  43. <DropdownContainer>
  44. <Dropdown
  45. options={{
  46. "30 Seconds": 30,
  47. "2 Minutes": 120,
  48. "5 Minutes": 300,
  49. "1 Hour": 3600,
  50. }}
  51. onChange={setTimer}
  52. >
  53. Round Timer: {ms(timer * 1000)}
  54. </Dropdown>
  55. <Dropdown
  56. options={{
  57. "1 Round": 1,
  58. "3 Rounds": 3,
  59. "5 Rounds": 5,
  60. "10 Rounds": 10,
  61. }}
  62. onChange={setRounds}
  63. >
  64. Rounds: {rounds}
  65. </Dropdown>
  66. <Dropdown
  67. options={{
  68. "All Countries": false,
  69. "Just America": true,
  70. }}
  71. onChange={setOnlyAmerica}
  72. >
  73. {onlyAmerica ? "Just America" : "All Countries" }
  74. </Dropdown>
  75. </DropdownContainer>
  76. <StartButton onClick={onCreateGame}>
  77. Create New Game
  78. </StartButton>
  79. </Container>
  80. );
  81. }