Showing
1 changed file
with
29 additions
and
9 deletions
1 | -import React, { useCallback, useContext } from 'react'; | 1 | +import React, { useCallback, useContext, useEffect, useState } from 'react'; |
2 | +import { useLocation } from 'react-router'; | ||
2 | import SocketContext from '../../contexts/SocketContext'; | 3 | import SocketContext from '../../contexts/SocketContext'; |
3 | import { MessageType, RawMessage } from '../common/types'; | 4 | import { MessageType, RawMessage } from '../common/types'; |
5 | +import { User } from './types'; | ||
6 | + | ||
7 | +interface ReadyLocation { | ||
8 | + state: { username: string } | ||
9 | +} | ||
4 | 10 | ||
5 | interface ReadyProps { | 11 | interface ReadyProps { |
6 | - isReady: boolean; | 12 | + users: User[]; |
7 | - isAdmin: boolean; | ||
8 | - isAllReady: boolean; | ||
9 | } | 13 | } |
10 | 14 | ||
11 | -export const Ready: React.FC<ReadyProps> = ({ isReady, isAdmin, isAllReady }) => { | 15 | +export const Ready: React.FC<ReadyProps> = ({ users }) => { |
12 | const socket = useContext(SocketContext); | 16 | const socket = useContext(SocketContext); |
17 | + const location: ReadyLocation = useLocation(); | ||
18 | + | ||
19 | + const [ isAdmin, setIsAdmin ] = useState(false); | ||
20 | + const [ isReady, setIsReady ] = useState(false); | ||
21 | + const [ isAllReady, setIsAllReady ] = useState(false); | ||
22 | + | ||
23 | + useEffect(() => { | ||
24 | + const me = users.find(x => x.username === location.state.username); | ||
25 | + setIsAdmin(me?.admin || false); | ||
26 | + setIsReady(me?.ready || false); | ||
27 | + | ||
28 | + const test = true; | ||
29 | + users.forEach(x => test && x.ready); | ||
30 | + setIsAllReady(test); | ||
31 | + }); | ||
32 | + | ||
13 | const handleReady = useCallback(() => { | 33 | const handleReady = useCallback(() => { |
14 | if (isAdmin && isAllReady) { | 34 | if (isAdmin && isAllReady) { |
15 | const rawMessage: RawMessage = { | 35 | const rawMessage: RawMessage = { |
... | @@ -24,17 +44,17 @@ export const Ready: React.FC<ReadyProps> = ({ isReady, isAdmin, isAllReady }) => | ... | @@ -24,17 +44,17 @@ export const Ready: React.FC<ReadyProps> = ({ isReady, isAdmin, isAllReady }) => |
24 | } | 44 | } |
25 | socket.emit('msg', rawMessage, () => {}); | 45 | socket.emit('msg', rawMessage, () => {}); |
26 | } | 46 | } |
27 | - }, []); | 47 | + }, [isAdmin, isReady, isAllReady]); |
28 | 48 | ||
29 | return ( | 49 | return ( |
30 | <button className={`${isAdmin ? isAllReady ? 'bg-green-500' : 'bg-gray-400' | 50 | <button className={`${isAdmin ? isAllReady ? 'bg-green-500' : 'bg-gray-400' |
31 | : isReady ? 'bg-green-600' | 51 | : isReady ? 'bg-green-600' |
32 | : 'bg-green-500 active:bg-green-600'} | 52 | : 'bg-green-500 active:bg-green-600'} |
33 | - text-white font-bold uppercase text-sm | 53 | + text-white font-bold uppercase |
34 | - px-5 py-2 ml-3 rounded shadow | 54 | + px-7 py-3 m-8 rounded shadow |
35 | outline-none focus:outline-none hover:shadow-md | 55 | outline-none focus:outline-none hover:shadow-md |
36 | ease-linear transition-all duration-100`} | 56 | ease-linear transition-all duration-100`} |
37 | type="button" | 57 | type="button" |
38 | - onClick={() => {}}>{isAdmin ? 'Start' : 'Ready'}</button> | 58 | + onClick={() => handleReady()}>{isAdmin ? 'Start' : 'Ready'}</button> |
39 | ); | 59 | ); |
40 | } | 60 | } | ... | ... |
-
Please register or login to post a comment