import React from 'react';
import { Button } from 'react-bootstrap';
import ICAT from '../../config/icat/icat';
import { doSignIn } from '../../actions/login';
import { useDispatch } from 'react-redux';

function AnonymousSignIn() {
  const dispatch = useDispatch();

  function handleClick() {
    dispatch(
      doSignIn(
        ICAT.authentication.anonymous.plugin,
        ICAT.authentication.anonymous.username,
        ICAT.authentication.anonymous.password
      )
    );
  }

  if (!ICAT.authentication.anonymous.enabled) {
    return null;
  }

  return (
    <p style={{ marginTop: 5, marginBottom: -10, textAlign: 'center' }}>
      <Button bsStyle="link" onClick={handleClick}>
        or sign in as anonymous
      </Button>
    </p>
  );
}

export default AnonymousSignIn;
