Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import React, { ChangeEvent, useState } from "react";
  2.  
  3. import { Form, Container } from "semantic-ui-react";
  4. import "./Login.css";
  5. import { axiosGetContentAction, User } from "../../actions/actions";
  6. import { connect } from "react-redux";
  7. import { Dispatch } from "redux";
  8. import { Redirect } from "react-router";
  9. import { IState } from "../../reducers/rootReducer";
  10.  
  11. interface IPropsFromState {
  12. token: string,
  13. error: string
  14. }
  15.  
  16. interface IPropsFromDispatch {
  17. onRequestUser: (d: User) => ReturnType<typeof axiosGetContentAction.request>
  18. }
  19.  
  20. type Props = IPropsFromState & IPropsFromDispatch;
  21.  
  22. interface IUser {
  23. login: string;
  24. email: string;
  25. password: string;
  26. }
  27.  
  28. const Login = (props: Props) => {
  29.  
  30. const initialState: IUser = {
  31. login: "",
  32. email: "",
  33. password: "",
  34. };
  35.  
  36. const [userCredentials, setUserCredentials] = useState(initialState);
  37.  
  38. // console.log(userCredentials);
  39.  
  40. const handleChange = (e: ChangeEvent<HTMLInputElement>): void => {
  41. const { name, value } = e.target;
  42. setUserCredentials({ ...userCredentials, ...{ [name]: value } });
  43. };
  44.  
  45. const handleSubmit = (e: React.FormEvent) => {
  46. e.preventDefault();
  47.  
  48. const { login, email, password } = userCredentials;
  49.  
  50. console.log(`After submit: login: ${login}, email: ${email} password: ${password}`);
  51.  
  52. //dispatch must be here
  53. if (login && email && password) {
  54. props.onRequestUser(userCredentials)
  55. }
  56.  
  57. setUserCredentials({ login: "", email: "", password: "" });
  58. };
  59.  
  60. return (
  61.  
  62. <div>
  63. {
  64. !props.token ?
  65. <Container style={{ width: "200px" }} textAlign="center">
  66.  
  67. <Form onSubmit={ handleSubmit } className="form" style={{ paddingTop: "200px" }}>
  68. <div>{props.error}</div>
  69.  
  70. <Form.Field required>
  71. <Form.Input
  72. type="text"
  73. placeholder="Login"
  74. name={ "login" }
  75. value={ userCredentials.login }
  76. onChange={ handleChange }
  77. />
  78. </Form.Field>
  79.  
  80. <Form.Field required>
  81. <Form.Input
  82. type="email"
  83. placeholder="Email"
  84. name={ "email" }
  85. value={ userCredentials.email }
  86. onChange={ handleChange }
  87. />
  88. </Form.Field>
  89.  
  90.  
  91. <Form.Field required>
  92. <Form.Input
  93. type="password"
  94. placeholder="Password"
  95. name={ "password" }
  96. value={ userCredentials.password }
  97. onChange={ handleChange }
  98. />
  99. </Form.Field>
  100.  
  101. <Form.Button
  102. content="LogIn"
  103. disabled = {
  104. !userCredentials.login
  105. || !userCredentials.email
  106. || !userCredentials.password
  107. }
  108. />
  109. </Form>
  110. </Container>
  111. : <Redirect to='/dashboard' />
  112. }
  113. </div>
  114. );
  115. };
  116.  
  117. const mapStateToProps = (state: IState) => {
  118. return {
  119. token: state.token,
  120. error: state.error
  121. }
  122. };
  123.  
  124. const mapDispatchToProps = (dispatch : Dispatch) => {
  125. return {
  126. onRequestUser: (data: User) => {
  127. return dispatch(axiosGetContentAction.request(data));
  128. }
  129. }
  130. };
  131.  
  132. export default connect(mapStateToProps, mapDispatchToProps)(Login);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement