GameCreationForm.jsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import ms from 'pretty-ms';
  2. import React, { useState } from 'react';
  3. import { createGame } from '../../../domain/apiMethods';
  4. import { MAP_CRUNCH, RANDOM_STREET_VIEW, URBAN } from '../../../domain/genMethods';
  5. import Loading from '../Loading';
  6. import OldDropdown, { Dropdown, Item } from './Dropdown';
  7. import styles from './GameCreationForm.module.css';
  8. export default ({ afterCreate }) => {
  9. const [ loading, setLoading ] = useState(false);
  10. const [ timer, setTimer ] = useState(300);
  11. const [ rounds, setRounds ] = useState(5);
  12. const [ onlyAmerica, setOnlyAmerica ] = useState(false);
  13. const [ genMethod, setGenMethod ] = useState(MAP_CRUNCH);
  14. if (loading) {
  15. return <Loading />;
  16. }
  17. const invalidCombo = genMethod === URBAN && onlyAmerica;
  18. const onCreateGame = async () => {
  19. setLoading(true);
  20. const gameId = await createGame(timer, rounds, onlyAmerica, genMethod);
  21. if (afterCreate) {
  22. afterCreate(gameId);
  23. }
  24. };
  25. return (
  26. <div className={styles.form}>
  27. <div className={styles.dropdowns}>
  28. <Dropdown selected={`Round Timer: ${ms(timer * 1000)}`} onSelect={setTimer}>
  29. <Item value={30}>30 Seconds</Item>
  30. <Item value={120}>2 Minutes</Item>
  31. <Item value={300}>5 Minutes</Item>
  32. <Item value={3600}>1 Hour</Item>
  33. </Dropdown>
  34. <OldDropdown
  35. options={{
  36. '1 Round': 1,
  37. '3 Rounds': 3,
  38. '5 Rounds': 5,
  39. '10 Rounds': 10
  40. }}
  41. onChange={setRounds}
  42. >
  43. Rounds: {rounds}
  44. </OldDropdown>
  45. <OldDropdown
  46. options={{
  47. 'All Countries': false,
  48. 'Just America': true
  49. }}
  50. onChange={setOnlyAmerica}
  51. >
  52. {onlyAmerica ? 'Just America' : 'All Countries'}
  53. </OldDropdown>
  54. <OldDropdown
  55. options={{
  56. 'Map Crunch': MAP_CRUNCH,
  57. 'Random Street View': RANDOM_STREET_VIEW,
  58. 'Urban Centers': URBAN
  59. }}
  60. onChange={setGenMethod}
  61. >
  62. Generator:{' '}
  63. {genMethod === MAP_CRUNCH ? 'Map Crunch' : genMethod === RANDOM_STREET_VIEW ? 'RSV' : 'Urban Centers'}
  64. </OldDropdown>
  65. </div>
  66. <button className={styles.start} onClick={onCreateGame} disabled={invalidCombo}>
  67. {invalidCombo ? 'Incompatible Options' : 'New Game'}
  68. </button>
  69. </div>
  70. );
  71. };