Skip to content

Instantly share code, notes, and snippets.

@ryanbelke
Created May 17, 2020 04:23
Show Gist options
  • Save ryanbelke/b5852d1d4cd6cd02a871263d36ebc97e to your computer and use it in GitHub Desktop.
Save ryanbelke/b5852d1d4cd6cd02a871263d36ebc97e to your computer and use it in GitHub Desktop.
new RestaurantList.js for deployment
/* components/RestaurantList/index.js */
import { useQuery } from "@apollo/react-hooks";
import { gql } from "apollo-boost";
import Link from "next/link";
import {
Card,
CardBody,
CardImg,
CardText,
CardTitle,
Row,
Col,
} from "reactstrap";
const QUERY = gql`
{
restaurants {
id
name
description
image {
url
}
}
}
`;
function RestaurantList(props) {
const { loading, error, data } = useQuery(QUERY);
if (error) return "Error loading restaurants";
//if restaurants are returned from the GraphQL query, run the filter query
//and set equal to variable restaurantSearch
if (loading) return <h1>Fetching</h1>;
if (data.restaurants && data.restaurants.length) {
//searchQuery
const searchQuery = data.restaurants.filter((query) =>
query.name.toLowerCase().includes(props.search)
);
if (searchQuery.length != 0) {
return (
<Row>
{searchQuery.map((res) => (
<Col xs="6" sm="4" key={res.id}>
<Card style={{ margin: "0 0.5rem 20px 0.5rem" }}>
<CardImg
top={true}
style={{ height: 250 }}
src={
process.env.NODE_ENV === "production"
? res.image[0].url
: `${process.env.NEXT_PUBLIC_API_URL}${res.image[0].url}`
}
/>
<CardBody>
<CardTitle>{res.name}</CardTitle>
<CardText>{res.description}</CardText>
</CardBody>
<div className="card-footer">
<Link
as={`/restaurants/${res.id}`}
href={`/restaurants?id=${res.id}`}
>
<a className="btn btn-primary">View</a>
</Link>
</div>
</Card>
</Col>
))}
<style jsx global>
{`
a {
color: white;
}
a:link {
text-decoration: none;
color: white;
}
a:hover {
color: white;
}
.card-columns {
column-count: 3;
}
`}
</style>
</Row>
);
} else {
return <h1>No Restaurants Found</h1>;
}
}
return <h5>Add Restaurants</h5>;
}
export default RestaurantList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment