AllenYuan

settingsform

May 5th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import ListErrors from './ListErrors';
  2. import React from 'react';
  3. import { Link } from 'react-router';
  4. import agent from '../agent';
  5. import { connect } from 'react-redux';
  6.  
  7. // extract settings and current user from state
  8. const mapStateToProps = state => ({
  9.   ...state.settings,
  10.   currentUser: state.common.currentUser
  11. });
  12.  
  13. const mapDispatchToProps = dispatch => ({
  14.   // LOGOUT dispatch
  15.   onClickLogout: () => dispatch({ type: 'LOGOUT' }),
  16.   // SETTINGS_SAVED dispatch + settings form promise
  17.   onSubmitForm: user =>
  18.     dispatch({ type: 'SETTINGS_SAVED', payload: agent.Auth.save(user) })
  19. });
  20.  
  21. class SettingsForm extends React.Component {
  22.   constructor() {
  23.     super();
  24.  
  25.     // init state
  26.     this.state = {
  27.       image: '',
  28.       username: '',
  29.       bio: '',
  30.       email: '',
  31.       password: ''
  32.     };
  33.  
  34.     // changehandler to update state of given field
  35.     this.updateState = field => ev => {
  36.       const state = this.state;
  37.       const newState = Object.assign({}, state, { [field]: ev.target.value });
  38.       this.setState(newState);
  39.     };
  40.  
  41.     // dispatch promise with updated settings
  42.     this.submitForm = ev => {
  43.       ev.preventDefault();
  44.  
  45.       const user = Object.assign({}, this.state);
  46.       if (!user.password) {
  47.         delete user.password;
  48.       }
  49.  
  50.       this.props.onSubmitForm(user);
  51.     };
  52.   }
  53.  
  54.   // initialize state to currentuser's settings
  55.   componentWillMount() {
  56.     if (this.props.currentUser) {
  57.       Object.assign(this.state, {
  58.         image: this.props.currentUser.image || '',
  59.         username: this.props.currentUser.username,
  60.         bio: this.props.currentUser.bio,
  61.         email: this.props.currentUser.email
  62.       });
  63.     }
  64.   }
  65.  
  66.   // new user data => update state
  67.   componentWillReceiveProps(nextProps) {
  68.     if (nextProps.currentUser) {
  69.       this.setState(Object.assign({}, this.state, {
  70.         image: this.props.currentUser.image || '',
  71.         username: this.props.currentUser.username,
  72.         bio: this.props.currentUser.bio,
  73.         email: this.props.currentUser.email
  74.       }));
  75.     }
  76.   }
  77.  
  78.   // render form component
  79.   render() {
  80.     return (
  81.       <form onSubmit={this.submitForm}>
  82.         <fieldset>
  83.           <fieldset className="form-group">
  84.             <input
  85.               className="form-control"
  86.               type="text"
  87.               placeholder="URL of profile picture"
  88.               value={this.state.image}
  89.               onChange={this.updateState('image')} />
  90.           </fieldset>
  91.  
  92.           <fieldset className="form-group">
  93.             <input
  94.               className="form-control form-control-lg"
  95.               type="text"
  96.               placeholder="Username"
  97.               value={this.state.username}
  98.               onChange={this.updateState('username')} />
  99.           </fieldset>
  100.  
  101.           <fieldset className="form-group">
  102.             <textarea
  103.               className="form-control form-control-lg"
  104.               rows="8"
  105.               placeholder="Short bio about you"
  106.               value={this.state.bio}
  107.               onChange={this.updateState('bio')} />
  108.           </fieldset>
  109.  
  110.           <fieldset className="form-group">
  111.             <input
  112.               className="form-control form-control-lg"
  113.               type="email"
  114.               placeholder="Email"
  115.               value={this.state.email}
  116.               onChange={this.updateState('email')} />
  117.           </fieldset>
  118.  
  119.           <fieldset className="form-group">
  120.             <input
  121.               className="form-control form-control-lg"
  122.               type="password"
  123.               placeholder="New Password"
  124.               value={this.state.password}
  125.               onChange={this.updateState('password')} />
  126.           </fieldset>
  127.  
  128.           <button
  129.             className="btn btn-lg btn-primary pull-xs-right"
  130.             type="submit"
  131.             disabled={this.state.inProgress}>
  132.             Update Settings
  133.           </button>
  134.         </fieldset>
  135.       </form>
  136.     );
  137.   }
  138. }
  139.  
  140. class Settings extends React.Component {
  141. // ...
Add Comment
Please, Sign In to add comment