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