client/src/components/Settings.js
author ymh <ymh.work@gmail.com>
Fri, 30 Nov 2018 10:53:15 +0100
changeset 183 f8f3af9e5c83
parent 170 7da1d5137b0b
child 191 3f71ad81a5a9
permissions -rw-r--r--
Change the settings to avoid using Session authentication for rest framework as it raise exceptions in case client and backend are on the same domain On the filter, adapt to take into account new version of django_filters

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import '../App.css';
import Navbar from './Navbar';
import * as userActions from '../actions/userActions';
import './Settings.css';
import { getCurrentUser } from '../selectors/authSelectors';

class Settings extends Component {

  updateSettings = (e) => {
    e.preventDefault();
    const username = this.props.currentUser.username;
    const firstname = this.firstname.value;
    const lastname = this.lastname.value;

    this.props.userActions.updateSettings(username, firstname, lastname);
  }

  render() {

    const firstname = this.props.currentUser ? this.props.currentUser.first_name : '';
    const lastname = this.props.currentUser ? this.props.currentUser.last_name : '';

    return (
      <div>
        <Navbar history={this.props.history} />
        <div className="container-fluid">
          <div className="row">
            <div className="col-lg-6 offset-md-3">
              <form>
                <div className="form-group">
                  <label className="col-form-label text-primary">Prénom</label>
                  <input className="form-control bg-irinotes-form text-muted"
                    name="firstname"
                    defaultValue={ firstname }
                    placeholder="Entrez un prénom"
                    ref={(firstname) => { this.firstname = firstname; }}
                  />
                </div>
                <div className="form-group">
                  <label className="col-form-label text-primary">Nom</label>
                  <input className="form-control bg-irinotes-form text-muted"
                    name="lastname"
                    defaultValue={ lastname }
                    placeholder="Entrez un nom"
                    ref={(lastname) => { this.lastname = lastname; }}
                  />
                </div>
              </form>
              <button type="submit" className="btn btn-primary btn-lg text-secondary" onClick={this.updateSettings}>Enregistrer</button>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

function mapStateToProps(state, props) {
  return {
    currentUser: getCurrentUser(state),
  };
}

function mapDispatchToProps(dispatch) {
  return {
    userActions: bindActionCreators(userActions, dispatch)
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Settings);