Guest User

Untitled

a guest
Feb 17th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. import React from "react";
  2. import FontAwesomeIcon from "@fortawesome/react-fontawesome";
  3. import { Link } from "react-router-dom";
  4. import axios from "axios";
  5. import { Alert } from "reactstrap";
  6. import PropTypes from "prop-types";
  7. import { connect } from "react-redux";
  8. import { login } from "../../actions/authActions";
  9. import { LoadingButton, ConfirmAble, Input } from "../../elements";
  10.  
  11. class Login extends React.Component {
  12. constructor(props) {
  13. super(props);
  14.  
  15. this.handleSubmit = this.handleSubmit.bind(this);
  16. this.errorMessageOnDismiss = this.errorMessageOnDismiss.bind(this);
  17.  
  18. this.state = {
  19. email: "",
  20. password: "",
  21. formErrors: {
  22. email: "",
  23. password: ""
  24. },
  25. formSubmit: false,
  26. errorMessage: "",
  27. errorMessageVisible: false
  28. };
  29. }
  30.  
  31. errorMessageOnDismiss() {
  32. this.setState(prevState => ({
  33. errorMessageVisible: false
  34. }));
  35. }
  36.  
  37. handleSubmit(e) {
  38. e.preventDefault();
  39.  
  40. this.setState(prevState => ({
  41. formSubmit: true,
  42. errorMessageVisible: false
  43. }));
  44.  
  45. const user = {
  46. email: this.state.email,
  47. password: this.state.password
  48. };
  49.  
  50. this.props
  51. .login(user)
  52. .then(
  53. res => this.context.router.history.push("/home"),
  54. err => {
  55. var settedFormErrors = {};
  56. for (var key in err.response.data) {
  57. settedFormErrors[key] = err.response.data[key][0];
  58. }
  59.  
  60. this.setState(prevState => ({
  61. formErrors: {
  62. ...prevState.formErrors,
  63. ...settedFormErrors
  64. }
  65. }));
  66. }
  67. )
  68. .then(() => {
  69. console.log("asdasasd");
  70. this.setState(prevState => ({
  71. formSubmit: false
  72. }));
  73. });
  74. }
  75.  
  76. render() {
  77. return (
  78. <div className="row">
  79. <div className="col-md-8 offset-md-2 col-lg-6 offset-lg-3">
  80. <div className="card auth-card">
  81. <div className="card-body shadow-3">
  82. <div className="card-title text-center mb-3">
  83. Giriş Yap
  84. </div>
  85. <hr />
  86. {this.state.errorMessage.length > 0 ? (
  87. <Alert
  88. color="danger"
  89. isOpen={this.state.errorMessageVisible}
  90. toggle={this.errorMessageOnDismiss}
  91. >
  92. {this.state.errorMessage}
  93. </Alert>
  94. ) : (
  95. ""
  96. )}
  97. <form
  98. onSubmit={this.handleSubmit}
  99. className="table-responsive"
  100. autoComplete="off"
  101. >
  102. <table className="table table-borderless mb-0">
  103. <tbody>
  104. <tr>
  105. <td>E-mail</td>
  106. <td>
  107. <ConfirmAble
  108. name="email"
  109. value={this.props.email}
  110. message={
  111. this.state.formErrors
  112. .email
  113. }
  114. />
  115. </td>
  116. </tr>
  117. <tr>
  118. <td>Şifre</td>
  119. <td>
  120. <ConfirmAble
  121. type="password"
  122. name="password"
  123. value={this.props.password}
  124. message={
  125. this.state.formErrors
  126. .password
  127. }
  128. />
  129. </td>
  130. </tr>
  131. </tbody>
  132. <tfoot>
  133. <tr>
  134. <td colSpan="2">
  135. <LoadingButton
  136. text="Giriş Yap"
  137. block
  138. submit
  139. isLoading={
  140. this.state.formSubmit
  141. }
  142. />
  143. </td>
  144. </tr>
  145. <tr>
  146. <td colSpan="2">
  147. <Link
  148. to="/forget_password"
  149. className="btn btn-link"
  150. >
  151. Şifremi Unuttum
  152. </Link>
  153. <Link
  154. to="/register"
  155. className="btn btn-link float-right"
  156. >
  157. Kayıt Ol
  158. </Link>
  159. </td>
  160. </tr>
  161. </tfoot>
  162. </table>
  163. </form>
  164. </div>
  165. </div>
  166. </div>
  167. </div>
  168. );
  169. }
  170. }
  171.  
  172. Login.propTypes = {
  173. login: PropTypes.func.isRequired
  174. };
  175.  
  176. Login.contextTypes = {
  177. router: PropTypes.object.isRequired
  178. };
  179.  
  180. export default connect(null, { login })(Login);
  181.  
  182. import React from "react";
  183. import PropTypes from "prop-types";
  184. import { connect } from "react-redux";
  185.  
  186. import Input from "./input";
  187. import FeedBack from "../static/feedback";
  188.  
  189. class ConfirmAble extends React.Component {
  190. constructor(props) {
  191. super(props);
  192. }
  193.  
  194. render() {
  195. const { message } = this.props;
  196.  
  197. return (
  198. <div>
  199. <Input {...this.props} />
  200. <FeedBack text={message} />
  201. </div>
  202. );
  203. }
  204. }
  205.  
  206. ConfirmAble.propTypes = {
  207. message: PropTypes.string.isRequired
  208. };
  209.  
  210. export default connect(null)(ConfirmAble);
  211.  
  212. import React from "react";
  213. import PropTypes from "prop-types";
  214.  
  215. class Input extends React.Component {
  216. constructor(props) {
  217. super(props);
  218.  
  219. this.state = {
  220. [props.name]: props.value
  221. };
  222.  
  223. this.handleUserInput = this.handleUserInput.bind(this);
  224. }
  225.  
  226. handleUserInput(e) {
  227. var _name = e.target.name,
  228. _value = e.target.value;
  229. this.setState(() => ({
  230. [_name]: _value
  231. }));
  232. }
  233.  
  234. render() {
  235. const {
  236. type,
  237. value,
  238. placeholder,
  239. className,
  240. name,
  241. id,
  242. form_control,
  243. disabled,
  244. readOnly,
  245. minLength,
  246. maxLength,
  247. ref,
  248. autoFocus,
  249. message
  250. } = this.props;
  251.  
  252. let CLASS =
  253. (form_control ? "form-control" : "") + " " + (className || "");
  254.  
  255. if (message) {
  256. CLASS += (className ? " " : "") + "is-invalid ";
  257. }
  258.  
  259. return (
  260. <input
  261. type={type}
  262. name={name}
  263. value={this.state[name]}
  264. placeholder={placeholder}
  265. className={CLASS}
  266. id={id}
  267. disabled={disabled}
  268. readOnly={readOnly}
  269. minLength={minLength}
  270. maxLength={maxLength}
  271. ref={ref}
  272. autoFocus={autoFocus}
  273. onChange={this.handleUserInput}
  274. />
  275. );
  276. }
  277. }
  278.  
  279. Input.defaultProps = {
  280. type: "text",
  281. form_control: true,
  282. value: ""
  283. };
  284.  
  285. Input.propTypes = {
  286. type: PropTypes.string,
  287. value: PropTypes.string,
  288. placeholder: PropTypes.string,
  289. className: PropTypes.string,
  290. name: PropTypes.string.isRequired,
  291. id: PropTypes.string,
  292. form_control: PropTypes.bool,
  293. disabled: PropTypes.bool,
  294. readOnly: PropTypes.bool,
  295. minLength: PropTypes.number,
  296. maxLength: PropTypes.number,
  297. ref: PropTypes.string,
  298. autoFocus: PropTypes.bool
  299. };
  300.  
  301. export default Input;
Add Comment
Please, Sign In to add comment