1
- import React from ' react' ;
2
- import Select from ' react-select' ;
3
- import each from ' lodash/each'
1
+ import React from " react" ;
2
+ import Select from " react-select" ;
3
+ import each from " lodash/each" ;
4
4
5
- import Card from ' ./ProjectsCards' ;
6
- import projectList from ' ./listOfProjects' ;
5
+ import Card from " ./ProjectsCards" ;
6
+ import projectList from " ./listOfProjects" ;
7
7
8
- import ' ./css/cards-container.css' ;
9
- import ' ./css/search.css' ;
8
+ import " ./css/cards-container.css" ;
9
+ import " ./css/search.css" ;
10
10
11
11
export default class CardsContainer extends React . Component {
12
12
constructor ( props ) {
13
13
super ( props ) ;
14
14
15
15
this . state = {
16
16
value : [ ] ,
17
- filterList : this . sortArrayRandom ( projectList )
18
- }
17
+ filterList : this . sortArrayRandom ( projectList ) ,
18
+ } ;
19
19
20
20
this . setTags = new Set ( ) ;
21
21
this . filterOptions = [ ] ;
22
22
this . valueList = [ ] ;
23
23
24
24
for ( let i = 0 ; i < projectList . length ; i ++ ) {
25
-
26
25
if ( projectList [ i ] . tags ) {
27
-
28
- projectList [ i ] . tags . forEach ( tag => {
29
-
30
- projectList [ i ] . tags . sort ( )
31
- this . setTags . add ( tag . toLowerCase ( ) )
32
- } )
26
+ projectList [ i ] . tags . forEach ( ( tag ) => {
27
+ projectList [ i ] . tags . sort ( ) ;
28
+ this . setTags . add ( tag . toLowerCase ( ) ) ;
29
+ } ) ;
33
30
}
34
31
}
35
32
36
- this . setTags . forEach ( v => this . filterOptions . push ( { value : v , label : v } ) ) ;
33
+ this . setTags . forEach ( ( v ) =>
34
+ this . filterOptions . push ( { value : v , label : v } )
35
+ ) ;
37
36
this . handleSelectChange = this . handleSelectChange . bind ( this ) ;
38
37
this . handleChange = this . handleChange . bind ( this ) ;
39
38
}
40
39
41
- handleSelectChange ( value ) {
42
-
43
- this . value = value ;
44
- this . setState ( { value } ) ;
45
- this . handleFilterListUpdate ( value ) ;
40
+ handleSelectChange ( selectedOptions ) {
41
+ const valueArray = Array . isArray ( selectedOptions )
42
+ ? selectedOptions
43
+ : [ selectedOptions ] ;
44
+ this . setState ( { value : valueArray } ) ;
45
+ this . handleFilterListUpdate ( valueArray ) ;
46
46
}
47
47
48
48
handleFilterListUpdate ( value ) {
49
+ let updatedList = [ ...projectList ] ;
49
50
50
- let updatedList = [ ] ;
51
-
52
- // If no filters
53
- if ( ( ! value || value . length === 0 ) && ( ! this . inputValue || this . inputValue . length === 0 ) ) {
51
+ if (
52
+ ( ! value || value . length === 0 ) &&
53
+ ( ! this . inputValue || this . inputValue . length === 0 )
54
+ ) {
54
55
return this . setState ( { filterList : this . sortArrayRandom ( projectList ) } ) ;
55
56
}
56
57
57
58
// If tags filter applied
58
- if ( value && value . length > 0 ) {
59
- const valueList = [ ] ;
60
-
61
- value . map ( v => {
62
- return valueList . push ( v . value )
63
- } ) ;
64
-
65
- each ( projectList , ( project ) => {
66
-
67
- if ( ! project . tags ) return ;
68
-
69
- let lowerCaseTags = project . tags . map ( v => v . toLowerCase ( ) )
70
- if ( valueList . every ( v => lowerCaseTags . includes ( v ) ) ) {
71
-
72
- updatedList . push ( project ) ;
73
- }
74
- } )
59
+ if ( value . length > 0 ) {
60
+ const valueList = value . map ( ( v ) => v . value . toLowerCase ( ) ) ;
61
+
62
+ updatedList = updatedList . filter (
63
+ ( project ) =>
64
+ project . tags &&
65
+ project . tags . some ( ( tag ) => valueList . includes ( tag . toLowerCase ( ) ) )
66
+ ) ;
75
67
}
76
68
77
69
// If search input value is not empty
78
- if ( this . inputValue && this . inputValue . length > 0 ) {
79
-
80
- const searchedList = [ ]
81
- each ( ( ( updatedList . length > 0 ) ? updatedList : projectList ) , ( project ) => {
82
-
83
- if ( project . name . toLowerCase ( ) . includes ( this . inputValue )
84
- || project . description . toLowerCase ( ) . includes ( this . inputValue )
85
- || project . tags . toString ( ) . toLowerCase ( ) . includes ( this . inputValue ) ) {
86
-
87
- searchedList . push ( project )
88
- }
89
- } ) ;
90
-
91
- updatedList = searchedList ;
70
+ if ( this . inputValue && this . inputValue . trim ( ) . length > 0 ) {
71
+ const searchTerm = this . inputValue . toLowerCase ( ) ;
72
+
73
+ updatedList = updatedList . filter (
74
+ ( project ) =>
75
+ project . name . toLowerCase ( ) . includes ( searchTerm ) ||
76
+ project . description . toLowerCase ( ) . includes ( searchTerm ) ||
77
+ ( project . tags &&
78
+ project . tags . some ( ( tag ) => tag . toLowerCase ( ) . includes ( searchTerm ) ) )
79
+ ) ;
92
80
}
93
81
94
82
this . setState ( { filterList : updatedList } ) ;
95
83
}
96
84
97
85
// Search input handler
98
86
handleChange ( event ) {
99
-
100
87
this . inputValue = event . currentTarget . value ;
101
88
102
89
this . inputValue = this . inputValue . trim ( ) ;
103
90
this . inputValue = this . inputValue . toLowerCase ( ) ;
104
91
105
- this . handleFilterListUpdate ( this . value )
92
+ this . handleFilterListUpdate ( this . value ) ;
106
93
}
107
94
108
- sortArrayRandom ( array ) {
109
- if ( Array . isArray ( array ) ) {
110
- return array . sort ( ( ) => 0.5 - Math . random ( ) )
95
+ sortArrayRandom ( array ) {
96
+ if ( Array . isArray ( array ) ) {
97
+ return array . sort ( ( ) => 0.5 - Math . random ( ) ) ;
111
98
}
112
- return array
99
+ return array ;
113
100
}
114
101
115
102
render ( ) {
116
-
117
103
return (
118
104
< div >
119
- < div id = 'container' >
120
- < div className = 'inputContainer' >
121
- < input id = 'search' type = 'text' name = 'search' placeholder = 'Search...' onChange = { this . handleChange } aria-label = 'Search' />
105
+ < div id = "container" >
106
+ < div className = "inputContainer" >
107
+ < input
108
+ id = "search"
109
+ type = "text"
110
+ name = "search"
111
+ placeholder = "Search..."
112
+ onChange = { this . handleChange }
113
+ aria-label = "Search"
114
+ />
122
115
</ div >
123
- < div id = "tag-selector-container" className = ' inputContainer' >
116
+ < div id = "tag-selector-container" className = " inputContainer" >
124
117
< Select
125
- name = ' tag-selector'
118
+ name = " tag-selector"
126
119
value = { this . state . value }
127
120
onChange = { this . handleSelectChange }
128
121
options = { this . filterOptions }
129
- multi = { true }
130
- placeholder = { < div className = 'filter-placeholder-text' > Filter</ div > }
131
- aria-labelledby = 'tag-selector-container'
122
+ isMulti = { true }
123
+ placeholder = {
124
+ < div className = "filter-placeholder-text" > Filter</ div >
125
+ }
126
+ aria-labelledby = "tag-selector-container"
132
127
/>
133
128
</ div >
134
129
</ div >
135
- < section id = ' project-list' className = ' containerLayout' >
130
+ < section id = " project-list" className = " containerLayout" >
136
131
{ this . state . filterList . map ( ( item , key ) => {
137
132
return (
138
133
< Card
@@ -142,7 +137,7 @@ export default class CardsContainer extends React.Component {
142
137
projectLink = { item . projectLink }
143
138
description = { item . description }
144
139
tags = { item . tags }
145
- className = ' testing-testing'
140
+ className = " testing-testing"
146
141
/>
147
142
) ;
148
143
} ) }
0 commit comments