From 6aa7614a8bdbd584d087f8f78cb7f44e446009f8 Mon Sep 17 00:00:00 2001 From: Drew Larson Date: Sun, 29 Jan 2017 14:52:13 -0600 Subject: [PATCH] Refactor form. --- .../js/bikes/components/BikeForm/index.jsx | 190 ++++++++++++++++++ .../js/bikes/components/BikeModal/index.jsx | 116 +---------- .../assets/js/bikes/components/Size/index.jsx | 10 +- .../js/bikes/components/Source/index.jsx | 5 +- 4 files changed, 205 insertions(+), 116 deletions(-) create mode 100644 bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx diff --git a/bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx b/bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx new file mode 100644 index 0000000..781d64a --- /dev/null +++ b/bikeshop_project/assets/js/bikes/components/BikeForm/index.jsx @@ -0,0 +1,190 @@ +import Checkbox from 'material-ui/Checkbox'; +import FlatButton from 'material-ui/FlatButton'; +import moment from 'moment-timezone'; +import React from 'react'; +import TextField from 'material-ui/TextField'; +import Source from '../Source'; +import Size from '../Size'; + +const styles = { + block: { + maxWidth: 250, + }, + checkbox: { + marginBottom: 16, + }, + bottom: { + alignItems: 'flex-end', + }, +}; + +class BikeForm extends React.Component { + constructor({ bike }) { + super(); + this.state = { + bike, + }; + + this.handleChange = this.handleChange.bind(this); + this.handleSizeChange = this.handleSizeChange.bind(this); + this.handleSourceChange = this.handleSourceChange.bind(this); + } + + handleChange(event, value) { + this.setState({ bike: { ...this.state.bike, [event.target.name]: value } }); + } + + handleSizeChange(event, index, value) { + this.setState({ bike: { ...this.state.bike, size: value } }); + } + + handleSourceChange(event, index, value) { + this.setState({ bike: { ...this.state.bike, source: value } }); + } + + render() { + const timezone = moment.tz.guess(); + const { + price, + claimed_at, + claimed_by, + colour, + cpic_searched_at, + created_at, + size, + serial_number, + source, + stolen, + stripped, + } = this.state.bike; + 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) + .fromNow() : ''; + + return ( +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+
+ +
+
+
+
+ ); + } +} + +export default BikeForm; diff --git a/bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx b/bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx index 31ca3f6..7383f34 100644 --- a/bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx +++ b/bikeshop_project/assets/js/bikes/components/BikeModal/index.jsx @@ -1,12 +1,7 @@ -import Checkbox from 'material-ui/Checkbox'; +import React, { PropTypes } from 'react'; import Dialog from 'material-ui/Dialog'; import FlatButton from 'material-ui/FlatButton'; -import moment from 'moment-timezone'; -import React, { PropTypes } from 'react'; -import TextField from 'material-ui/TextField'; - -import Source from '../Source'; -import Size from '../Size'; +import BikeForm from '../BikeForm'; /** * A modal dialog can only be closed by selecting one of the actions. @@ -24,26 +19,16 @@ export default class BikeModal extends React.Component { bike: undefined, }; } + componentWillReceiveProps(newProps) { this.setState({ open: newProps.open || false, bike: newProps.bike || false }); } + handleClose = () => { this.setState({ open: false }); }; render() { - const styles = { - block: { - maxWidth: 250, - }, - checkbox: { - marginBottom: 16, - }, - bottom: { - alignItems: 'flex-end', - }, - }; - const actions = [ , ]; - let form; - - - if (this.state.bike !== undefined) { - const timezone = moment.tz.guess(); - const { - make, - price, - claimed_at, - claimed_by, - colour, - cpic_searched_at, - created_at, - size, - serial_number, - source, - stolen, - stripped } = this.state.bike; - 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) - .fromNow() : ''; - - form = ( -
-
-
- -
-
- -
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
- -
-
-
-
- -
-
-
- -
-
-
-
- ); - } - return (
- {form ||
Unable to edit bike.
} - + { this.state.bike ? :
Unable to edit bike.
}
); } diff --git a/bikeshop_project/assets/js/bikes/components/Size/index.jsx b/bikeshop_project/assets/js/bikes/components/Size/index.jsx index 80aaa90..5ec5ce9 100644 --- a/bikeshop_project/assets/js/bikes/components/Size/index.jsx +++ b/bikeshop_project/assets/js/bikes/components/Size/index.jsx @@ -22,20 +22,21 @@ export const friendlySize = (size) => { }; const styles = { - float: 'left' + float: 'left', } -const Size = ({ size }) => { +const Size = ({ size, onChange }) => { const items = sizes.map(s => - , + , ); return (
@@ -47,6 +48,7 @@ const Size = ({ size }) => { Size.propTypes = { size: PropTypes.string, + onChange: PropTypes.function, }; export default Size; diff --git a/bikeshop_project/assets/js/bikes/components/Source/index.jsx b/bikeshop_project/assets/js/bikes/components/Source/index.jsx index 2bfb130..594c7a0 100644 --- a/bikeshop_project/assets/js/bikes/components/Source/index.jsx +++ b/bikeshop_project/assets/js/bikes/components/Source/index.jsx @@ -17,7 +17,7 @@ export const friendly = (s) => { } }; -const Source = ({ source }) => { +const Source = ({ source, onChange }) => { const items = sources.map(s => , ); @@ -27,7 +27,7 @@ const Source = ({ source }) => { @@ -39,6 +39,7 @@ const Source = ({ source }) => { Source.propTypes = { source: PropTypes.string, + onChange: PropTypes.function }; export default Source;