Skip to content
Snippets Groups Projects
Login.jsx 2.84 KiB
Newer Older
import React from 'react'
import { Field } from 'formik'
import { isEmpty } from 'lodash'
import { ErrorText, H1, Link, Button, TextField, Checkbox } from '@pubsweet/ui'
import { th } from '@pubsweet/ui-toolkit'
import styled from 'styled-components'
MoSelim's avatar
MoSelim committed
import { Page, Notification } from '../ui'
import SignInFooter from '../SignInFooter'
const Signup = styled.p`
Audrey Hamelers's avatar
Audrey Hamelers committed
  margin-bottom: 0;
`
const ResetPassword = styled.p`
Audrey Hamelers's avatar
Audrey Hamelers committed
  margin-top: 0;
Audrey Hamelers's avatar
Audrey Hamelers committed
const ShowPassword = styled(Checkbox)`
  position: absolute;
  right: 0;
  top: 0;
  font-size: ${th('fontSizeBaseSmall')};
ahamelers's avatar
ahamelers committed
  input {
    transform: scale(1.1);
    zoom: 1.1;
  }
Audrey Hamelers's avatar
Audrey Hamelers committed
`
MoSelim's avatar
MoSelim committed
const PasswordField = styled.div`
  position: relative;
  max-width: 500px;
`
MoSelim's avatar
MoSelim committed

const Container = styled.form`
MoSelim's avatar
MoSelim committed
  margin: 0 0;
  max-width: 350px;
`
const EmailInput = props => <TextField label="Email address" {...props.field} />
const PasswordInput = props => (
  <TextField label="Password" {...props.field} type="password" />
)

MoSelim's avatar
MoSelim committed
const PlainPasswordInput = props => (
  <TextField
    invalidTest={props.invalidTest}
    label="Password"
    {...props.field}
  />
)
MoSelim's avatar
MoSelim committed
class Login extends React.Component {
  state = {
    showPassword: false,
  }
  toggleShowPassword = () => {
    const { showPassword } = this.state
    this.setState({
      showPassword: !showPassword,
    })
  }
  render() {
    const {
      errors,
MoSelim's avatar
MoSelim committed
      handleSubmit,
      signup = true,
      passwordReset = true,
Audrey Hamelers's avatar
Audrey Hamelers committed
      location,
MoSelim's avatar
MoSelim committed
    } = this.props
    return (
      <Page>
        <H1>Sign in with your Europe PMC plus account</H1>
        {!isEmpty(errors) && <Notification type="error">{errors}</Notification>}
        <Container onSubmit={handleSubmit}>
          <Field component={EmailInput} name="email" />
          <PasswordField>
            {!this.state.showPassword && (
              <Field component={PasswordInput} name="password" />
            )}
            {this.state.showPassword && (
              <Field component={PlainPasswordInput} name="password" />
            )}
            <ShowPassword
              checked={this.state.showPassword}
              label="Show password"
              onChange={() => this.toggleShowPassword()}
            />
          </PasswordField>
          <Button disabled={isSubmitting} primary type="submit">
MoSelim's avatar
MoSelim committed
        </Container>
MoSelim's avatar
MoSelim committed
        {signup && (
          <Signup>
Audrey Hamelers's avatar
Audrey Hamelers committed
            {`Don’t have a Europe PMC plus account? `}
Audrey Hamelers's avatar
Audrey Hamelers committed
            <Link to={`/signup${location.search}`}>Create an account.</Link>
MoSelim's avatar
MoSelim committed
          </Signup>
        )}
        {passwordReset && (
          <ResetPassword>
Audrey Hamelers's avatar
Audrey Hamelers committed
            {`Forgot your login details? `}
MoSelim's avatar
MoSelim committed
            <Link to="/password-reset">Reset your password.</Link>
          </ResetPassword>
        )}
        <SignInFooter />
      </Page>
    )
  }
}

// used by tests
export { Login, ErrorText, Signup, ResetPassword }

// used by consumers
export default Login