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