Login.jsx 5.5KB

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