Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Druon Mehdi #2

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
ajout update user
Mehdi Druon committed Jun 16, 2017
commit 3e07c44bebe66d8107f44e23acf1ab3908e3417f
1 change: 1 addition & 0 deletions kickass/src/App.js
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import {Users, UserProfile} from './components/Users/Users';
import {Projects, ProjectInfo} from './components/Projects/Project';
import AddUser from './components/Users/NewUser';
import AddProject from './components/Projects/NewProject';

import './App.css';


2 changes: 1 addition & 1 deletion kickass/src/components/Projects/NewProject.js
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ export default class AddProject extends Component {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: this.state.title,
2 changes: 1 addition & 1 deletion kickass/src/components/Users/NewUser.js
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ export default class AddUser extends Component {
type: this.state.type
})
})
.then(res => {res.json()})
.then((res) => res.json())
.catch(err => console.log('error ', err));
}

5 changes: 0 additions & 5 deletions kickass/src/components/Users/Users.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
.userList {
display: flex;
justify-content: space-around;

}
.listNames{
text-decoration: none;
}
55 changes: 51 additions & 4 deletions kickass/src/components/Users/Users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from 'react';
import {Link, Redirect} from 'react-router-dom';
import {Link, Redirect, Route} from 'react-router-dom';
import './Users.css';

export class Users extends Component {
@@ -48,7 +48,10 @@ export class UserProfile extends Component {
super(props);
this.state = {
user: "",
id: this.props.match.params.id
id: this.props.match.params.id,
name: "",
age: "",
type:"",
}
}

@@ -59,7 +62,7 @@ export class UserProfile extends Component {
this.setState({user: json})
})
}

// Supprimer un utilisateur
handleDelete = () => {
fetch(`https://kickass-sdw-3a.herokuapp.com/api/user/${this.state.user._id}`, {
method: "DELETE",
@@ -71,6 +74,39 @@ export class UserProfile extends Component {
.then(<Redirect to="/users" />)
.catch(err => console.log('err', err))
}

//Update un user déjà existant
handleUpdateSubmit= () => {
fetch(`https://kickass-sdw-3a.herokuapp.com/api/user/${this.state.user._id}`, {
method: "PUT",
headers: {
'Accept' : 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: this.state.name,
age: this.state.age,
type: this.state.type
})
})
.then(<Redirect to="/users" />)
.catch( err => console.log('error', err))
}

handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value,
})
}

handleUpdate = () => {
this.setState ({
name: this.state.user.name,
age: this.state.user.age,
type: this.state.user.type
})
}

render(){
return(
<div>
@@ -81,7 +117,18 @@ export class UserProfile extends Component {
<li>Age : {this.state.user.age}</li>
<li>Type : {this.state.user.type}</li>
</ul>
<button onClick={() => this.handleDelete()}>Supprimer OMG WTF</button>
<button onClick={() => this.handleDelete()}>Supprimer l'utilisateur</button>
</div>
<div>
<form>
<p>Nom : </p>
<input type='text' name='name' value={this.state.name} onChange={this.handleChange}></input>
<p>Age : </p>
<input type='text' name='age' value={this.state.age} onChange={this.handleChange}></input>
<p>Type :</p>
<input type='text' name='type' value={this.state.type} onChange={this.handleChange}></input>
<button type='submit' onClick={() => this.handleUpdateSubmit()}>Update</button>
</form>
</div>
</div>
)