Instantly share code, notes, and snippets.
Tunji545/MenuComponent.js Secret
Last active
October 26, 2020 06:58
Star
You must be signed in to star a gist
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, { Component } from "react"; | |
import { Card, CardImg, CardImgOverlay, CardTitle } from "reactstrap"; | |
import DishDetail from "./DishdetailComponent"; | |
class Menu extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
selectedDish: null, | |
selectedComment: null | |
} | |
this.onDishSelect = this.onDishSelect.bind(this); | |
this.renderDish = this.renderDish.bind(this); | |
this.renderCommments = this.renderComments.bind(this); | |
} | |
onDishSelect(dish, dishComments) { | |
this.setState({ | |
selectedDish: dish, | |
selectedComment: dishComments | |
}) | |
console.log(dish, dishComments); | |
} | |
renderDish(dish) { | |
if(dish != null) { | |
console.log(dish); | |
return( | |
<DishDetail dish={this.state.selectedDish} /> | |
) | |
} | |
else { | |
return ( | |
<div></div> | |
) | |
} | |
} | |
renderComments(dishComments) { | |
if(dishComments !== null) { | |
return ( | |
<DishDetail comments={this.state.selectedComment} /> | |
) | |
} | |
else { | |
return ( | |
<div></div> | |
) | |
} | |
} | |
render() { | |
const menu = this.props && this.props.dishes.map((dish) => { | |
return ( | |
<div className="col-12 col-md-5 m-1"> | |
<Card key={dish.id} onClick={() => this.onDishSelect(dish, dish.comments)}> | |
<CardImg width="100%" src={dish.image} alt={dish.name} /> | |
<CardImgOverlay> | |
<CardTitle>{dish.name}</CardTitle> | |
</CardImgOverlay> | |
</Card> | |
</div> | |
) | |
}) | |
return ( | |
<div className="container"> | |
<div className="row"> | |
{menu} | |
</div> | |
<div> | |
{this.renderDish(this.state.selectedDish)} | |
{this.renderComments(this.state.selectedComment)} | |
</div> | |
</div> | |
) | |
} | |
} | |
export default Menu; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment