File size: 1,895 Bytes
79278ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// LoginForm.tsx
import Button from "@/components/generics/button/Button";
import Input from "@/components/generics/input/Input";
import { useAuth } from "@/shared/hooks/useAuth/useAuth";
import { useMemo, useState } from "react";
import Modal from "react-modal";

import "./LoginForm.scss";

const customStyles = {
  content: {
    top: "50%",
    left: "50%",
    right: "auto",
    bottom: "auto",
    transform: "translate(-50%, -50%)",
    borderRadius: "15px",
    width: "400px",
  },
};

const LoginForm = () => {
  const { login, isAuth } = useAuth();
  const [value, setValue] = useState<string>("");
  const [error, setError] = useState<string | null>(null);

  const rules = useMemo(
    () => [
      {
        rule: (newValue: string) => newValue.trim().length >= 5,
        errorMessage: "Пожалуйста, введите имя пользователя длиной не менее 5 символов",
      },
    ],
    []
  );

  const onSetValue = (newValue: string) => {
    if (newValue.length <= 65) {
      setValue(newValue);
    }
  };

  const onSetError = (newError: string | null) => {
    setError(newError);
  };

  const handleLogin = () => {
    if (error == null) {
      login({ name: value.trim() });
    }
  };

  return (
    <Modal style={customStyles} isOpen={!isAuth} ariaHideApp={false}>
      <div className={"login_form"}>
        <h3>Введите ваше имя</h3>
        <Input
          name={"userName"}
          placeholder={"Иванов И. И."}
          rules={rules}
          value={value}
          onSetValue={onSetValue}
          error={error}
          onSetError={onSetError}
        />
        <div className="button_container">
          <Button name="Войти" onClick={handleLogin} disabled={value.trim().length === 0 || error != null} />
        </div>
      </div>
    </Modal>
  );
};

export default LoginForm;