Guest User

Untitled

a guest
Sep 13th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import { translate } from 'react-translate';
  3. import {Link} from 'react-router-dom';
  4. import { connect } from 'react-redux';
  5. import {
  6. signOutAction, loginWithEmailAndPasswordAction,
  7. loginWithSocialAuthProviderAction, getRedirectResultAction
  8. } from '../../actions/userActions';
  9.  
  10. import './style.css';
  11. import EmailValidator from '../../utility/EmailValidator';
  12.  
  13. import Button from '../button';
  14. import FormInput from '../FromInput';
  15.  
  16. class Login extends Component{
  17.  
  18. constructor(props){
  19. super(props);
  20. this.state = this.getInitialState();
  21. this.handleChange = this.handleChange.bind(this);
  22. this.handleCheck = this.handleCheck.bind(this);
  23. this.handleBlur = this.handleBlur.bind(this);
  24. this.handleSubmit = this.handleSubmit.bind(this);
  25. this.shouldMarkError = this.shouldMarkError.bind(this);
  26. this.handleSocialLogin = this.handleSocialLogin.bind(this);
  27. }
  28.  
  29. registerRedirect = () => {
  30. this.setState((state,props)=>{
  31. return {isRedirectRegistered: true}
  32. });
  33. }
  34.  
  35. getInitialState = () => {
  36. return {
  37. isRedirectRegistered: false,
  38. user: {
  39. email: '',
  40. password: '',
  41. rememberMe: ''
  42. },
  43. touched:{
  44. email: false,
  45. password: false
  46. }
  47. }
  48. }
  49.  
  50. componentDidMount() {
  51. if (!this.state.isRedirectRegistered) {
  52. this.props.getRedirectResult();
  53. this.registerRedirect();
  54. }
  55. }
  56.  
  57. handleChange(e) {
  58. let user = this.state.user;
  59. user[e.target.name] = e.target.value;
  60. this.setState({ user: user });
  61. }
  62.  
  63. handleCheck(e){
  64. let user = this.state.user;
  65. user[e.target.name] = e.target.checked;
  66. this.setState({ user: user });
  67. }
  68.  
  69. handleSocialLogin(e, providerName) {
  70. this.props.loginWithSocialAuthProvider(providerName);
  71. }
  72.  
  73. handleBlur(e) {
  74. let touched = this.state.touched;
  75. touched[e.target.name] = true;
  76. this.setState({ touched: touched });
  77. }
  78.  
  79. handleSubmit(e) {
  80. e.preventDefault();
  81. if (!this.canBeSubmitted()) {
  82. return;
  83. }
  84. //some actions
  85. this.props.loginWithEmailAndPassword(this.state.user.email,this.state.user.password)
  86. }
  87.  
  88. canBeSubmitted() {
  89. let validations = this.validate(this.state.user);
  90. //can be submitted or not, so opposite of if validations are true.
  91. return !Object.keys(validations).some(x => validations[x]);
  92. }
  93.  
  94. validate = (user) => {
  95. return {
  96. email: !this.isEmailValid() || user.email === undefined || user.email.length === 0,
  97. password: user.password === undefined || user.password.length === 0 || user.password.length < 6
  98. };
  99. }
  100.  
  101. isEmailValid() {
  102. if (this.state.user.email)
  103. return EmailValidator.validate(this.state.user.email);
  104. return true;
  105. }
  106.  
  107. shouldMarkError(field){
  108. const errors = this.validate(this.state.user);
  109. const hasError = errors[field];
  110. const shouldShow = this.state.touched[field];
  111. return (hasError && shouldShow) ? 'error' : '';
  112. }
  113.  
  114. render(){
  115. const {t} = this.props;
  116.  
  117. // BUTTONS DATA
  118.  
  119. const googleBtn = {
  120. icon: 'icon-google',
  121. btnData:{
  122. btnText: t('btn-google-plus'),
  123. btnClass: 'google-plus-button',
  124. btnAction: (e) => this.handleSocialLogin(e, 'google')
  125. }
  126. }
  127.  
  128. const facebookBtn = {
  129. icon: 'icon-facebook',
  130. btnData:{
  131. btnText: t('btn-facebook'),
  132. btnClass: 'facebook-button',
  133. btnAction: (e) => this.handleSocialLogin(e, 'facebook')
  134. }
  135. }
  136.  
  137. const signUpBtn = {
  138. btnData:{
  139. btnText: t('btn-log-in'),
  140. btnClass: 'action-button',
  141. btnAction: this.handleSubmit
  142. },
  143. }
  144.  
  145. // INPUTS DATA
  146.  
  147. const emailInput = {
  148. icon:'icon-mail',
  149. input:{
  150. type: 'text',
  151. placeholder: t('input-email'),
  152. name:'email',
  153. class: this.shouldMarkError('email'),
  154. action:{
  155. onChange:this.handleChange,
  156. onBlur:this.handleBlur
  157. }
  158. }
  159. }
  160.  
  161. const passwordInput = {
  162. icon:'icon-padlock',
  163. input:{
  164. type: 'Password',
  165. placeholder: t('input-password'),
  166. name:'password',
  167. class: this.shouldMarkError('password'),
  168. action:{
  169. onChange:this.handleChange,
  170. onBlur:this.handleBlur
  171. }
  172. }
  173. }
  174.  
  175. return(
  176. <div className=" form-container log-in">
  177. <h1 className="title">{t('title')}</h1>
  178.  
  179. <Button
  180. btn={googleBtn.btnData}
  181. icon={googleBtn.icon}/>
  182.  
  183. <Button
  184. btn={facebookBtn.btnData}
  185. icon={facebookBtn.icon}/>
  186.  
  187. <p className="text">{t('log-email')}</p>
  188. <form>
  189. <FormInput
  190. icon={emailInput.icon}
  191. input={emailInput.input}/>
  192.  
  193. <FormInput
  194. icon={passwordInput.icon}
  195. input={passwordInput.input}/>
  196.  
  197.  
  198. <div className="form-group">
  199. <div className="custom-control custom-checkbox">
  200. <input type="checkbox"
  201. className="custom-control-input"
  202. id="remember-check"
  203. onChange={this.handleCheck}
  204. />
  205. <label className="custom-control-label" htmlFor="remember-check">
  206. {t('check-remember')}
  207. </label>
  208. </div>
  209. </div>
  210. <Button
  211. btn={signUpBtn.btnData}/>
  212. </form>
  213.  
  214. <Link className="link-text" to="/">
  215. {t('forgot-your-password')}
  216. </Link>
  217.  
  218. <div className="sign-up-text-container">
  219. <p className="text" >
  220. {t('dont-account')}
  221. <Link className="link-text" to="/">
  222. {t('sing-up')}
  223. </Link>
  224. </p>
  225. </div>
  226. </div>
  227. );
  228. }
  229.  
  230. }
  231.  
  232. const mapStateToProps = (state) => ({
  233. user: state.user.user,
  234. isLoading: state.user.isLoading
  235. });
  236.  
  237. const mapDispatchToProps = (dispatch) => ({
  238. logout: () => dispatch(signOutAction()),
  239. loginWithEmailAndPassword: (email, password) => dispatch(loginWithEmailAndPasswordAction(email, password)),
  240. loginWithSocialAuthProvider: (providerName) => dispatch(loginWithSocialAuthProviderAction(providerName)),
  241. getRedirectResult: () => dispatch(getRedirectResultAction())
  242. });
  243.  
  244. export default connect(mapStateToProps,mapDispatchToProps)(translate("Login")(Login));
Add Comment
Please, Sign In to add comment