Guest User

Untitled

a guest
Jun 24th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withRouter } from 'react-router';
  4. import { connect } from 'react-redux';
  5. import Cookies from 'js-cookie';
  6. import get from 'lodash/get';
  7. import includes from 'lodash/includes';
  8. import { login } from 'actions/access.actions';
  9. import toggleLogin from 'actions/modals.actions';
  10. import cn from 'classnames';
  11.  
  12. import ReactModal from 'react-modal';
  13. import { Tooltip } from 'react-tippy';
  14.  
  15. import './loginForm.scss';
  16.  
  17. class LoginForm extends Component {
  18. static propTypes = {
  19. loginUser: PropTypes.func,
  20. toggleLogin: PropTypes.func,
  21. openLogin: PropTypes.bool.isRequired,
  22. userObj: PropTypes.object,
  23. error: PropTypes.any,
  24. };
  25.  
  26. static defaultProps = {
  27. error: null,
  28. userObj: {},
  29. loginUser: () => null,
  30. toggleLogin: () => null,
  31. };
  32.  
  33. state = {
  34. error: null,
  35. };
  36.  
  37. componentWillReceiveProps(nextProps) {
  38. if (nextProps.userObj && nextProps.userObj.isAuthenticated) {
  39. this.closeModal();
  40. }
  41.  
  42. if (nextProps.error) {
  43. this.setState({ error: nextProps.error });
  44. }
  45. }
  46.  
  47. handleInputChange = (e) => {
  48. const nextState = {};
  49. nextState[e.target.name] = e.target.value;
  50. this.setState(nextState);
  51. };
  52.  
  53. closeModal = () => {
  54. this.props.toggleLogin(false);
  55. };
  56.  
  57. handleSubmitForm = (e) => {
  58. e.preventDefault();
  59. this.props.loginUser(this.email.value, this.password.value);
  60. };
  61.  
  62. handleFocusInput = (e) => {
  63. const label = document.querySelector(`[for=${e.target.id}]`);
  64. if (!label.classList.contains('active')) {
  65. label.classList.add('active');
  66. }
  67. this.resetError();
  68. };
  69.  
  70. handleBlurInput = (e) => {
  71. if (!e.target.value) {
  72. document.querySelector(`[for=${e.target.id}]`).classList.remove('active');
  73. }
  74. };
  75.  
  76. onClickModalWindow = () => {
  77. this.resetError();
  78. };
  79.  
  80. resetError = () => {
  81. if (this.errorElement && this.errorElement.length > 0) {
  82. this.setState({ error: null });
  83. }
  84. };
  85.  
  86. onFacebookLogin = () => {
  87. const inOneHour = new Date(new Date().getTime() + 60 * 60 * 1000);
  88. Cookies.set('lastLocation_before_logging', this.props.location.pathname, { expires: inOneHour });
  89. window.location.href = `${window.location.origin}/auth/facebook`;
  90. };
  91.  
  92. render() {
  93. const { openLogin } = this.props;
  94. const { error } = this.state;
  95. const customModalStyle = {
  96. overlay: {
  97. backgroundColor: 'rgba(0, 0, 0, 0.75)',
  98. },
  99. };
  100.  
  101. const errorMessage = error ? error.message : '';
  102.  
  103. return (
  104. <ReactModal
  105. isOpen={openLogin}
  106. contentLabel="Modal"
  107. ariaHideApp={false}
  108. closeTimeoutMS={500}
  109. onRequestClose={this.closeModal}
  110. style={customModalStyle}
  111. className={{
  112. base: 'loginForm',
  113. afterOpen: 'loginForm_after-open',
  114. beforeClose: 'loginForm_before-close',
  115. }}
  116. >
  117. <div onClick={this.onClickModalWindow}>
  118. <h5 className="loginForm__heading mx-auto mb-2 text-center">Sign in</h5>
  119. <div className="loginForm__formContainer d-flex flex-column px-3 py-4">
  120. <button
  121. type="button"
  122. className="btn facebook-login-container mx-auto mt-2 rounded"
  123. onClick={this.onFacebookLogin}
  124. >
  125. Continue with Facebook
  126. </button>
  127. <div className="form-divider mt-3">
  128. <span className="d-flex flex-row">
  129. <strong className="loginForm__dividerLabel">or</strong>
  130. </span>
  131. </div>
  132. <form className="loginForm__form d-flex flex-column mx-auto mb-2" onSubmit={this.handleSubmitForm}>
  133. <span className="loginForm__formHeader my-3 text-center">Sign in with your email</span>
  134. <div className="form-group">
  135. <div
  136. className={cn('form-group', 'mb-4', {
  137. 'has-error': includes(get(error, 'type'), 'email'),
  138. })}
  139. >
  140. <Tooltip
  141. html={<span>{errorMessage}</span>}
  142. open={includes(get(error, 'type'), 'email')}
  143. onRequestClose={() => this.setState({ error: null })}
  144. >
  145. <input
  146. type="email"
  147. className="form-control floatLabel"
  148. id="registerInputEmail"
  149. required
  150. onChange={this.handleInputChange}
  151. onFocus={this.handleFocusInput}
  152. onBlur={this.handleBlurInput}
  153. autoComplete="email"
  154. ref={el => (this.email = el)}
  155. />
  156. <label htmlFor="registerInputEmail">Email</label>
  157. </Tooltip>
  158. </div>
  159. <div className={cn('form-group', { 'has-error': includes(get(error, 'type'), 'password') })}>
  160. <Tooltip
  161. html={<span>{errorMessage}</span>}
  162. open={includes(get(error, 'type'), 'password')}
  163. onRequestClose={() => this.setState({ error: null })}
  164. >
  165. <input
  166. type="password"
  167. className="form-control floatLabel mt-2"
  168. id="registerInputPassword"
  169. required
  170. onChange={this.handleInputChange}
  171. onFocus={this.handleFocusInput}
  172. onBlur={this.handleBlurInput}
  173. autoComplete="current-password"
  174. ref={el => (this.password = el)}
  175. />
  176. <label htmlFor="registerInputPassword">Password</label>
  177. </Tooltip>
  178. </div>
  179. </div>
  180. <button type="submit" className="btn loginForm__signIn">
  181. Sign in
  182. </button>
  183. </form>
  184. </div>
  185. </div>
  186. </ReactModal>
  187. );
  188. }
  189. }
  190.  
  191. const mapStateToProps = state => ({
  192. userObj: state.access.user,
  193. error: state.access.error,
  194. openLogin: state.toggleModal.login,
  195. });
  196.  
  197. const mapDispatchToProps = dispatch => ({
  198. loginUser: (email, password) => dispatch(login(email, password)),
  199. toggleLogin: newState => dispatch(toggleLogin(newState)),
  200. });
  201.  
  202. export default withRouter(
  203. connect(
  204. mapStateToProps,
  205. mapDispatchToProps,
  206. )(LoginForm),
  207. );
Add Comment
Please, Sign In to add comment