import React from 'react'; import { socket } from '../../App' import './EnterChat.scss' const EnterChat = ({ setUser }) => { const [authError, setAuthError] = React.useState({ status: false, message: '' }) const loginHandler = e => { e.preventDefault() const username = e.target.elements.username.value.trim()[0].toUpperCase() + e.target.elements.username.value.trim().slice(1) const maxUsernameLength = 20 if (username === '') { setAuthError({ status: true, message: '*Invalid username!' }) return } if (username.match(/\d/)) { setAuthError({ status: true, message: '*Numbers are not allowed!' }) return } if (username.length >= maxUsernameLength) { setAuthError({ status: true, message: `*Max username length: ${maxUsernameLength}` }) return } setUser({ username, isLoggedIn: true }) setAuthError({ status: false, message: '' }) socket.emit('user:connected', username) } return (
) } export default EnterChat