1
- import React , { Component } from 'react'
2
- import FarmContext from '../../contexts/FarmContext'
3
- import './FilterModal.css'
1
+ /* eslint-disable react/no-array-index-key */
2
+ import React , { Component } from 'react' ;
3
+ import FarmContext from '../../contexts/FarmContext' ;
4
+ import './FilterModal.css' ;
4
5
5
6
6
7
class FilterModal extends Component {
7
- static defaultProps = {
8
- onUpdateProducts : ( ) => { } ,
9
- onUpdatePurchaseOptions : ( ) => { } ,
8
+ constructor ( props ) {
9
+ super ( props ) ;
10
+ this . state = {
11
+ products : [ ] ,
12
+ purchaseOptions : [ ] ,
13
+ } ;
10
14
}
11
15
12
- static contextType = FarmContext
13
-
14
- state = {
15
- products : [ ] ,
16
- purchaseOptions : [ ]
17
- }
18
-
19
- onUpdateProducts = e => {
20
- const productsArray = this . state . products
16
+ onUpdateProducts = ( e ) => {
17
+ const { products } = this . state ;
18
+ const productsArray = products ;
21
19
if ( e . target . checked ) {
22
- productsArray . push ( e . target . value )
23
- this . setState ( { products : productsArray } )
20
+ productsArray . push ( e . target . value ) ;
21
+ this . setState ( { products : productsArray } ) ;
24
22
} else if ( ! e . target . checked ) {
25
- const removedProducts = productsArray . filter ( product => product !== e . target . value )
26
- this . setState ( { products : removedProducts } )
23
+ const removedProducts = productsArray . filter (
24
+ ( product ) => product !== e . target . value ,
25
+ ) ;
26
+ this . setState ( { products : removedProducts } ) ;
27
27
}
28
-
29
28
}
30
29
31
- onUpdatePurchaseOptions = e => {
32
- const purchaseOptionsArray = this . state . purchaseOptions
30
+ onUpdatePurchaseOptions = ( e ) => {
31
+ const { purchaseOptions } = this . state ;
32
+ const purchaseOptionsArray = purchaseOptions ;
33
33
if ( e . target . checked ) {
34
- purchaseOptionsArray . push ( e . target . value )
35
- this . setState ( { purchaseOptions : purchaseOptionsArray } )
34
+ purchaseOptionsArray . push ( e . target . value ) ;
35
+ this . setState ( { purchaseOptions : purchaseOptionsArray } ) ;
36
36
} else if ( ! e . target . checked ) {
37
- const removedPurchaseOptions = purchaseOptionsArray . filter ( option => option !== e . target . value )
38
- this . setState ( { purchaseOptions : removedPurchaseOptions } )
37
+ const removedPurchaseOptions = purchaseOptionsArray . filter (
38
+ ( option ) => option !== e . target . value ,
39
+ ) ;
40
+ this . setState ( { purchaseOptions : removedPurchaseOptions } ) ;
39
41
}
40
-
41
42
}
42
43
43
- handleSubmit = e => {
44
- e . preventDefault ( )
45
- this . props . onUpdateOptions ( this . state . products , this . state . purchaseOptions )
46
- this . props . handleClose ( )
44
+ handleSubmit = ( e ) => {
45
+ e . preventDefault ( ) ;
46
+ const { onUpdateOptions, handleClose } = this . props ;
47
+ const { products, purchaseOptions } = this . state ;
48
+ onUpdateOptions ( products , purchaseOptions ) ;
49
+ handleClose ( ) ;
47
50
}
48
51
49
- render ( ) {
50
- const showHideClassName = this . props . show ? 'filter-modal display-block' : 'filter-modal display-none'
52
+ render ( ) {
53
+ const { show, handleClose } = this . props ;
54
+ const showHideClassName = show ? 'filter-modal display-block' : 'filter-modal display-none' ;
51
55
52
- const { products } = this . context
53
- const { purchaseOptions } = this . context
56
+ const { products, purchaseOptions } = this . context ;
54
57
55
- const productCheckboxes = products . map ( ( product , index ) =>
58
+ const productCheckboxes = products . map ( ( product , index ) => (
56
59
< li key = { index } >
57
- < label className = ' filter-modal__checkbox--label' htmlFor = { ' product' + index } >
58
- < input
59
- type = ' checkbox'
60
- id = { ' product' + index }
61
- name = { ' product' + index }
60
+ < label className = " filter-modal__checkbox--label" htmlFor = { ` product${ index } ` } >
61
+ < input
62
+ type = " checkbox"
63
+ id = { ` product${ index } ` }
64
+ name = { ` product${ index } ` }
62
65
value = { product }
63
- onChange = { this . onUpdateProducts . bind ( this ) } />
64
- < span className = 'filter-modal__checkbox--custom' > </ span >
66
+ onChange = { this . onUpdateProducts . bind ( this ) }
67
+ />
68
+ < span className = "filter-modal__checkbox--custom" />
65
69
{ product }
66
70
</ label >
67
- </ li > )
71
+ </ li >
72
+ ) ) ;
68
73
69
- const purchaseOptionCheckboxes = purchaseOptions . map ( ( purchaseOption , index ) =>
74
+ const purchaseOptionCheckboxes = purchaseOptions . map ( ( purchaseOption , index ) => (
70
75
< li key = { index } >
71
- < label className = ' filter-modal__checkbox--label' htmlFor = { ' purchaseOption' + index } >
76
+ < label className = " filter-modal__checkbox--label" htmlFor = { ` purchaseOption${ index } ` } >
72
77
< input
73
- type = ' checkbox'
74
- id = { ' purchaseOption' + index }
75
- name = { ' purchaseOption' + index }
78
+ type = " checkbox"
79
+ id = { ` purchaseOption${ index } ` }
80
+ name = { ` purchaseOption${ index } ` }
76
81
value = { purchaseOption }
77
82
onChange = { this . onUpdatePurchaseOptions . bind ( this ) } />
78
- < span className = ' filter-modal__checkbox--custom' > </ span >
83
+ < span className = " filter-modal__checkbox--custom" / >
79
84
{ purchaseOption }
80
85
</ label >
81
- </ li > )
86
+ </ li >
87
+ ) ) ;
82
88
83
89
return (
84
90
< div className = { showHideClassName } >
85
- < div className = ' filter-modal__main' >
91
+ < div className = " filter-modal__main" >
86
92
< form onSubmit = { this . handleSubmit } >
87
93
< div >
88
94
< div >
89
95
Filter by product type:
90
96
</ div >
91
- < ul className = ' filter-modal__products' >
97
+ < ul className = " filter-modal__products" >
92
98
{ productCheckboxes }
93
99
</ ul >
94
100
</ div >
95
101
< div >
96
102
< div >
97
103
Filter by purchase option:
98
104
</ div >
99
- < ul className = ' filter-modal__purchase-options' >
105
+ < ul className = " filter-modal__purchase-options" >
100
106
{ purchaseOptionCheckboxes }
101
107
</ ul >
102
108
</ div >
103
- < button type = ' button' onClick = { this . props . handleClose } >
109
+ < button type = " button" onClick = { handleClose } >
104
110
Close
105
111
</ button >
106
- < button type = ' submit' >
112
+ < button type = " submit" >
107
113
Filter
108
114
</ button >
109
115
</ form >
110
116
</ div >
111
117
</ div >
112
- )
118
+ ) ;
113
119
}
114
120
}
115
121
116
- export default FilterModal
122
+ export default FilterModal ;
123
+
124
+ FilterModal . defaultProps = {
125
+ onUpdateProducts : ( ) => { } ,
126
+ onUpdatePurchaseOptions : ( ) => { } ,
127
+ } ;
128
+
129
+ FilterModal . contextType = FarmContext ;
0 commit comments