import React, { Component } from 'react'; import { Container, Row, Col, Form, FormGroup, Button, Label, Input, } from 'reactstrap'; import { FaEdit, FaMapMarkerAlt } from 'react-icons/fa'; import Navigation from './Navigation'; import Header from './Header'; import API from './Api'; import './Vegetable.css'; const Map = { width: 734, height: 530, }; class Vegetable extends Component { constructor(props) { super(props); this.getVegetable = this.getVegetable.bind(this); this.updateVegetable = this.updateVegetable.bind(this); this.handleChange = this.handleChange.bind(this); this.setMap = this.setMap.bind(this); this.onImageChange = this.onImageChange.bind(this); this.state = { Vegetable: { name: '', lat: 0, lng: 0, description: '', }, imagePreviewUrl: '', Map: { width: '20px', height: '20px', }, }; this.getVegetable(props.match.params.categoryId, props.match.params.vegetableId); } componentDidMount() { window.addEventListener('resize', this.setMap); this.setMap(); } componentWillUnmount() { window.removeEventListener('resize', this.setMap); } handleChange(event) { const target = event.target; const value = target.type === 'checkbox' ? target.checked : target.value; const name = target.name; this.setState(prevState => ({ Vegetable: { ...prevState.Vegetable, [name]: value, }, })); } onImageChange(e) { e.preventDefault(); const reader = new FileReader(); const file = e.target.files[0]; reader.onloadend = () => { this.setState(prevState => ({ Vegetable: { ...prevState.Vegetable, mainPictureImported: file, }, imagePreviewUrl: reader.result, })); }; reader.readAsDataURL(file); } updateVegetable(event) { event.preventDefault(); // Let's stop this event. event.stopPropagation(); // Really this time. const fd = new FormData(); // // if (this.state.imagePreviewUrl) { fd.append('mainPicture', this.state.Vegetable.mainPictureImported); } Object.keys(this.state.Vegetable).map((objectKey) => { if (objectKey !== 'mainPicture' && objectKey !== 'mainPictureImported') { fd.append(objectKey, this.state.Vegetable[objectKey]); } return true; }); console.log(this.state.Vegetable); API.patch(`types/${this.state.Vegetable.Type.id}/vegetables/${this.state.Vegetable.id}`, fd) .then((res) => { // this.setState({ Category: res.data }); }) .catch(() => { alert('Impossile de mettre à jour cette catégorie'); }); } getVegetable(categoryId, vegetableId) { API.get(`types/${categoryId}/vegetables/${vegetableId}`) .then((res) => { if (res.status === 200) { const item = res.data; if (item.description === null) { item.description = ' '; } this.setState({ Vegetable: item }); } }) .catch((e) => { alert('Erreur lors de la récupération de ce végétal'); }); } setMap() { const width = document.getElementById('MapContainer').clientWidth; let MapWidth = Map.width; let MapHeight = Map.height; if (width < Map.width) { MapWidth = width; MapHeight = Math.round(Map.height * MapWidth / Map.width); } this.setState({ Map: { width: MapWidth, height: MapHeight, }, }); } render() { const { imagePreviewUrl } = this.state; let $imagePreview = null; if (imagePreviewUrl) { $imagePreview = (Preview); } else if (this.state.Vegetable.mainPicture) { $imagePreview = (Preview); } else { $imagePreview = (
); } return (
this.onImageChange(e)} />
{$imagePreview}
); } } export default Vegetable;