Created
June 30, 2020 15:28
-
-
Save cv2k10/d1cf28f049ccc4ec90567b9c39b16266 to your computer and use it in GitHub Desktop.
On line 69, code that handles not found search result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import Card from '../Components/Card'; | |
import data from '../Data/drinks.json'; | |
import Preparation from '../Pages/Preparation'; | |
import { Link } from 'react-router-dom'; | |
import _, { update } from 'lodash'; | |
function searchingFor(search) { | |
return function(cocktails) { | |
return cocktails.name.toLowerCase().includes(search.toLowerCase()) || !search; | |
} | |
} | |
export default class Searcher extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
search: '', | |
data: data, | |
show: null, | |
}; | |
this.clickMe = this.clickMe.bind(this); | |
} | |
updateSearch = (event) => { | |
this.setState({ search: event.target.value.substr(0, 20)}); | |
} | |
onSubmit = e => { | |
e.preventDefault(); | |
//console.log(this.state.seach) | |
this.setState({searchingFor}) | |
}; | |
clickMe = show => { | |
//console.log(show); | |
this.setState({show}) | |
}; | |
render() { | |
const {search, data} = this.state; | |
return ( | |
<div className="search"> | |
<form onSubmit={e => this.onSubmit(e)}> | |
<div> | |
<input type="text" | |
id="input" | |
value={search} | |
onChange={this.updateSearch.bind(this)}></input> | |
<button>Search</button> | |
</div> | |
</form> | |
<div className="container"> | |
{this.state.show ? <Preparation cocktail={this.state.show} /> : null} | |
</div> | |
<div> | |
{ data.cocktails.filter(searchingFor(search)).length===0 && <p>The drink is not found / no result</p> } | |
{ data.cocktails.filter(searchingFor(search)).map((cocktail, i) => { | |
return ( | |
<Link to="/explore/preparation" onClick={this.clickMe.bind(this, cocktail)}> | |
<Card | |
key={i} | |
name={cocktail.name} | |
preparation={cocktail.preparation} | |
src={cocktail.image} | |
/> | |
</Link> | |
) | |
})} | |
</div> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment