All files / src/pages LoginPage.tsx

100% Statements 82/82
100% Branches 13/13
80% Functions 4/5
100% Lines 82/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 951x     1x 38x 38x 38x   38x 9x 9x 9x 2x 2x 2x 7x 7x 9x   38x 38x 38x 38x 38x 38x 38x 38x 38x 38x   38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x   38x 38x 38x 38x 38x 38x 38x 38x 38x 19x 19x 19x 38x 38x   38x 2x 2x 2x 36x   38x   38x   38x   38x 38x 38x   38x  
import { useState } from "react";
import type { Role, Session } from "../auth/session";
 
export function LoginPage({ onLogin }: { onLogin: (session: Session) => void }) {
  const [role, setRole] = useState<Role>("operator");
  const [apiKey, setApiKey] = useState("");
  const [error, setError] = useState<string | null>(null);
 
  function submit(event: React.FormEvent) {
    event.preventDefault();
    const trimmed = apiKey.trim();
    if (trimmed.length > 0 && trimmed.length < 4) {
      setError("Ключ доступа слишком короткий");
      return;
    }
    setError(null);
    onLogin({ role, apiKey: trimmed });
  }
 
  return (
    <div className="login">
      <form className="login__card" onSubmit={submit} aria-label="Вход в консоль">
        <header className="login__head">
          <span className="brand__mark">RD</span>
          <div>
            <h1>Robot Dispatch</h1>
            <p>Консоль флота: задачи, роботы, состояние сервиса</p>
          </div>
        </header>
 
        <fieldset className="login__roles">
          <legend>Роль</legend>
          <div className={role === "operator" ? "role role--active" : "role"}>
            <input
              id="role-operator"
              type="radio"
              name="role"
              value="operator"
              checked={role === "operator"}
              onChange={() => setRole("operator")}
            />
            <label htmlFor="role-operator">
              <strong>Оператор</strong>
              <small>создавать и отменять задачи</small>
            </label>
          </div>
          <div className={role === "viewer" ? "role role--active" : "role"}>
            <input
              id="role-viewer"
              type="radio"
              name="role"
              value="viewer"
              checked={role === "viewer"}
              onChange={() => setRole("viewer")}
            />
            <label htmlFor="role-viewer">
              <strong>Наблюдатель</strong>
              <small>только просмотр</small>
            </label>
          </div>
        </fieldset>
 
        <label className="field" htmlFor="api-key">
          <span>Ключ доступа (X-API-Key)</span>
          <input
            id="api-key"
            type="password"
            autoComplete="off"
            placeholder="оставьте пустым, если сервер без ключа"
            value={apiKey}
            onChange={(event) => {
              setApiKey(event.target.value);
              setError(null);
            }}
          />
        </label>
 
        {error ? (
          <p className="error-text" role="alert">
            {error}
          </p>
        ) : null}
 
        <button type="submit" className="button button--primary">
          Войти
        </button>
 
        <p className="login__hint">
          Ключ хранится в sessionStorage вкладки и не отправляется никуда, кроме API.
        </p>
      </form>
    </div>
  );
}