signIn.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Form, Input, Button, Row, Col, Alert } from 'antd';
  2. import { connect } from 'react-redux';
  3. import { actionFullLogin } from '../action';
  4. import { useHistory } from 'react-router-dom';
  5. import { useEffect, useState } from 'react';
  6. const Sign = ({ getState, auth, logStatus, user }) => {
  7. const [logMessage, setLogMessage] = useState(false);
  8. const 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 &&
  59. logStatus === 'RESOLVED' &&
  60. (auth).length === 0 &&
  61. <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
  62. <Alert
  63. message="Ошибка"
  64. description="Неправильно введен логин или пароль"
  65. type="error"
  66. />
  67. </Form.Item>}
  68. </Form>
  69. </Row>
  70. );
  71. };
  72. const mapStateToProps = state => ({
  73. auth: state.auth?.token || '',
  74. logStatus: state.promise.login?.status || '',
  75. user: state.auth || ''
  76. });
  77. const SignIn = connect(mapStateToProps, { getState: actionFullLogin })(Sign);
  78. export default SignIn;