Ready.tsx
1.39 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
import React, { useCallback, useContext } from 'react';
import SocketContext from '../../contexts/SocketContext';
import { MessageType, RawMessage } from '../common/types';
interface ReadyProps {
isReady: boolean;
isAdmin: boolean;
isAllReady: boolean;
}
export const Ready: React.FC<ReadyProps> = ({ isReady, isAdmin, isAllReady }) => {
const socket = useContext(SocketContext);
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, () => {});
}
}, []);
return (
<button className={`${isAdmin ? isAllReady ? 'bg-green-500' : 'bg-gray-400'
: isReady ? 'bg-green-600'
: 'bg-green-500 active:bg-green-600'}
text-white font-bold uppercase text-sm
px-5 py-2 ml-3 rounded shadow
outline-none focus:outline-none hover:shadow-md
ease-linear transition-all duration-100`}
type="button"
onClick={() => {}}>{isAdmin ? 'Start' : 'Ready'}</button>
);
}