Advertisement
Guest User

Untitled

a guest
Apr 11th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. import React, { Component } from 'react';
  4.  
  5. import { connect } from 'react-redux';
  6. import { updatePassword } from '../actions/users-actions';
  7. import { bindActionCreators } from 'redux';
  8.  
  9. class Profile extends Component {
  10.  
  11.   constructor (props) {
  12.     super(props);
  13.  
  14.     this.state = {
  15.       editMode : false
  16.     };
  17.  
  18.   }
  19.  
  20.   onEditFormSubmit (event) {
  21.     event.preventDefault();
  22.     this.setState({
  23.       editMode : true,
  24.       changePass : {
  25.         oldPass : null,
  26.         newPass : null,
  27.         repPass : null
  28.       }
  29.     });
  30.   }
  31.  
  32.   onCancelEdit (event) {
  33.     event.preventDefault();
  34.     this.setState({
  35.       editMode : false,
  36.       changePass : null
  37.     });
  38.   }
  39.  
  40.   onUpdateFormSubmit (event) {
  41.     event.preventDefault();
  42.     if (this.state.changePass.newPass === this.state.changePass.repPass) {
  43.       this.props.updatePassword(this.props.user.id, this.state.changePass.oldPass, this.state.changePass.newPass);
  44.     } else {
  45.       alert('New password and confirmation dont match');
  46.     }
  47.   }
  48. }
  49.  
  50. onInputChange (event) {
  51.  
  52.   var changePass = Object.assign({}, this.state.changePass);
  53.  
  54.   changePass[event.target.id] = event.target.value;
  55.  
  56.   this.setState({
  57.     changePass
  58.   });
  59.  
  60. }
  61.  
  62. userProfile () {
  63.  
  64.   if (this.props.user) {
  65.  
  66.     if (this.state.editMode) {
  67.       return (
  68.         <form onSubmit={this.onUpdateFormSubmit.bind(this)} className="form-group-sm">
  69.           <div className="form-group">
  70.             <input id="oldPass" onChange={this.onInputChange.bind(this)} value={this.state.changePass.oldPass}
  71.                    type="password" className="form-control" placeholder="Old Password" required/>
  72.             <input id="newPass" onChange={this.onInputChange.bind(this)} value={this.state.changePass.newPass}
  73.                    type="password" className="form-control" placeholder="New Password" required/>
  74.             <input id="repPass" onChange={this.onInputChange.bind(this)} value={this.state.changePass.repPass}
  75.                    type="password" className="form-control" placeholder="Repeat New Password" required/>
  76.           </div>
  77.           <div className="form-group">
  78.             <button type="submit" className="btn btn-primary btn-sm btn-block">Save</button>
  79.             <button type="button" onClick={this.onCancelEdit.bind(this)} className="btn btn-warning btn-sm btn-block">
  80.               Cancel
  81.             </button>
  82.           </div>
  83.         </form>
  84.       );
  85.     } else {
  86.       return (
  87.         <div>
  88.           <h3>Username: {this.props.user.username}</h3>
  89.           <h4>Name: {this.props.user.name}</h4>
  90.           <h4>Email: {this.props.user.email}</h4>
  91.           <div className="row">
  92.             <form onSubmit={this.onEditFormSubmit.bind(this)} className="form-group-sm col-xs-6">
  93.               <div className="clearfix">
  94.                 <button type="submit" className="btn btn-info btn-sm">
  95.                   <span className="glyphicon glyphicon-edit"/>
  96.                 </button>
  97.               </div>
  98.             </form>
  99.           </div>
  100.         </div>
  101.       );
  102.     }
  103.  
  104.   } else {
  105.     return (
  106.       <h3>NO SELECTED USER!</h3>
  107.     );
  108.   }
  109.  
  110. }
  111.  
  112. render () {
  113.   return (
  114.     <div className="jumbotron">
  115.       {this.userProfile()}
  116.     </div>
  117.   );
  118. }
  119.  
  120.  
  121.  
  122. function mapStateToProps (state) {
  123.   return {
  124.     user : state.login.user
  125.   };
  126. }
  127.  
  128. function mapDispatchToProps (dispatch) {
  129.   return bindActionCreators({
  130.     updatePassword : updatePassword
  131.   }, dispatch);
  132. }
  133.  
  134. export default connect(mapStateToProps, mapDispatchToProps)(User);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement