diff --git a/src/components/ProjectList/CardsContainer.jsx b/src/components/ProjectList/CardsContainer.jsx index d786add..de467c0 100644 --- a/src/components/ProjectList/CardsContainer.jsx +++ b/src/components/ProjectList/CardsContainer.jsx @@ -1,6 +1,6 @@ import React from 'react'; import Select from 'react-select'; -import each from 'lodash/each' +import each from 'lodash/each'; import Card from './ProjectsCards'; import projectList from './listOfProjects'; @@ -15,125 +15,146 @@ export default class CardsContainer extends React.Component { this.state = { value: [], - filterList: this.sortArrayRandom(projectList) - } + filterList: this.sortArrayRandom(projectList), + error: "", + }; 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()) - }) + 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 })); + if (this.setTags.size) { + 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 ( + (!value || value.length === 0) && + (!this.inputValue || this.inputValue.length === 0) + ) { + return this.setState({ + filterList: this.sortArrayRandom(projectList), + error: "", + }); } // If tags filter applied if (value && value.length > 0) { const valueList = []; - value.map(v => { - return valueList.push(v.value) - }); + value.map((v) => 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))) { - + 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) + 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 }); + // Update state with results or error message + if (updatedList.length > 0) { + this.setState({ filterList: updatedList, error: "" }); + } else { + this.setState({ + filterList: [], + error: `No results found for your search: "${this.inputValue}". Please try again.`, + }); + } } // Search input handler handleChange(event) { - - this.inputValue = event.currentTarget.value; - - this.inputValue = this.inputValue.trim(); - this.inputValue = this.inputValue.toLowerCase(); - - this.handleFilterListUpdate(this.value) + this.inputValue = event.currentTarget.value.trim().toLowerCase(); + this.handleFilterListUpdate(this.value); } - sortArrayRandom(array){ - if(Array.isArray(array)){ - return array.sort(()=>0.5-Math.random()) + sortArrayRandom(array) { + if (Array.isArray(array)) { + return array.sort(() => 0.5 - Math.random()); } - return array + return array; } render() { - return (