import React, { Component, } from 'react'; import { Container, Row, Col, Form, FormGroup, Button, Label, Input, TabContent, TabPane, Nav, NavItem, NavLink, Carousel, CarouselItem, CarouselControl, CarouselIndicators, } from 'reactstrap'; import classnames from 'classnames'; import { FaEdit, FaMapMarkerAlt, FaFileImage, } 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.state = { Vegetable: { name: '', lat: 0, lng: 0, description: '', Pictures: [], }, imagePreviewUrl: '', imagesPreviewUrls: [], Map: { width: '20px', height: '20px', }, activeTab: '1', activeIndex: 0, }; 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.onPictureChange = this.onPictureChange.bind(this); this.toggle = this.toggle.bind(this); this.next = this.next.bind(this); this.previous = this.previous.bind(this); this.goToIndex = this.goToIndex.bind(this); this.onExiting = this.onExiting.bind(this); this.onExited = this.onExited.bind(this); this.postPicture = this.postPicture.bind(this); this.getVegetable(props.match.params.categoryId, props.match.params.vegetableId); } toggle(tab) { if (this.state.activeTab !== tab) { this.setState({ activeTab: tab, }, () => { this.setMap(); }); } } 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); } onPictureChange(e) { e.preventDefault(); const reader = new FileReader(); const file = e.target.files[0]; reader.onloadend = () => { this.setState(prevState => ({ Vegetable: { ...prevState.Vegetable, Pictures: [ ...prevState.Vegetable.Pictures, file ] }, imagesPreviewUrls: [ ...prevState.imagesPreviewUrls, reader.result ], activeIndex: prevState.imagesPreviewUrls.length })); }; this.postPicture(file); 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; }); 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 ce végétal'); }); } postPicture(picture) { const fd = new FormData(); fd.append('picture', picture); API.post(`types/${this.state.Vegetable.Type.id}/vegetables/${this.state.Vegetable.id}/pictures`, fd) .then((res) => { // this.setState({ Category: res.data }); }) .catch(() => { alert('Impossile d\'ajouter cette image'); }); } getVegetable(categoryId, vegetableId) { API.get(`types/${categoryId}/vegetables/${vegetableId}`) .then((res) => { if (res.status === 200) { const item = res.data; let images = []; if (item.description === null) { item.description = ' '; } if (item.Pictures.length > 0) { for (let i = 0; i < item.Pictures.length; i += 1) { images.push(item.Pictures[i].url); } } this.setState(prevState => ({ ...prevState, Vegetable: item, imagesPreviewUrls: images })); } }) .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, }, }); } onExiting() { this.animating = true; } onExited() { this.animating = false; } next() { if (this.animating) return; const nextIndex = this.state.activeIndex === this.state.imagesPreviewUrls.length - 1 ? 0 : this.state.activeIndex + 1; this.setState({ activeIndex: nextIndex, }); } previous() { if (this.animating) return; const nextIndex = this.state.activeIndex === 0 ? this.state.imagesPreviewUrls.length - 1 : this.state.activeIndex - 1; this.setState({ activeIndex: nextIndex, }); } goToIndex(newIndex) { if (this.animating) return; this.setState({ activeIndex: newIndex, }); } render() { const { activeIndex, } = this.state; 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}
this.onPictureChange(e)} /> {this.state.imagesPreviewUrls && this.state.imagesPreviewUrls.length > 0 ? ( {this.state.imagesPreviewUrls.map( (item, key) => ( Carousel ))} ) : (null) }
); } } export default Vegetable;