import React, { Component } from 'react'; import { Container, Table, Form, FormGroup, Button, Input, } from 'reactstrap'; import { FaTrashAlt, FaPlus, FaEdit } from 'react-icons/fa'; import { Link } from 'react-router-dom'; import { NotificationContainer, NotificationManager } from 'react-notifications'; import Navigation from './Navigation'; import Header from './Header'; import API from './Api'; import 'react-notifications/lib/notifications.css'; 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}`, { name: this.state.categoryName, }) .then((res) => { this.setState({ Category: res.data }); }) .catch(() => { NotificationManager.error('Impossile de mettre à jour cette catégorie'); }); } getVegetables(categoryId) { API.get(`types/${categoryId}`) .then((res) => { if (res.status === 200) { if (res.data.Vegetables === undefined) { res.data.Vegetables = []; } this.setState({ Category: res.data, categoryName: res.data.name }); } else { this.setState({ Category: {} }); } }) .catch((e) => { NotificationManager.error('Erreur lors de la récupération des catégories'); }); } addVegetable(event) { event.preventDefault(); // Let's stop this event. event.stopPropagation(); // Really this time. API.post(`types/${this.state.Category.id}/vegetables/`, { name: this.state.name, lat: 0, lng: 0, description: '', }) .then((res) => { const Category = this.state.Category; Category.Vegetables.push(res.data); this.setState({ Category }); }) .catch(() => { NotificationManager.error('Impossile de créer cette catégorie'); }); } 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); this.setState({ Category }); }) .catch((e) => { NotificationManager.error('Erreur lors de la suppression de ce végétal'); }); } render() { return (
{this.state.Category.Vegetables && this.state.Category.Vegetables.map((item, key) => ( ))}
# Nom Action
{item.id} {item.name} {' '}
); } } export default Category;