diff --git a/src/Vegetable.js b/src/Vegetable.js index b1ca544..d216ebc 100644 --- a/src/Vegetable.js +++ b/src/Vegetable.js @@ -3,40 +3,27 @@ import React, { } 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 VegetableMain from './Vegetable/Main' +import VegetableCarousel from './Vegetable/Carousel' +import VegetableMap from './Vegetable/Map' import './Vegetable.css'; -const Map = { - width: 734, - height: 530, -}; - class Vegetable extends Component { constructor(props) { super(props); @@ -51,27 +38,17 @@ class Vegetable extends Component { }, imagePreviewUrl: '', imagesPreviewUrls: [], - Map: { - width: '20px', - height: '20px', - }, activeTab: '1', - activeIndex: 0, + availableProperties: [] }; 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.changeMainPicture = this.changeMainPicture.bind(this); + this.addPicture = this.addPicture.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.updateValueFromChild = this.updateValueFromChild.bind(this); this.getVegetable(props.match.params.categoryId, props.match.params.vegetableId); } @@ -80,26 +57,11 @@ class Vegetable extends Component { 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; - + updateValueFromChild(name, value) { this.setState(prevState => ({ Vegetable: { ...prevState.Vegetable, @@ -108,51 +70,33 @@ class Vegetable extends Component { })); } - 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); + changeMainPicture(file, reader) { + this.setState(prevState => ({ + Vegetable: { + ...prevState.Vegetable, + mainPictureImported: file, + }, + imagePreviewUrl: reader.result, + })); } - 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 - })); - }; + addPicture(file, reader) { + 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) { @@ -194,6 +138,19 @@ class Vegetable extends Component { } getVegetable(categoryId, vegetableId) { + API.get('properties') + .then(res => { + //availableProperties + if (res.status === 200) { + this.setState({ + availableProperties: res.data.rows + }); + } + }) + .catch(e => { + alert('Erreur lors de la récupération des types de propriétés'); + }); + API.get(`types/${categoryId}/vegetables/${vegetableId}`) .then((res) => { if (res.status === 200) { @@ -222,71 +179,7 @@ class Vegetable extends Component { }); } - 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 (
@@ -321,143 +214,25 @@ class Vegetable extends Component { - - - - - - - - - - - - - - - - - - - this.onImageChange(e)} - /> - - - - -
- {$imagePreview} -
- -
+
- - - - - - - - - - - - - - - - -
-
-
-
-
- -
+
- - - - - - this.onPictureChange(e)} - /> - - - - - {this.state.imagesPreviewUrls - && this.state.imagesPreviewUrls.length > 0 - ? ( - - {this.state.imagesPreviewUrls.map( (item, key) => ( - - Carousel - - ))} - - - ) - : (null) - } - - +
diff --git a/src/Vegetable/Carousel.js b/src/Vegetable/Carousel.js new file mode 100644 index 0000000..773a263 --- /dev/null +++ b/src/Vegetable/Carousel.js @@ -0,0 +1,145 @@ +import React, { + Component, +} from 'react'; +import { + Row, + Col, + FormGroup, + Label, + Input, + Carousel, + CarouselItem, + CarouselControl, + CarouselIndicators, +} from 'reactstrap'; +import { + FaFileImage, +} from 'react-icons/fa'; + +class VegetableCarousel extends Component { + constructor(props) { + super(props); + + this.state = { + imagesPreviewUrls: this.props.imagesPreviewUrls, + activeIndex: 0 + }; + + this.onPictureChange = this.onPictureChange.bind(this); + this.next = this.next.bind(this); + this.previous = this.previous.bind(this); + this.goToIndex = this.goToIndex.bind(this); + this.onExited = this.onExited.bind(this); + this.onExiting = this.onExiting.bind(this); + } + + componentWillReceiveProps(nextProps) { + if ( nextProps.imagesPreviewUrls) { + this.setState({ + imagesPreviewUrls: nextProps.imagesPreviewUrls + }); + } + if ( nextProps.activeIndex) { + this.setState({ + activeIndex: nextProps.activeIndex + }); + } + } + + onPictureChange(e) { + e.preventDefault(); + + const reader = new FileReader(); + const file = e.target.files[0]; + + reader.onloadend = () => { + this.props.addPicture(file, reader); + }; + + reader.readAsDataURL(file); + } + + 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, + }); + } + + onExiting() { + this.animating = true; + } + + onExited() { + this.animating = false; + } + + render() { + const { + activeIndex, + } = this.state; + + return ( + + + + + + this.onPictureChange(e)} + /> + + + + + {this.state.imagesPreviewUrls + && this.state.imagesPreviewUrls.length > 0 + ? ( + + {this.state.imagesPreviewUrls.map( (item, key) => ( + + Carousel + + ))} + + + ) + : (null) + } + + + ) + } +} + +export default VegetableCarousel; diff --git a/src/Vegetable/Main.js b/src/Vegetable/Main.js new file mode 100644 index 0000000..5e3326e --- /dev/null +++ b/src/Vegetable/Main.js @@ -0,0 +1,142 @@ +import React, { + Component, +} from 'react'; +import { + Row, + Col, + FormGroup, + Label, + Input, +} from 'reactstrap'; +import { + FaFileImage, +} from 'react-icons/fa'; + +class VegetableMain extends Component { + constructor(props) { + super(props); + + this.state = { + Vegetable: { + name: '', + lat: 0, + lng: 0, + description: '', + Pictures: [], + }, + imagePreviewUrl: '', + }; + + this.handleChange = this.handleChange.bind(this); + } + + componentWillReceiveProps(nextProps) { + console.log(nextProps) + if (nextProps.Vegetable) { + this.setState({ + Vegetable: nextProps.Vegetable + }); + } + if (nextProps.imagePreviewUrl) { + this.setState({ + imagePreviewUrl: nextProps.imagePreviewUrl + }); + } + } + + 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, + }, + })); + + this.props.updateValue(name, value) + } + + onImageChange(e) { + e.preventDefault(); + + const reader = new FileReader(); + const file = e.target.files[0]; + + reader.onloadend = () => { + this.props.changeMainPicture(file, reader); + }; + + reader.readAsDataURL(file); + } + + 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 VegetableMain; diff --git a/src/Vegetable/Map.js b/src/Vegetable/Map.js new file mode 100644 index 0000000..8514760 --- /dev/null +++ b/src/Vegetable/Map.js @@ -0,0 +1,145 @@ +import React, { + Component, +} from 'react'; +import { + Row, + Col, + FormGroup, + Label, + Input, +} from 'reactstrap'; +import { + FaMapMarkerAlt +} from 'react-icons/fa'; + +const Map = { + width: 734, + height: 530, +}; + +class VegetableMap extends Component { + constructor(props) { + super(props); + + this.state = { + Vegetable: { + name: '', + lat: 0, + lng: 0, + description: '', + Pictures: [], + }, + Map: { + width: '20px', + height: '20px', + }, + }; + + this.setMap = this.setMap.bind(this); + this.handleChange = this.handleChange.bind(this); + } + + componentWillReceiveProps(nextProps) { + if (nextProps.Vegetable) { + this.setState({ + Vegetable: nextProps.Vegetable + }); + } + + setTimeout( () => { + this.setMap(); + }, 200); + } + + 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, + }, + })); + + this.props.updateValue(name, value) + } + + 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() { + return ( + + + + + + + + + + + + + + + + +
+
+
+
+
+ +
+ ) + } +} + +export default VegetableMap