Skip to content
Snippets Groups Projects
SignInForm.tsx 2.61 KiB
Newer Older
import React, { FC } from 'react';
Defendi Alberto's avatar
Defendi Alberto committed
import axios from 'axios';
import { SubmitHandler, useForm } from 'react-hook-form';
Defendi Alberto's avatar
Defendi Alberto committed
import { createStyles, makeStyles, Theme } from '@material-ui/core/styles';
import { Button } from '@material-ui/core';
Defendi Alberto's avatar
Defendi Alberto committed
import { InputField } from 'components/AuthUser/SignInForm/InputField/InputField';
Defendi Alberto's avatar
Defendi Alberto committed
import { useHistory } from 'react-router-dom';
import { AuthRoutes } from 'components/api/routes';
Defendi Alberto's avatar
Defendi Alberto committed

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    root: {
      '& > *': {
        margin: theme.spacing(0),
      },
    },
    form: {
      width: '100%', // Fix IE 11 issue.
      marginTop: theme.spacing(1),
    },
    submit: {
      margin: theme.spacing(3, 0, 2),
    },
  }),
);
export const SignInForm: FC = () => {
Defendi Alberto's avatar
Defendi Alberto committed
  const history = useHistory();

  interface FormData {
Defendi Alberto's avatar
Defendi Alberto committed
    email: string;
    password: string;
Defendi Alberto's avatar
Defendi Alberto committed

  const defaultValues: FormData = {
Defendi Alberto's avatar
Defendi Alberto committed
    email: '',
    password: '',
  };

  const { control, errors, handleSubmit } = useForm<FormData>({
    defaultValues,
  });

  const onSubmit: SubmitHandler<FormData> = (values: FormData) => {
Defendi Alberto's avatar
Defendi Alberto committed
    axios
      .post(
        '/api/web/login',
        {
          username: values.email,
          password: values.password,
Defendi Alberto's avatar
Defendi Alberto committed
        {
          headers: {
            'Content-Type': 'application/json',
          },
        },
      )
      .then(() => history.replace(AuthRoutes.dashboard));
Defendi Alberto's avatar
Defendi Alberto committed
  const classes = useStyles();
  return (
    <>
      <form
        className={classes.form}
        onSubmit={handleSubmit(onSubmit)}
        data-testid="Form"
Defendi Alberto's avatar
Defendi Alberto committed
      >
Defendi Alberto's avatar
Defendi Alberto committed
          name="email"
          control={control}
Defendi Alberto's avatar
Defendi Alberto committed
          rules={{
            validate: (value: string) =>
Defendi Alberto's avatar
Defendi Alberto committed
              /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(value),
            required: {
              value: true,
Defendi Alberto's avatar
Defendi Alberto committed
              message: 'Email is not valid',
Defendi Alberto's avatar
Defendi Alberto committed
            },
          }}
Defendi Alberto's avatar
Defendi Alberto committed
          label="Email"
Defendi Alberto's avatar
Defendi Alberto committed
          errorMessage="Insert email"
Defendi Alberto's avatar
Defendi Alberto committed
        <InputField
Defendi Alberto's avatar
Defendi Alberto committed
          name="password"
Defendi Alberto's avatar
Defendi Alberto committed
          control={control}
          rules={{
            minLength: 8,
            maxLength: 60,
Defendi Alberto's avatar
Defendi Alberto committed
            required: {
              value: true,
Defendi Alberto's avatar
Defendi Alberto committed
              message: 'Insert valid password',
Defendi Alberto's avatar
Defendi Alberto committed
            },
Defendi Alberto's avatar
Defendi Alberto committed
          }}
Defendi Alberto's avatar
Defendi Alberto committed
          label="Password"
Defendi Alberto's avatar
Defendi Alberto committed
          error={!!errors.password}
Defendi Alberto's avatar
Defendi Alberto committed
          errorMessage="Insert valid password"
        <Button
Defendi Alberto's avatar
Defendi Alberto committed
          data-testid="Submit"
          type="submit"
          fullWidth
          variant="contained"
          color="primary"
          className={classes.submit}
        >
          Sign In
        </Button>
      </form>
    </>
Defendi Alberto's avatar
Defendi Alberto committed
  );
};