Skip to content

Commit

Permalink
Using linter
Browse files Browse the repository at this point in the history
  • Loading branch information
ChampionBuffalo1 committed Jun 23, 2023
1 parent 7f8d416 commit 05346a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function App() {
// Connect to websockets in here
const token = localStorage.getItem('token');
navigate(token ? '/home' : '/login');
}, []);
}, [navigate]);

return (
<div className="flex justify-center items-center h-screen">
Expand Down
6 changes: 3 additions & 3 deletions src/components/AuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AxiosStatusError } from '../typings';
import { useMutation } from '@tanstack/react-query';
import { AuthenticationProps } from '../pages/Authentication';

const userErrCode = [17, 19, 22];
const userErrCode = new Set([17, 19, 22]);

export default function AuthForm({ type }: AuthenticationProps) {
const mutation = useMutation({
Expand Down Expand Up @@ -34,7 +34,7 @@ export default function AuthForm({ type }: AuthenticationProps) {
username,
password
});
}, []);
}, [mutation]);

return (
<div className="grid gap-6">
Expand All @@ -43,7 +43,7 @@ export default function AuthForm({ type }: AuthenticationProps) {
<div className="grid gap-2">
<label htmlFor="username">Username</label>
{mutation.isError &&
userErrCode.includes((mutation.error as AxiosStatusError).response?.data.errors[0].code) && (
userErrCode.has((mutation.error as AxiosStatusError).response?.data.errors[0].code) && (
<DisplayError message={(mutation.error as AxiosStatusError).response.data.errors[0].name} />
)}
<input
Expand Down
20 changes: 11 additions & 9 deletions src/lib/ws.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
let socket: WebSocket | null = null;
let socket: WebSocket | undefined;

const startWebsocket = (url: string, messageHandler: Function): void => {
type handler = (message: Record<string, unknown>) => void;

const startWebsocket = (url: string, messageHandler: handler): void => {
if (socket) return;
socket = new WebSocket(url);
socket.onmessage = event => {
socket.addEventListener('message', event => {
const jsonMessage = JSON.parse(event.data);
messageHandler(jsonMessage);
};
socket.onclose = event => {
console.log('Websocket connection closed with reason: ', event);
socket = null;
};
});
socket.addEventListener('close', event => {
console.log('Websocket connection closed with reason: ' + event);
socket = undefined;
});
};

const sendMessage = (message: string): void => socket?.send(JSON.stringify(message));

const closeSocket = () => {
socket?.close();
socket = null;
socket = undefined;
};

export { startWebsocket, sendMessage, closeSocket };

0 comments on commit 05346a4

Please sign in to comment.