import axios from 'axios' import React from 'react' import '../../css/Components/createarticle.css' import plusIcon from '@iconify/icons-fa-solid/plus'; import { Icon} from '@iconify/react'; import CreateCategory from './CreateCategory'; import Tooltip from '@material-ui/core/Tooltip'; import Zoom from '@material-ui/core/Zoom'; class CreateArticle extends React.Component { constructor(){ super() this.state = { name: '', description:'', quantity : '', price : '', code : '', image: '', category_id:'', errors : [], categories : [], categoryName:'', onNewCategory:false, alreadyDone:false } } componentDidMount() { axios.get('https://etud.insa-toulouse.fr/~bamarty/api_proximo/public/api/categories') .then(res => { this.setState({categories : res.data}, () => { console.log(this.state.categories) this.setState({categoryName:res.data[0].name}) }) }) .catch(error => { console.log(error.response) }) } handleNameChange = event => { this.setState({name : event.target.value},() => { console.log(this.state.name) }) } handleCategoryChange = event => { this.setState({category_id : event.target.value},() => { console.log(this.state.category_id); }) } handleDescriptionChange = event => { this.setState({description : event.target.value},() => { console.log(this.state.description) }) } handleQuantityChange = event => { this.setState({quantity : event.target.value},() => { console.log(this.state.quantity) }) } handlePriceChange = event => { this.setState({price : event.target.value},() => { console.log(this.state.price) }) } handleCodeChange = event => { this.setState({code : event.target.value},() => { console.log(this.state.code) }) } handleImageChange = event => { this.setState({image : event.target.files[0]},() => { console.log(this.state.image) }) } handleSubmit = event => { event.preventDefault() let bodyFormData = new FormData() bodyFormData.set('name',this.state.name) bodyFormData.set('description',this.state.description) bodyFormData.set('quantity',this.state.quantity) bodyFormData.set('price',this.state.price) bodyFormData.set('code',this.state.code) bodyFormData.set('image',this.state.image) bodyFormData.set('category_id',this.state.category_id) axios.post('https://etud.insa-toulouse.fr/~bamarty/api_proximo/public/api/articles/store',bodyFormData) .then(res => { console.log(res) window.location.reload() this.props.handleCloseAbove(); }) .catch(error => { if(error.response.status === 401){ this.setState({errors : error.response.data.errors}) } console.log(error.response.data.errors) }); } handleNewCategory = () => { this.setState({onNewCategory:true}) } handleCloseNewCategory = () => { this.setState({onNewCategory:false}) } reload = () => { if (!this.state.alreadyDone){ axios.get('https://etud.insa-toulouse.fr/~bamarty/api_proximo/public/api/categories') .then(res => { this.setState({categories : res.data}, () => { console.log(this.state.categories) this.setState({categoryName:res.data[0].name}) this.setState({alreadyDone:true}) }) }) .catch(error => { console.log(error.response) }) } } render(){ if (this.state.onNewCategory){ return } else { return(
Nouvel Article

{!this.state.name && this.state.errors.name}

{/* Affichage de l'icone correspondant à la catégorie
eslint-disable-next-line {this.state.categories.map((categorie)=> {if (this.state.category_id === categorie.id){ return bonjour }} )}
*/}

{!this.state.category_id && this.state.errors.category_id}