Skip to content

Commit

Permalink
fix(client): handle socket connect and disconnect in provider layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jozwiaczek committed Aug 27, 2021
1 parent 4e49104 commit bf03dbb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect, useMemo } from 'react';
import React, { useContext, useMemo } from 'react';

import { ToggleSlider } from '../../../../../elements';
import { ConnectionState } from '../../../../../enums/connectionState.enum';
Expand All @@ -8,18 +8,7 @@ import GateDisconnected from './components/GateDisconnected';
import { ToggleSliderWrapper } from './TogglingSection.styled';

const TogglingSection = () => {
const { connect, disconnect, toggleGate, connectionState, deviceStatus } =
useContext(WebSocketContext);

useEffect(() => {
void connect();
}, [connect]);

useEffect(() => {
return () => {
disconnect();
};
}, [disconnect]);
const { toggleGate, connectionState, deviceStatus } = useContext(WebSocketContext);

const isConnected = useMemo(
() => connectionState === ConnectionState.CONNECTED && deviceStatus === DeviceStatus.CONNECTED,
Expand Down
12 changes: 11 additions & 1 deletion packages/client/src/providers/api/WebSocketProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import socketIOClient, { Socket } from 'socket.io-client';

import { ConnectionState } from '../../../enums/connectionState.enum';
Expand Down Expand Up @@ -64,6 +64,16 @@ const WebSocketProvider = ({ children }: WebSocketProviderProps) => {
}
}, [socket]);

useEffect(() => {
void connect();
}, [connect]);

useEffect(() => {
return () => {
disconnect();
};
}, [disconnect]);

const toggleGate = useCallback(() => {
if (socket) {
socket.emit(WebSocketEvent.TOGGLE_GATE);
Expand Down

0 comments on commit bf03dbb

Please sign in to comment.