|
1 | 1 | import { SOCKET_BASE_URL } from 'app/../config/config'; |
2 | 2 | import * as SocketHandlerInterfaces from 'app/types/SocketHandler'; |
3 | 3 | import * as React from 'react'; |
| 4 | +import { useToasts } from 'react-toast-notifications'; |
4 | 5 | import * as io from 'socket.io-client'; |
5 | 6 |
|
6 | | -export class SocketHandler extends React.Component<SocketHandlerInterfaces.Props, {}> { |
7 | | - private socket: SocketIOClient.Socket; |
8 | | - constructor(props: SocketHandlerInterfaces.Props) { |
9 | | - super(props); |
10 | | - this.socket = io.connect(SOCKET_BASE_URL, { |
11 | | - reconnection: true, |
12 | | - reconnectionDelay: 1000, |
13 | | - transports: ['websocket'], |
14 | | - }); |
15 | | - } |
| 7 | +// tslint:disable-next-line: variable-name |
| 8 | +export const SocketHandler = (props: SocketHandlerInterfaces.Props) => { |
| 9 | + const { addToast } = useToasts(); |
| 10 | + const socket: SocketIOClient.Socket = io.connect(SOCKET_BASE_URL, { |
| 11 | + reconnection: true, |
| 12 | + reconnectionDelay: 1000, |
| 13 | + transports: ['websocket'], |
| 14 | + }); |
16 | 15 |
|
17 | | - public componentDidMount() { |
| 16 | + React.useEffect(() => { |
18 | 17 | const { |
19 | 18 | sendCompileError, |
20 | 19 | sendCompileSuccess, |
21 | 20 | sendExecuteError, |
22 | 21 | sendExecuteSuccess, |
23 | | - sendInfo, |
24 | | - sendSuccess, |
25 | | - sendError, |
26 | 22 | sendDebugRunSuccess, |
27 | 23 | sendDebugRunError, |
28 | | - } = this.props; |
| 24 | + } = props; |
29 | 25 |
|
30 | | - this.socket.on('Info', (message: string) => { |
31 | | - sendInfo(message); |
| 26 | + socket.on('Info', (message: string) => { |
| 27 | + addToast(message, { appearance: 'info', autoDismiss: true }); |
32 | 28 | }); |
33 | 29 |
|
34 | | - this.socket.on('Success', (message: string) => { |
35 | | - sendSuccess(message); |
| 30 | + socket.on('Success', (message: string) => { |
| 31 | + addToast(message, { appearance: 'success', autoDismiss: true }); |
36 | 32 | }); |
37 | 33 |
|
38 | | - this.socket.on('Error', (message: string) => { |
39 | | - sendError(message); |
| 34 | + socket.on('Error', (message: string) => { |
| 35 | + addToast(message, { appearance: 'error', autoDismiss: true }); |
40 | 36 | }); |
41 | 37 |
|
42 | | - this.socket.on('connect', () => { |
43 | | - sendSuccess('Connected to Server!'); |
| 38 | + socket.on('connect', () => { |
| 39 | + addToast('Connected to Server!', { appearance: 'success', autoDismiss: true }); |
44 | 40 | }); |
45 | 41 |
|
46 | | - this.socket.on('Compile Info', (message: string) => { |
47 | | - sendInfo(message); |
| 42 | + socket.on('Compile Info', (message: string) => { |
| 43 | + addToast(message, { appearance: 'info', autoDismiss: true }); |
48 | 44 | }); |
49 | 45 |
|
50 | | - this.socket.on('Compile Success', () => { |
51 | | - sendSuccess('Compiled Successfully!'); |
| 46 | + socket.on('Compile Success', () => { |
| 47 | + addToast('Compiled Successfully!', { appearance: 'success', autoDismiss: true }); |
52 | 48 | sendCompileSuccess(); |
53 | 49 | }); |
54 | 50 |
|
55 | | - this.socket.on('Compile Error', (message: string) => { |
56 | | - sendError(`Compile Error: ${message}`); |
57 | | - sendCompileError(''); |
| 51 | + socket.on('Compile Error', (message: string) => { |
| 52 | + addToast(`Compile Error: ${message}`, { appearance: 'error', autoDismiss: true }), |
| 53 | + sendCompileError(''); |
58 | 54 | }); |
59 | 55 |
|
60 | | - this.socket.on('Compile Error Log', (log: string) => { |
61 | | - sendError('Compile Error'); |
62 | | - sendCompileError(log); |
| 56 | + socket.on('Compile Error Log', (log: string) => { |
| 57 | + addToast('Compile Error', { appearance: 'error', autoDismiss: true }), sendCompileError(log); |
63 | 58 | }); |
64 | 59 |
|
65 | | - this.socket.on('Match Info', (message: string) => { |
66 | | - sendInfo(message); |
| 60 | + socket.on('Match Info', (message: string) => { |
| 61 | + addToast(message, { appearance: 'info', autoDismiss: true }); |
67 | 62 | }); |
68 | 63 |
|
69 | | - this.socket.on('Match Error', (message: string) => { |
70 | | - sendError(message); |
71 | | - sendExecuteError(message); |
| 64 | + socket.on('Match Error', (message: string) => { |
| 65 | + addToast(message, { appearance: 'error', autoDismiss: true }), sendExecuteError(message); |
72 | 66 | }); |
73 | 67 |
|
74 | | - this.socket.on('Match Result Success', (result: string) => { |
75 | | - sendSuccess(result); |
| 68 | + socket.on('Match Result Success', (result: string) => { |
| 69 | + addToast(result, { appearance: 'success', autoDismiss: true }); |
76 | 70 | }); |
77 | 71 |
|
78 | | - this.socket.on('Match Result Error', (result: string) => { |
79 | | - sendError(result); |
| 72 | + socket.on('Match Result Error', (result: string) => { |
| 73 | + addToast(result, { appearance: 'error', autoDismiss: true }); |
80 | 74 | }); |
81 | 75 |
|
82 | | - this.socket.on('Match Success', (matchLogs: string) => { |
83 | | - sendSuccess('Match Executed Successfully!'); |
| 76 | + socket.on('Match Success', (matchLogs: string) => { |
| 77 | + addToast('Match Executed Successfully!', { appearance: 'success', autoDismiss: true }); |
84 | 78 | sendExecuteSuccess(matchLogs); |
85 | 79 | }); |
86 | 80 |
|
87 | | - this.socket.on('Debug Run Info', (message: string) => { |
88 | | - sendInfo(message); |
| 81 | + socket.on('Debug Run Info', (message: string) => { |
| 82 | + addToast(message, { appearance: 'info', autoDismiss: true }); |
89 | 83 | }); |
90 | 84 |
|
91 | | - this.socket.on('Debug Run Success', (stackTrace: string) => { |
| 85 | + socket.on('Debug Run Success', (stackTrace: string) => { |
92 | 86 | sendDebugRunSuccess(stackTrace); |
93 | 87 | }); |
94 | 88 |
|
95 | | - this.socket.on('Debug Run Error', (message: string) => { |
96 | | - sendError(`Debug Run Error: ${message}`); |
97 | | - sendDebugRunError(); |
| 89 | + socket.on('Debug Run Error', (message: string) => { |
| 90 | + addToast(`Debug Run Error: ${message}`, { appearance: 'error', autoDismiss: true }), |
| 91 | + sendDebugRunError(); |
98 | 92 | }); |
99 | 93 |
|
100 | | - this.socket.on('disconnect', () => { |
101 | | - sendError('Disconnected'); |
| 94 | + socket.on('disconnect', () => { |
| 95 | + addToast('Disconnected', { appearance: 'error', autoDismiss: true }); |
102 | 96 | }); |
103 | | - } |
104 | 97 |
|
105 | | - public componentWillUnmount() { |
106 | | - this.socket.disconnect(); |
107 | | - } |
| 98 | + return () => { |
| 99 | + socket.disconnect(); |
| 100 | + }; |
| 101 | + }, []); |
108 | 102 |
|
109 | | - public render() { |
110 | | - return null; |
111 | | - } |
112 | | -} |
| 103 | + return null; |
| 104 | +}; |
0 commit comments