forked from polka_billy/porridger
Added TypeScript for frontend
Added type definitions for components, functions, data Added guards for network responses fixes #8
This commit is contained in:
92
front/src/pages/LoginPage.tsx
Normal file
92
front/src/pages/LoginPage.tsx
Normal file
@ -0,0 +1,92 @@
|
||||
import { FormEventHandler } from 'react'
|
||||
import { Form, Button, Card, Tabs, Tab } from "react-bootstrap"
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import { useAuth } from "../hooks/api";
|
||||
import { setToken } from "../utils/auth";
|
||||
|
||||
function LoginPage() {
|
||||
const navigate = useNavigate()
|
||||
|
||||
const { doAuth } = useAuth() // TODO: Add loading and error handling
|
||||
|
||||
const handleAuth = (newAccount: boolean): FormEventHandler<HTMLFormElement> => async (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
const formData = new FormData(event.currentTarget)
|
||||
|
||||
const data = {
|
||||
email: formData.get('email') as string,
|
||||
name: newAccount ? formData.get('name') as string : undefined,
|
||||
surname: newAccount ? formData.get('surname') as string : undefined,
|
||||
password: formData.get('password') as string
|
||||
}
|
||||
|
||||
const token = import.meta.env.PROD ? await doAuth(data, newAccount) : "a"
|
||||
|
||||
if (token) {
|
||||
setToken(token)
|
||||
navigate("/")
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="m-4">
|
||||
<Card.Body>
|
||||
<Tabs defaultActiveKey="register" fill justify className="mb-3">
|
||||
<Tab eventKey="register" title="Регистрация">
|
||||
<Form onSubmit={handleAuth(true)}>
|
||||
<Form.Group className="mb-3" controlId="email">
|
||||
<Form.Label>Почта</Form.Label>
|
||||
<Form.Control type="email" required />
|
||||
</Form.Group>
|
||||
|
||||
<Form.Group className="mb-3" controlId="name">
|
||||
<Form.Label>Имя</Form.Label>
|
||||
<Form.Control type="text" required />
|
||||
</Form.Group>
|
||||
|
||||
<Form.Group className="mb-3" controlId="surname">
|
||||
<Form.Label>Фамилия</Form.Label>
|
||||
<Form.Control type="text" required />
|
||||
</Form.Group>
|
||||
|
||||
<Form.Group className="mb-3" controlId="password">
|
||||
<Form.Label>Пароль</Form.Label>
|
||||
<Form.Control type="password" required />
|
||||
</Form.Group>
|
||||
|
||||
<Form.Group className="mb-3" controlId="privacyPolicyConsent">
|
||||
<Form.Check type="checkbox" required label="Я согласен с условиями обработки персональных данных" />
|
||||
</Form.Group>
|
||||
|
||||
<Button variant="success" type="submit">
|
||||
Зарегистрироваться
|
||||
</Button>
|
||||
</Form>
|
||||
</Tab>
|
||||
<Tab eventKey="login" title="Вход">
|
||||
<Form onSubmit={handleAuth(false)}>
|
||||
<Form.Group className="mb-3" controlId="email">
|
||||
<Form.Label>Почта</Form.Label>
|
||||
<Form.Control type="email" required />
|
||||
</Form.Group>
|
||||
|
||||
<Form.Group className="mb-3" controlId="password">
|
||||
<Form.Label>Пароль</Form.Label>
|
||||
<Form.Control type="password" required />
|
||||
</Form.Group>
|
||||
|
||||
<Button variant="success" type="submit">
|
||||
Войти
|
||||
</Button>
|
||||
</Form>
|
||||
</Tab>
|
||||
</Tabs>
|
||||
</Card.Body>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
export default LoginPage
|
Reference in New Issue
Block a user