Search by name and category
Solution: Created a query string that would be parsed on the business index page and saved in the local state
constructor(props) {
super(props);
this.state = {
name: "",
query: ""
}
this.businessCategories = this.businessCategories.bind(this);
this.searchBusinesses = this.searchBusinesses.bind(this);
this.redirectShow = this.redirectShow.bind(this);
}
searchBusinesses(category){
return (e) => {
e.preventDefault();
this.setState({name: category}, () => {
this.props.fetchBusinesses(this.state)
.then(
() => this.props.history.push({ pathname:'/businesses', search: `?name=${category}`})
)
});
}
}
Reviews on the business show page
Solution: The payload from the backend includes: businesses, business's reviews, and authored reviews
json.businesses do
@businesses.each do |business|
json.set! business.id do
json.avg_rating business.reviews.average(:rating).round(1)
json.num_reviews business.reviews.count
json.extract! business, :id, :name, :address, :phone, :price
json.categories business.categories.map { |category| category.title }
end
end
end
json.reviews do
@businesses.each do |business|
json.set! business.reviews.first.business_id do
json.extract! business.reviews.first, :id, :description, :author_id, :business_id, :rating
end
end
end