Skip to content

Commit 8927da5

Browse files
TWHoubbmoz
authored andcommitted
Feature/#154707877 - User filters list of projects with filters (#180)
* feature: link checkboxes to filters in state [#154707877] * feature: filter projects using filters in state [#154707877] * refactor: move filter logic to handleCheckbox function [#154707877] * feature: use some() to check filter [#154707877]
1 parent 467146e commit 8927da5

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

client/components/Home/Home.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,44 @@ class Home extends Component {
1515
this.handleCheckbox = this.handleCheckbox.bind(this)
1616
this.renderList = this.renderList.bind(this)
1717
this.state = {
18-
filters: {}
18+
filters: {
19+
causes: [],
20+
technologies: []
21+
},
22+
filteredProjects: props.projects
1923
}
2024
}
2125

2226
componentDidMount () {
2327
this.props.onLoad()
2428
}
2529

30+
componentWillReceiveProps = (nextProps) => {
31+
if (this.props.projects !== nextProps.projects) {
32+
this.setState({filteredProjects: nextProps.projects})
33+
}
34+
}
35+
36+
projectContainsCause (project) {
37+
return this.state.filters.causes.length === 0 || this.state.filters.causes.some(cause => project.causes.indexOf(cause) >= 0)
38+
}
39+
projectContainsTech (project) {
40+
return this.state.filters.technologies.length === 0 || this.state.filters.technologies.some(tech => project.technologies.indexOf(tech) >= 0)
41+
}
42+
2643
handleCheckbox (event, checked) {
27-
console.log(event.target.name, event.target.value, checked)
44+
const filters = this.state.filters
45+
const index = this.state.filters[event.target.name].indexOf(event.target.value)
46+
if (index === -1) {
47+
filters[event.target.name].push(event.target.value)
48+
} else {
49+
filters[event.target.name].splice(index, 1)
50+
}
51+
this.setState({ filters })
52+
const filteredProjects = this.props.projects.filter(project => {
53+
return this.projectContainsCause(project) && this.projectContainsTech(project)
54+
})
55+
this.setState({ filteredProjects })
2856
}
2957

3058
renderList (items, type) {
@@ -56,7 +84,7 @@ class Home extends Component {
5684

5785
<ListOfProjects
5886
title={'Click To Apply'}
59-
projects={this.props.projects}
87+
projects={this.state.filteredProjects}
6088
className={styles.projectList}
6189
/>
6290
</div>

0 commit comments

Comments
 (0)