bo/src/Category.js

204 lines
5.2 KiB
JavaScript
Raw Normal View History

2018-10-06 17:22:20 +02:00
import React, {
Component
} from 'react';
2018-09-09 12:06:10 +02:00
import {
Container,
Table,
Form,
FormGroup,
Button,
Input,
} from 'reactstrap';
2018-10-06 17:22:20 +02:00
import {
FaTrashAlt,
FaPlus,
FaEdit
} from 'react-icons/fa';
import {
Link
} from 'react-router-dom';
import {
NotificationContainer,
NotificationManager
} from 'react-notifications';
2018-09-09 23:11:27 +02:00
import Navigation from './Navigation';
2018-09-09 12:06:10 +02:00
import Header from './Header';
import API from './Api';
2018-10-06 17:22:20 +02:00
import 'react-notifications/lib/notifications.css';
2018-09-09 12:06:10 +02:00
class Category extends Component {
constructor(props) {
super(props);
this.updateCategory = this.updateCategory.bind(this);
this.getVegetables = this.getVegetables.bind(this);
this.deleteVegetable = this.deleteVegetable.bind(this);
this.addVegetable = this.addVegetable.bind(this);
this.handleChange = this.handleChange.bind(this);
this.state = {
Category: {},
name: '',
categoryId: props.match.params.categoryId,
categoryName: '',
};
this.getVegetables(props.match.params.categoryId);
}
handleChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value,
});
}
updateCategory(event) {
event.preventDefault(); // Let's stop this event.
event.stopPropagation(); // Really this time.
API.patch(`types/${this.state.Category.id}`, {
2018-10-06 17:22:20 +02:00
name: this.state.categoryName,
})
2018-09-09 12:06:10 +02:00
.then((res) => {
2018-10-06 17:22:20 +02:00
this.setState({
Category: res.data
});
2018-09-09 12:06:10 +02:00
})
.catch(() => {
2018-10-06 17:22:20 +02:00
NotificationManager.error('Impossile de mettre à jour cette catégorie');
2018-09-09 12:06:10 +02:00
});
}
getVegetables(categoryId) {
API.get(`types/${categoryId}`)
.then((res) => {
if (res.status === 200) {
if (res.data.Vegetables === undefined) {
res.data.Vegetables = [];
}
2018-10-06 17:22:20 +02:00
this.setState({
Category: res.data,
categoryName: res.data.name
});
2018-09-09 12:06:10 +02:00
} else {
2018-10-06 17:22:20 +02:00
this.setState({
Category: {}
});
2018-09-09 12:06:10 +02:00
}
})
.catch((e) => {
2018-10-06 17:22:20 +02:00
NotificationManager.error('Erreur lors de la récupération des catégories');
2018-09-09 12:06:10 +02:00
});
}
addVegetable(event) {
event.preventDefault(); // Let's stop this event.
event.stopPropagation(); // Really this time.
API.post(`types/${this.state.Category.id}/vegetables/`, {
2018-10-06 17:22:20 +02:00
name: this.state.name,
lat: 0,
lng: 0,
description: '',
})
2018-09-09 12:06:10 +02:00
.then((res) => {
const Category = this.state.Category;
Category.Vegetables.push(res.data);
2018-10-06 17:22:20 +02:00
this.setState({
Category
});
2018-09-09 12:06:10 +02:00
})
.catch(() => {
2018-10-06 17:22:20 +02:00
NotificationManager.error('Impossile de créer cette catégorie');
2018-09-09 12:06:10 +02:00
});
}
deleteVegetable(vegetable, key) {
API.delete(`types/${this.state.Category.id}/vegetables/${vegetable.id}`)
.then((res) => {
const Category = this.state.Category;
Category.Vegetables.splice(key, 1);
2018-10-06 17:22:20 +02:00
this.setState({
Category
});
2018-09-09 12:06:10 +02:00
})
.catch((e) => {
2018-10-06 17:22:20 +02:00
NotificationManager.error('Erreur lors de la suppression de ce végétal');
2018-09-09 12:06:10 +02:00
});
}
render() {
return (
2018-09-09 23:11:27 +02:00
<div>
2018-10-06 17:22:20 +02:00
<NotificationContainer/>
2018-09-09 23:11:27 +02:00
<Header />
<Container>
<Navigation categoryId={this.state.categoryId} categoryName={this.state.Category.name} />
<Form inline onSubmit={this.updateCategory} style={{ marginBottom: '16px' }}>
<FormGroup className="mb-8 mr-sm-8 mb-sm-0">
<Input
type="text"
name="categoryName"
value={this.state.categoryName}
placeholder="Changez le nom de la catégorie"
onChange={this.handleChange}
/>
</FormGroup>
<Button color="primary">
<FaEdit />
{' '}
Mettre à jour
</Button>
</Form>
<Table>
<thead>
<tr>
<th>#</th>
<th>Nom</th>
<th>Action</th>
2018-09-09 12:06:10 +02:00
</tr>
2018-09-09 23:11:27 +02:00
</thead>
<tbody>
{this.state.Category.Vegetables && this.state.Category.Vegetables.map((item, key) => (
<tr key={key}>
<th scope="row"><Link to={`/categories/${this.state.Category.id}/vegetables/${item.id}`}>{item.id}</Link></th>
<td>{item.name}</td>
<td>
{' '}
<Button color="danger" onClick={() => this.deleteVegetable(item, key)}><FaTrashAlt /></Button>
</td>
</tr>
))}
</tbody>
</Table>
<Form inline onSubmit={this.addVegetable}>
<FormGroup className="mb-8 mr-sm-8 mb-sm-0">
<Input
type="text"
name="name"
placeholder="Nouveau végétal"
onChange={this.handleChange}
/>
</FormGroup>
<Button color="primary">
<FaPlus />
{' '}
Ajouter
</Button>
</Form>
</Container>
</div>
2018-09-09 12:06:10 +02:00
);
}
}
export default Category;