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