signIn.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Form, Input, Button, Checkbox, Row, Col, Alert } from 'antd';
  2. import { connect } from 'react-redux';
  3. import { actionFullLogin } from '../action';
  4. import {Router, Route, Link, Redirect, Switch, useHistory} from 'react-router-dom';
  5. import { useEffect, useState } from 'react';
  6. const Sign = ({getState, auth, logStatus, user}) => { console.log('auth', auth);
  7. const [logMessage, setLogMessage] = useState(false);
  8. let history = useHistory();
  9. useEffect(
  10. () => {
  11. if((auth).length !== 0) {
  12. history.push('/');
  13. }
  14. if(user.payload && user.payload.sub && user.payload.sub.acl.includes('admin') ) {
  15. history.push('/admin');
  16. }
  17. } , [auth])
  18. const onFinish = (values) => {
  19. console.log('Success:', values);
  20. getState(values.username, values.password);
  21. setLogMessage(!logMessage);
  22. };
  23. const onFinishFailed = (errorInfo) => {
  24. console.log('Failed:', errorInfo);
  25. };
  26. return (
  27. <Row>
  28. <Col span ={7}>
  29. </Col>
  30. <Form
  31. name="basic"
  32. labelCol={{ span: 8 }}
  33. wrapperCol={{ span: 16 }}
  34. initialValues={{ remember: true }}
  35. onFinish={onFinish}
  36. onFinishFailed={onFinishFailed}
  37. autoComplete="off"
  38. >
  39. <Form.Item
  40. label="Имя"
  41. name="username"
  42. rules={[{ required: true, message: 'Please input your username!' }]}
  43. >
  44. <Input />
  45. </Form.Item>
  46. <Form.Item
  47. label="Пароль"
  48. name="password"
  49. rules={[{ required: true, message: 'Please input your password!' }]}
  50. >
  51. <Input.Password />
  52. </Form.Item>
  53. <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
  54. <Button type="primary" htmlType="submit">
  55. Войти
  56. </Button>
  57. </Form.Item>
  58. { logMessage && logStatus == 'RESOLVED' && (auth).length == 0 && <Form.Item wrapperCol={{ offset: 8, span: 16 }}> {console.log('tttt')}
  59. <Alert
  60. message="Ошибка"
  61. description="Неправильно введен логин или пароль"
  62. type="error"
  63. />
  64. </Form.Item>}
  65. </Form>
  66. </Row>
  67. )
  68. };
  69. const mapStateToProps = state => ({
  70. auth: state.auth?.token || '',
  71. logStatus: state.promise.login?.status || '',
  72. user: state.auth || '',
  73. })
  74. const SignIn = connect(mapStateToProps, {getState:actionFullLogin})(Sign)
  75. export default SignIn;