diff --git a/bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx b/bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx index 06462c7..513bd8a 100644 --- a/bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx +++ b/bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx @@ -22,11 +22,17 @@ const styles = { }; class BikeForm extends React.Component { - constructor({ bike }) { + constructor({ bike, editing = false }) { super(); - this.state = { - bike, - }; + if (editing) { + this.state = { + bike, + }; + } else { + this.state = { + bike: {}, + }; + } this.handleChange = this.handleChange.bind(this); this.handleSizeChange = this.handleSizeChange.bind(this); @@ -74,10 +80,11 @@ class BikeForm extends React.Component { const id = this.state.bike.id; const data = JSON.stringify(this.state.bike); const csrfToken = Cookies.get('csrftoken'); + const url = this.props.editing ? `/api/v1/bikes/${id}/` : '/api/v1/bikes/'; - fetch(`/api/v1/bikes/${id}/`, { + fetch(url, { credentials: 'same-origin', - method: 'PUT', + method: this.props.editing ? 'PUT' : 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRFToken': csrfToken, @@ -99,7 +106,8 @@ class BikeForm extends React.Component { cpic_searched_at, created_at, stolen, - } = this.state.bike; + } = this.props.bike; + const editing = this.props.editing; const createdAtFormatted = (moment(created_at).isValid()) ? moment(created_at).tz(timezone).fromNow() : ''; const claimedAtFormatted = (moment(claimed_at).isValid()) ? moment(claimed_at).tz(timezone).fromNow() : ''; const cpicSearchedAtFormatted = (moment(cpic_searched_at).isValid()) ? moment(cpic_searched_at).tz(timezone) @@ -159,53 +167,60 @@ class BikeForm extends React.Component { required /> -