@@ -10,134 +10,134 @@ import API from "./api";
10
10
import PropTypes from 'prop-types' ;
11
11
12
12
const productPropTypes = {
13
- product : PropTypes . shape ( {
14
- id : PropTypes . string . isRequired ,
15
- name : PropTypes . string . isRequired ,
16
- type : PropTypes . string . isRequired ,
17
- } ) . isRequired
13
+ product : PropTypes . shape ( {
14
+ id : PropTypes . string . isRequired ,
15
+ name : PropTypes . string . isRequired ,
16
+ type : PropTypes . string . isRequired ,
17
+ } ) . isRequired
18
18
} ;
19
19
20
20
function ProductTableRow ( props ) {
21
- return (
22
- < tr >
23
- < td > { props . product . name } </ td >
24
- < td > { props . product . type } </ td >
25
- < td >
26
- < Link className = "btn btn-link" to = { {
27
- pathname : "/products/" + props . product . id ,
28
- state : {
29
- product : props . product
30
- }
31
- } } > See more!</ Link >
32
- </ td >
33
- </ tr >
34
- ) ;
21
+ return (
22
+ < tr >
23
+ < td > { props . product . name } </ td >
24
+ < td > { props . product . type } </ td >
25
+ < td >
26
+ < Link className = "btn btn-link" to = { {
27
+ pathname : "/products/" + props . product . id ,
28
+ state : {
29
+ product : props . product
30
+ }
31
+ } } > See more!</ Link >
32
+ </ td >
33
+ </ tr >
34
+ ) ;
35
35
}
36
36
ProductTableRow . propTypes = productPropTypes ;
37
37
38
38
function ProductTable ( props ) {
39
- const products = props . products . map ( p => (
40
- < ProductTableRow key = { p . id } product = { p } />
41
- ) ) ;
42
- return (
43
- < table className = "table table-striped table-hover" >
44
- < thead >
45
- < tr >
46
- < th > Name</ th >
47
- < th > Type</ th >
48
- < th />
49
- </ tr >
50
- </ thead >
51
- < tbody >
52
- { products }
53
- </ tbody >
54
- </ table >
55
- ) ;
39
+ const products = props . products . map ( p => (
40
+ < ProductTableRow key = { p . id } product = { p } />
41
+ ) ) ;
42
+ return (
43
+ < table className = "table table-striped table-hover" >
44
+ < thead >
45
+ < tr >
46
+ < th > Name</ th >
47
+ < th > Type</ th >
48
+ < th />
49
+ </ tr >
50
+ </ thead >
51
+ < tbody >
52
+ { products }
53
+ </ tbody >
54
+ </ table >
55
+ ) ;
56
56
}
57
57
58
58
ProductTable . propTypes = {
59
- products : PropTypes . arrayOf ( productPropTypes . product )
59
+ products : PropTypes . arrayOf ( productPropTypes . product )
60
60
} ;
61
61
62
62
class App extends React . Component {
63
- constructor ( props ) {
64
- super ( props ) ;
65
-
66
- this . state = {
67
- loading : true ,
68
- searchText : '' ,
69
- products : [ ] ,
70
- visibleProducts : [ ]
71
- } ;
72
- this . onSearchTextChange = this . onSearchTextChange . bind ( this ) ;
73
- }
74
-
75
- componentDidMount ( ) {
76
- API . getAllProducts ( )
77
- . then ( r => {
78
- this . setState ( {
79
- loading : false ,
80
- products : r
81
- } ) ;
82
- this . determineVisibleProducts ( ) ;
83
- } )
84
- . catch ( e => {
85
- this . props . history . push ( {
86
- pathname : "/error" ,
87
- state : {
88
- error : e . toString ( )
89
- }
90
- } ) ;
91
- } ) ;
92
- }
63
+ constructor ( props ) {
64
+ super ( props ) ;
93
65
94
- determineVisibleProducts ( ) {
95
- const findProducts = ( search ) => {
96
- search = search . toLowerCase ( ) ;
97
- return this . state . products . filter ( p =>
98
- p . id . toLowerCase ( ) . includes ( search )
99
- || p . name . toLowerCase ( ) . includes ( search )
100
- || p . type . toLowerCase ( ) . includes ( search )
101
- )
102
- } ;
103
- this . setState ( ( s ) => {
104
- return {
105
- visibleProducts : s . searchText ? findProducts ( s . searchText ) : s . products
106
- }
107
- } ) ;
108
- }
66
+ this . state = {
67
+ loading : true ,
68
+ searchText : '' ,
69
+ products : [ ] ,
70
+ visibleProducts : [ ]
71
+ } ;
72
+ this . onSearchTextChange = this . onSearchTextChange . bind ( this ) ;
73
+ }
109
74
110
- onSearchTextChange ( e ) {
75
+ componentDidMount ( ) {
76
+ API . getAllProducts ( )
77
+ . then ( r => {
111
78
this . setState ( {
112
- searchText : e . target . value
79
+ loading : false ,
80
+ products : r
113
81
} ) ;
114
- this . determineVisibleProducts ( )
115
- }
82
+ this . determineVisibleProducts ( ) ;
83
+ } )
84
+ . catch ( e => {
85
+ this . props . history . push ( {
86
+ pathname : "/error" ,
87
+ state : {
88
+ error : e . toString ( )
89
+ }
90
+ } ) ;
91
+ } ) ;
92
+ }
116
93
117
- render ( ) {
118
- return (
119
- < Layout >
120
- < Heading text = "Products" href = "/" />
121
- < div className = "form-group col-2" >
122
- < label className = "form-label" htmlFor = "input-product-search" > Search</ label >
123
- < input id = "input-product-search" className = "form-input" type = "text"
124
- value = { this . state . searchText } onChange = { this . onSearchTextChange } />
125
- </ div >
126
- {
127
- this . state . loading ?
128
- < div className = "loading loading-lg centered" /> :
129
- < ProductTable products = { this . state . visibleProducts } />
130
- }
131
- </ Layout >
132
- ) ;
133
- }
94
+ determineVisibleProducts ( ) {
95
+ const findProducts = ( search ) => {
96
+ search = search . toLowerCase ( ) ;
97
+ return this . state . products . filter ( p =>
98
+ p . id . toLowerCase ( ) . includes ( search )
99
+ || p . name . toLowerCase ( ) . includes ( search )
100
+ || p . type . toLowerCase ( ) . includes ( search )
101
+ )
102
+ } ;
103
+ this . setState ( ( s ) => {
104
+ return {
105
+ visibleProducts : s . searchText ? findProducts ( s . searchText ) : s . products
106
+ }
107
+ } ) ;
108
+ }
109
+
110
+ onSearchTextChange ( e ) {
111
+ this . setState ( {
112
+ searchText : e . target . value
113
+ } ) ;
114
+ this . determineVisibleProducts ( )
115
+ }
116
+
117
+ render ( ) {
118
+ return (
119
+ < Layout >
120
+ < Heading text = "Products" href = "/" />
121
+ < div className = "form-group col-2" >
122
+ < label className = "form-label" htmlFor = "input-product-search" > Search</ label >
123
+ < input id = "input-product-search" className = "form-input" type = "text"
124
+ value = { this . state . searchText } onChange = { this . onSearchTextChange } />
125
+ </ div >
126
+ {
127
+ this . state . loading ?
128
+ < div className = "loading loading-lg centered" /> :
129
+ < ProductTable products = { this . state . visibleProducts } />
130
+ }
131
+ </ Layout >
132
+ ) ;
133
+ }
134
134
}
135
135
136
136
App . propTypes = {
137
- history : PropTypes . shape ( {
138
- push : PropTypes . func . isRequired
139
- }
140
- ) . isRequired
137
+ history : PropTypes . shape ( {
138
+ push : PropTypes . func . isRequired
139
+ }
140
+ ) . isRequired
141
141
} ;
142
142
143
143
export default withRouter ( App ) ;
0 commit comments