File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ .FormErrors {
2+ color : red;
3+ padding-left : 1.2em ;
4+ /* top | right | bottom | left */
5+ margin : 0 0 5px 0 ;
6+ font-size : 0.8em ;
7+ }
Original file line number Diff line number Diff line change 1+ import './FormErrors.css' ;
2+ import React from 'react' ;
3+ const FormErrors = props => {
4+ const { errors = [ ] , forField } = props ;
5+ let filteredErrors = errors ;
6+ if ( forField ) {
7+ filteredErrors = errors . filter (
8+ err => err . field . toLowerCase ( ) === forField . toLowerCase ( ) ,
9+ ) ;
10+ }
11+ return (
12+ < ul className = "FormErrors" >
13+ { filteredErrors . map ( ( error , i ) => (
14+ < li key = { i } >
15+ { error . field } { error . message }
16+ </ li >
17+ ) ) }
18+ </ ul >
19+ ) ;
20+ } ;
21+ export default FormErrors ;
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ class NewProductPage extends Component {
77 super ( props ) ;
88 this . state = {
99 ...initialProduct ,
10+ errors : [ ] ,
1011 } ;
1112 this . updateField = this . updateField . bind ( this ) ;
1213 this . createProduct = this . createProduct . bind ( this ) ;
@@ -22,7 +23,7 @@ class NewProductPage extends Component {
2223
2324 createProduct ( e ) {
2425 e . preventDefault ( ) ;
25- const product = this . state ;
26+ const { errors , ... product } = this . state ;
2627 Session . create ( {
2728 email : 'js@winterfell.gov' ,
2829 password : 'supersecret' ,
@@ -32,8 +33,12 @@ class NewProductPage extends Component {
3233 ...product ,
3334 } ) ;
3435 } )
35- . then ( ( { id } ) => {
36- this . props . history . push ( `/products/${ id } ` ) ;
36+ . then ( ( { id, errors } ) => {
37+ if ( errors ) {
38+ this . setState ( { errors } ) ;
39+ } else {
40+ this . props . history . push ( `/products/${ id } ` ) ;
41+ }
3742 } ) ;
3843 }
3944
Original file line number Diff line number Diff line change 11import React from 'react' ;
2+ import FormErrors from './FormErrors/FormErrors' ;
23
34function ProductForm ( props ) {
45 return (
@@ -7,33 +8,44 @@ function ProductForm(props) {
78 < div >
89 < label htmlFor = "title" > Title</ label >
910 < input
10- onChange = { e => props . onChange ( { title : e . currentTarget . value } ) }
11+ onChange = { e =>
12+ props . onChange ( {
13+ title : e . currentTarget . value ,
14+ } )
15+ }
1116 type = "text"
1217 name = "title"
1318 value = { props . title }
1419 />
20+ < FormErrors forField = "title" errors = { props . errors } />
1521 </ div >
1622 < div >
1723 < label htmlFor = "description" > Description</ label >
1824 < input
1925 onChange = { e =>
20- props . onChange ( { description : e . currentTarget . value } )
26+ props . onChange ( {
27+ description : e . currentTarget . value ,
28+ } )
2129 }
2230 type = "text"
2331 name = "description"
2432 value = { props . description }
2533 />
34+ < FormErrors forField = "description" errors = { props . errors } />
2635 </ div >
2736 < div >
2837 < label htmlFor = "price" > Price</ label >
2938 < input
3039 onChange = { e =>
31- props . onChange ( { price : parseInt ( e . currentTarget . value ) } )
40+ props . onChange ( {
41+ price : parseInt ( e . currentTarget . value ) ,
42+ } )
3243 }
3344 type = "number"
3445 name = "price"
3546 value = { props . price }
3647 />
48+ < FormErrors forField = "price" errors = { props . errors } />
3749 </ div >
3850 < input type = "submit" />
3951 </ form >
You can’t perform that action at this time.
0 commit comments