From 38b2b8c0a3d88986a8fb4e367afe7180434178ed Mon Sep 17 00:00:00 2001 From: Drew Larson Date: Wed, 1 Feb 2017 22:12:26 -0600 Subject: [PATCH] Support for adding a new bike. --- .../js/bikes/components/BikeForm/index.jsx | 141 ++++++++++-------- .../js/bikes/components/BikeModal/index.jsx | 15 +- .../js/bikes/components/BikeTable/index.jsx | 26 +++- 3 files changed, 113 insertions(+), 69 deletions(-) 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 /> -
- -
- -
-
- -
-
- -
+ {editing && +
+ +
+ }
-
-
- -
-
- + {editing && +
+
+ +
+
+ +
-
- + } + + {editing && +
+
+ +
+
+ +
+
+ +
-
+ }
-
-
- + {editing && +
+
+ +
-
+ }
diff --git a/bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx b/bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx index ad669ab..b7c49a6 100644 --- a/bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx +++ b/bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx @@ -9,6 +9,7 @@ import BikeForm from '../BikeForm'; export default class BikeModal extends React.Component { static propTypes = { open: PropTypes.bool, + editing: PropTypes.bool, } constructor(props) { @@ -17,11 +18,17 @@ export default class BikeModal extends React.Component { this.state = { open: props.open, bike: undefined, + editing: props.editing, }; } - componentWillReceiveProps(newProps) { - this.setState({ open: newProps.open || false, bike: newProps.bike || false }); + componentWillReceiveProps = (newProps) => { + this.setState({ + ...this.state, + open: newProps.open || false, + bike: newProps.bike || undefined, + editing: newProps.editing || false, + }); } handleClose = () => { @@ -45,7 +52,7 @@ export default class BikeModal extends React.Component { const title = this.state.bike && this.state.bike.stolen ? (
-

Edit Bike

+

{this.props.editing ? 'Edit Bike' : 'Add Bike'}

STOLEN

) :

Edit Bike

; @@ -58,7 +65,7 @@ export default class BikeModal extends React.Component { open={this.state.open} autoScrollBodyContent > - { this.state.bike ? :
Unable to edit bike.
} + { this.state.bike ? :
Unable to edit bike.
}
); } diff --git a/bikeshop_project/assets/js/bikes/components/BikeTable/index.jsx b/bikeshop_project/assets/js/bikes/components/BikeTable/index.jsx index ad9eaa0..6c812e6 100644 --- a/bikeshop_project/assets/js/bikes/components/BikeTable/index.jsx +++ b/bikeshop_project/assets/js/bikes/components/BikeTable/index.jsx @@ -2,6 +2,8 @@ import { Table, TableBody, TableHeader, TableHeaderColumn, TableRow, TableRowCol import Cookies from 'js-cookie'; import fetch from 'isomorphic-fetch'; import FlatButton from 'material-ui/FlatButton'; +import FloatingActionButton from 'material-ui/FloatingActionButton'; +import ContentAdd from 'material-ui/svg-icons/content/add'; import React from 'react'; import { friendlySize } from '../Size'; import BikeModal from '../BikeModal'; @@ -28,6 +30,7 @@ export default class BikeTable extends React.Component { bikeModal: { open: false, bike: undefined, + editing: false, }, }; @@ -35,7 +38,6 @@ export default class BikeTable extends React.Component { } componentDidMount() { - fetch('/api/v1/bikes/', { credentials: 'same-origin', }) @@ -51,12 +53,23 @@ export default class BikeTable extends React.Component { } handleEditBike(bike) { - console.log('Bike edit!'); this.setState({ ...this.state, bikeModal: { open: true, bike, + editing: true, + }, + }); + } + + handleAddBike = () => { + this.setState({ + ...this.state, + bikeModal: { + open: true, + bike: {}, + editing: false, }, }); } @@ -78,6 +91,9 @@ export default class BikeTable extends React.Component {

Bikes

+ + + @@ -99,7 +115,11 @@ export default class BikeTable extends React.Component { }
- +
);