GamePanel.jsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { useState } from 'react';
  2. import styled from 'styled-components';
  3. import Loading from '../../util/Loading';
  4. import GuessPane from "./GuessPane";
  5. import PositionedStreetView from "./PositionedStreetView";
  6. import useRoundInfo from "../../../hooks/useRoundInfo";
  7. import { dispatch } from '../../../domain/gameStore';
  8. import { useTimerFromLocalStorage, clearTimerFromLocalStorage } from '../../../domain/localStorageMethods';
  9. import usePreventNavigation from '../../../hooks/usePreventNavigation';
  10. const Container = styled.div`
  11. height: 100%;
  12. width: 100%;
  13. display: flex;
  14. flex-flow: column nowrap;
  15. justify-content: space-between;
  16. align-items: center;
  17. @media only screen and (min-width: 600px) and (min-height: 600px) {
  18. display: block;
  19. position: relative;
  20. margin-bottom: 2px;
  21. margin-right: 2px;
  22. }
  23. `
  24. const StreetViewWrapper = styled.div`
  25. height: 100%;
  26. width: 100%;
  27. margin-bottom: 2px;
  28. flex: 3;
  29. `
  30. export default () => {
  31. // warn the user if they navigate away
  32. usePreventNavigation();
  33. const [submitDisabled, setSubmitDisabled] = useState(false);
  34. const [selectedPoint, setSelectedPoint] = useState(null);
  35. const [finished, roundInfo] = useRoundInfo();
  36. if (finished) {
  37. dispatch.goToSummary();
  38. return <Loading/>
  39. }
  40. const { currentRound, targetPoint, roundSeconds } = roundInfo;
  41. if (!currentRound || !targetPoint || !roundSeconds) {
  42. return <Loading/>
  43. }
  44. const storedTimer = useTimerFromLocalStorage();
  45. const handleSubmitGuess = async () => {
  46. setSubmitDisabled(true);
  47. await dispatch.submitGuess(selectedPoint, currentRound, targetPoint);
  48. clearTimerFromLocalStorage();
  49. }
  50. return (
  51. <Container>
  52. <StreetViewWrapper>
  53. <PositionedStreetView position={targetPoint} />
  54. </StreetViewWrapper>
  55. <GuessPane
  56. roundSeconds={storedTimer ?? roundSeconds}
  57. onTimeout={handleSubmitGuess}
  58. onSelectPoint={setSelectedPoint}
  59. onSubmitGuess={handleSubmitGuess}
  60. submitDisabled={submitDisabled || selectedPoint === null}
  61. />
  62. </Container>
  63. );
  64. }