Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ListErrors from './ListErrors';
- import React from 'react';
- import { Link } from 'react-router';
- import agent from '../agent';
- import { connect } from 'react-redux';
- // extract settings and current user from state
- const mapStateToProps = state => ({
- ...state.settings,
- currentUser: state.common.currentUser
- });
- const mapDispatchToProps = dispatch => ({
- // LOGOUT dispatch
- onClickLogout: () => dispatch({ type: 'LOGOUT' }),
- // SETTINGS_SAVED dispatch + settings form promise
- onSubmitForm: user =>
- dispatch({ type: 'SETTINGS_SAVED', payload: agent.Auth.save(user) })
- });
- class SettingsForm extends React.Component {
- constructor() {
- super();
- // init state
- this.state = {
- image: '',
- username: '',
- bio: '',
- email: '',
- password: ''
- };
- // changehandler to update state of given field
- this.updateState = field => ev => {
- const state = this.state;
- const newState = Object.assign({}, state, { [field]: ev.target.value });
- this.setState(newState);
- };
- // dispatch promise with updated settings
- this.submitForm = ev => {
- ev.preventDefault();
- const user = Object.assign({}, this.state);
- if (!user.password) {
- delete user.password;
- }
- this.props.onSubmitForm(user);
- };
- }
- // initialize state to currentuser's settings
- componentWillMount() {
- if (this.props.currentUser) {
- Object.assign(this.state, {
- image: this.props.currentUser.image || '',
- username: this.props.currentUser.username,
- bio: this.props.currentUser.bio,
- email: this.props.currentUser.email
- });
- }
- }
- // new user data => update state
- componentWillReceiveProps(nextProps) {
- if (nextProps.currentUser) {
- this.setState(Object.assign({}, this.state, {
- image: this.props.currentUser.image || '',
- username: this.props.currentUser.username,
- bio: this.props.currentUser.bio,
- email: this.props.currentUser.email
- }));
- }
- }
- // render form component
- render() {
- return (
- <form onSubmit={this.submitForm}>
- <fieldset>
- <fieldset className="form-group">
- <input
- className="form-control"
- type="text"
- placeholder="URL of profile picture"
- value={this.state.image}
- onChange={this.updateState('image')} />
- </fieldset>
- <fieldset className="form-group">
- <input
- className="form-control form-control-lg"
- type="text"
- placeholder="Username"
- value={this.state.username}
- onChange={this.updateState('username')} />
- </fieldset>
- <fieldset className="form-group">
- <textarea
- className="form-control form-control-lg"
- rows="8"
- placeholder="Short bio about you"
- value={this.state.bio}
- onChange={this.updateState('bio')} />
- </fieldset>
- <fieldset className="form-group">
- <input
- className="form-control form-control-lg"
- type="email"
- placeholder="Email"
- value={this.state.email}
- onChange={this.updateState('email')} />
- </fieldset>
- <fieldset className="form-group">
- <input
- className="form-control form-control-lg"
- type="password"
- placeholder="New Password"
- value={this.state.password}
- onChange={this.updateState('password')} />
- </fieldset>
- <button
- className="btn btn-lg btn-primary pull-xs-right"
- type="submit"
- disabled={this.state.inProgress}>
- Update Settings
- </button>
- </fieldset>
- </form>
- );
- }
- }
- class Settings extends React.Component {
- // ...
Add Comment
Please, Sign In to add comment