import { Stack } from '@mui/material';
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { actionFullRegister } from '../api/api';
import LoginForm from '../components/LoginForm';
import { useNavigate } from 'react-router-dom';
import Title from '../components/Title';

export default function RegisterPage() {
  const token = useSelector(state => state.auth.token);
  const dispatch = useDispatch();
  const navigate = useNavigate();

  useEffect(() => {
    if (token) {
      navigate('/');
    }
  }, [token]);

  const onRegister = (login, password) => {
    dispatch(actionFullRegister({ login, password }));
  }

  return (
    <>
    <Title>Registration</Title>
      <Stack sx={{ maxWidth: '300px', width: '100%', m: '150px auto' }} spacing={2}>
        <LoginForm submit='Registration' onSubmit={onRegister} />
      </Stack>
    </>
  )
}
