Login.jsx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { Redirect } from 'react-router'
  4. import { translate } from 'react-i18next'
  5. import LoginLogo from '../component/Login/LoginLogo.jsx'
  6. import LoginLogoImg from '../img/logoTracimWhite.svg'
  7. import { postUserLogin } from '../action-creator.async.js'
  8. import Card from '../component/common/Card/Card.jsx'
  9. import CardHeader from '../component/common/Card/CardHeader.jsx'
  10. import CardBody from '../component/common/Card/CardBody.jsx'
  11. import InputGroupText from '../component/common/Input/InputGroupText.jsx'
  12. import InputCheckbox from '../component/common/Input/InputCheckbox.jsx'
  13. import Button from '../component/common/Input/Button.jsx'
  14. import LoginBtnForgotPw from '../component/Login/LoginBtnForgotPw.jsx'
  15. import {
  16. newFlashMessage,
  17. setUserConnected
  18. } from '../action-creator.sync.js'
  19. import {PAGE} from '../helper.js'
  20. class Login extends React.Component {
  21. constructor (props) {
  22. super(props)
  23. this.state = {
  24. inputLogin: {
  25. value: '',
  26. isInvalid: false
  27. },
  28. inputPassword: {
  29. value: '',
  30. isInvalid: false
  31. },
  32. inputRememberMe: false
  33. }
  34. }
  35. handleChangeLogin = e => this.setState({inputLogin: {...this.state.inputLogin, value: e.target.value}})
  36. handleChangePassword = e => this.setState({inputPassword: {...this.state.inputPassword, value: e.target.value}})
  37. handleChangeRememberMe = () => this.setState(prev => ({inputRememberMe: !prev.inputRememberMe}))
  38. handleClickSubmit = async () => {
  39. const { history, dispatch, t } = this.props
  40. const { inputLogin, inputPassword, inputRememberMe } = this.state
  41. const fetchPostUserLogin = await dispatch(postUserLogin(inputLogin.value, inputPassword.value, inputRememberMe))
  42. if (fetchPostUserLogin.status === 200) {
  43. dispatch(setUserConnected({...fetchPostUserLogin.json, logged: true}))
  44. history.push(PAGE.HOME)
  45. } else if (fetchPostUserLogin.status === 400) {
  46. dispatch(newFlashMessage(t('Login.fail'), 'danger'))
  47. }
  48. }
  49. render () {
  50. if (this.props.user.logged) return <Redirect to={{pathname: '/'}} />
  51. else {
  52. return (
  53. <section className='loginpage'>
  54. <div className='container-fluid'>
  55. <LoginLogo customClass='loginpage__logo' logoSrc={LoginLogoImg} />
  56. <div className='row justify-content-center'>
  57. <div className='col-12 col-sm-11 col-md-8 col-lg-6 col-xl-5'>
  58. <Card customClass='loginpage__connection'>
  59. <CardHeader customClass='connection__header text-center'>{'Connexion'}</CardHeader>
  60. <CardBody formClass='connection__form'>
  61. <div>
  62. <InputGroupText
  63. parentClassName='connection__form__groupemail'
  64. customClass='mb-3 mt-4'
  65. icon='fa-envelope-open-o'
  66. type='email'
  67. placeHolder='Adresse Email'
  68. invalidMsg='Email invalide.'
  69. isInvalid={this.state.inputLogin.isInvalid}
  70. value={this.state.inputLogin.value}
  71. onChange={this.handleChangeLogin}
  72. />
  73. <InputGroupText
  74. parentClassName='connection__form__groupepw'
  75. customClass=''
  76. icon='fa-lock'
  77. type='password'
  78. placeHolder='Mot de passe'
  79. invalidMsg='Mot de passe invalide.'
  80. isInvalid={this.state.inputPassword.isInvalid}
  81. value={this.state.inputPassword.value}
  82. onChange={this.handleChangePassword}
  83. />
  84. <div className='row mt-4 mb-4'>
  85. <div className='col-12 col-sm-6 col-md-6 col-lg-6 col-xl-6'>
  86. <InputCheckbox
  87. parentClassName='connection__form__rememberme'
  88. customClass=''
  89. label='Se souvenir de moi'
  90. checked={this.state.inputRememberMe}
  91. onChange={this.handleChangeRememberMe}
  92. />
  93. </div>
  94. <div className='col-12 col-sm-6 col-md-6 col-lg-6 col-xl-6 text-sm-right'>
  95. <LoginBtnForgotPw
  96. customClass='connection__form__pwforgot'
  97. label='Mot de passe oublié ?'
  98. />
  99. </div>
  100. </div>
  101. <Button
  102. htmlType='button'
  103. bootstrapType='primary'
  104. customClass='connection__form__btnsubmit'
  105. label='Connexion'
  106. onClick={this.handleClickSubmit}
  107. />
  108. </div>
  109. </CardBody>
  110. </Card>
  111. </div>
  112. </div>
  113. </div>
  114. </section>
  115. )
  116. }
  117. }
  118. }
  119. const mapStateToProps = ({ user }) => ({ user })
  120. export default connect(mapStateToProps)(translate()(Login))