@@ -2,111 +2,102 @@ import { SOCKET_BASE_URL } from 'app/../config/config';
22import * as SocketHandlerInterfaces from 'app/types/SocketHandler' ;
33import * as React from 'react' ;
44import * as io from 'socket.io-client' ;
5+ import { useToasts } from 'react-toast-notifications' ;
56
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+ } ) ;
1614
17- public componentDidMount ( ) {
15+ React . useEffect ( ( ) => {
1816 const {
1917 sendCompileError,
2018 sendCompileSuccess,
2119 sendExecuteError,
2220 sendExecuteSuccess,
23- sendInfo,
24- sendSuccess,
25- sendError,
2621 sendDebugRunSuccess,
2722 sendDebugRunError,
28- } = this . props ;
23+ } = props ;
2924
30- this . socket . on ( 'Info' , ( message : string ) => {
31- sendInfo ( message ) ;
25+ socket . on ( 'Info' , ( message : string ) => {
26+ addToast ( message , { appearance : 'info' , autoDismiss : true } ) ;
3227 } ) ;
3328
34- this . socket . on ( 'Success' , ( message : string ) => {
35- sendSuccess ( message ) ;
29+ socket . on ( 'Success' , ( message : string ) => {
30+ addToast ( message , { appearance : 'success' , autoDismiss : true } ) ;
3631 } ) ;
3732
38- this . socket . on ( 'Error' , ( message : string ) => {
39- sendError ( message ) ;
33+ socket . on ( 'Error' , ( message : string ) => {
34+ addToast ( message , { appearance : 'error' , autoDismiss : true } ) ;
4035 } ) ;
4136
42- this . socket . on ( 'connect' , ( ) => {
43- sendSuccess ( 'Connected to Server!' ) ;
37+ socket . on ( 'connect' , ( ) => {
38+ addToast ( 'Connected to Server!' , { appearance : 'success' , autoDismiss : true } ) ;
4439 } ) ;
4540
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 } ) ;
4843 } ) ;
4944
50- this . socket . on ( 'Compile Success' , ( ) => {
51- sendSuccess ( 'Compiled Successfully!' ) ;
45+ socket . on ( 'Compile Success' , ( ) => {
46+ addToast ( 'Compiled Successfully!' , { appearance : 'success' , autoDismiss : true } ) ;
5247 sendCompileSuccess ( ) ;
5348 } ) ;
5449
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 ( '' ) ;
5853 } ) ;
5954
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 ) ;
6357 } ) ;
6458
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 } ) ;
6761 } ) ;
6862
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 ) ;
7265 } ) ;
7366
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 } ) ;
7669 } ) ;
7770
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 } ) ;
8073 } ) ;
8174
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 } ) ;
8477 sendExecuteSuccess ( matchLogs ) ;
8578 } ) ;
8679
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 } ) ;
8982 } ) ;
9083
91- this . socket . on ( 'Debug Run Success' , ( stackTrace : string ) => {
84+ socket . on ( 'Debug Run Success' , ( stackTrace : string ) => {
9285 sendDebugRunSuccess ( stackTrace ) ;
9386 } ) ;
9487
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 ( ) ;
9891 } ) ;
9992
100- this . socket . on ( 'disconnect' , ( ) => {
101- sendError ( 'Disconnected' ) ;
93+ socket . on ( 'disconnect' , ( ) => {
94+ addToast ( 'Disconnected' , { appearance : 'error' , autoDismiss : true } ) ;
10295 } ) ;
103- }
10496
105- public componentWillUnmount ( ) {
106- this . socket . disconnect ( ) ;
107- }
97+ return ( ) => {
98+ socket . disconnect ( ) ;
99+ } ;
100+ } , [ ] ) ;
108101
109- public render ( ) {
110- return null ;
111- }
112- }
102+ return null ;
103+ } ;
0 commit comments