import React, { Component } from 'react'
import queryString from 'query-string'
import { Redirect, Link } from 'react-router-dom'
import api from '../api-proxy'
document.title = 'Update Item'
const ItemUpdate = (props) => (
)
class ItemUpdateForm extends Component {
constructor(props) {
super(props)
this.fileInput = React.createRef()
let params = queryString.parse(this.props.urlParam)
this.state = {
_id: params._id,
itemName: '',
description: '',
unitPrice: '',
categoryName: undefined,
categories: [],
returnToMenu: false
}
}
componentWillMount() {
fetch(api.url + '/categories')
.then((response) => response.json())
.then((categories) => {
this.setState({ categories: categories })
fetch(api.url + '/item/' + this.state._id)
.then((response) => response.json())
.then((item) => {
this.setState({
itemName: item.name,
description: item.description,
unitPrice: item.unitPrice,
categoryName: item.categoryName
})
})
})
}
itemNameChangeHandler(e) {
this.setState({ itemName: e.target.value })
}
descriptionChangeHandler(e) {
this.setState({ description: e.target.value })
}
unitPriceChangeHandler(e) {
this.setState({ unitPrice: e.target.value })
}
categoryNameChangeHandler(e) {
this.setState({ categoryName: e.target.value })
}
formSubmitHandler(e) {
e.preventDefault()
let payload = {
method: 'put',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
_id: this.state._id,
name: this.state.itemName,
description: this.state.description,
unitPrice: this.state.unitPrice,
categoryName: this.state.categoryName,
isArchived: 0
})
}
fetch(api.url + '/item', payload)
.then((response) => response.json())
.then((response) => {
if (response.error != null) {
alert(response.error)
} else {
this.setState({ returnToMenu: true })
}
})
}
render() {
if (this.state.returnToMenu) {
return
}
return (
)
}
}
export default ItemUpdate