Added patch method for properties

This commit is contained in:
dbroqua 2018-10-06 21:29:20 +02:00
parent 1abd6de68b
commit 91ee5f80f7

View file

@ -29,8 +29,10 @@ class Properties extends Component {
this.getProperties = this.getProperties.bind(this); this.getProperties = this.getProperties.bind(this);
this.addProperty = this.addProperty.bind(this); this.addProperty = this.addProperty.bind(this);
this.patchItem = this.patchItem.bind(this);
this.deleteProperty = this.deleteProperty.bind(this); this.deleteProperty = this.deleteProperty.bind(this);
this.handleChange = this.handleChange.bind(this); this.handleChange = this.handleChange.bind(this);
this.handleArrayChange = this.handleArrayChange.bind(this);
this.state = { this.state = {
Properties: [], Properties: [],
@ -50,6 +52,27 @@ class Properties extends Component {
}); });
} }
handleArrayChange(e) {
const {
id,
value
} = e.target;
let {
Properties
} = this.state;
const targetIndex = Properties.findIndex(datum => {
return Number(datum.id) === Number(id);
});
if (targetIndex !== -1) {
Properties[targetIndex].name = value;
this.setState({
Properties
});
}
}
getProperties() { getProperties() {
API.get('properties') API.get('properties')
.then((res) => { .then((res) => {
@ -85,6 +108,26 @@ class Properties extends Component {
}); });
} }
patchItem(event) {
event.preventDefault(); // Let's stop this event.
event.stopPropagation(); // Really this time.
const {
id,
value
} = event.target;
API.post(`properties/${id}`, {
name: value,
})
.then((res) => {
NotificationManager.success(`Propriété ${value} sauvegardée`);
})
.catch(() => {
NotificationManager.error('Impossile de mettre à jour cette propriété');
});
}
deleteProperty(property) { deleteProperty(property) {
API.delete(`properties/${property.id}`) API.delete(`properties/${property.id}`)
.then((res) => { .then((res) => {
@ -116,7 +159,16 @@ class Properties extends Component {
{this.state.Properties.map((item, key) => ( {this.state.Properties.map((item, key) => (
<tr key={key}> <tr key={key}>
<th scope="row">{item.id}</th> <th scope="row">{item.id}</th>
<td>{item.name}</td> <td>
<Input
type="text"
value={item.name}
id={item.id}
placeholder="Nom de propriété"
onChange={this.handleArrayChange}
onBlur={this.patchItem}
/>
</td>
<td> <td>
{' '} {' '}
<Button color="danger" onClick={() => this.deleteProperty(item)}><FaTrashAlt /></Button> <Button color="danger" onClick={() => this.deleteProperty(item)}><FaTrashAlt /></Button>