Skip to content

Instantly share code, notes, and snippets.

@ryanbelke
Created May 17, 2020 04:23
Show Gist options
  • Select an option

  • Save ryanbelke/2cf750a323da7f3afb062023f03729e6 to your computer and use it in GitHub Desktop.

Select an option

Save ryanbelke/2cf750a323da7f3afb062023f03729e6 to your computer and use it in GitHub Desktop.
new restaurants.js for deployment
/* /pages/restaurants.js */
import { useContext } from "react";
import { useQuery } from "@apollo/react-hooks";
import { useRouter } from "next/router";
import { gql } from "apollo-boost";
import Cart from "../components/cart/";
import AppContext from "../context/AppContext";
import {
Button,
Card,
CardBody,
CardImg,
CardText,
CardTitle,
Col,
Row,
} from "reactstrap";
const GET_RESTAURANT_DISHES = gql`
query($id: ID!) {
restaurant(id: $id) {
id
name
dishes {
id
name
description
price
image {
url
}
}
}
}
`;
function Restaurants() {
const appContext = useContext(AppContext);
const router = useRouter();
const { loading, error, data } = useQuery(GET_RESTAURANT_DISHES, {
variables: { id: router.query.id },
});
if (error) return "Error Loading Dishes";
if (loading) return <h1>Loading ...</h1>;
if (data.restaurant) {
const { restaurant } = data;
return (
<>
<h1>{restaurant.name}</h1>
<Row>
{restaurant.dishes.map((res) => (
<Col xs="6" sm="4" style={{ padding: 0 }} key={res.id}>
<Card style={{ margin: "0 10px" }}>
<CardImg
top={true}
style={{ height: 250 }}
src={
process.env.NODE_ENV === "production"
? res.image.url
: `${process.env.NEXT_PUBLIC_API_URL}${res.image.url}`
}
/>
<CardBody>
<CardTitle>{res.name}</CardTitle>
<CardText>{res.description}</CardText>
</CardBody>
<div className="card-footer">
<Button
outline
color="primary"
onClick={() => appContext.addItem(res)}
>
+ Add To Cart
</Button>
<style jsx>
{`
a {
color: white;
}
a:link {
text-decoration: none;
color: white;
}
.container-fluid {
margin-bottom: 30px;
}
.btn-outline-primary {
color: #007bff !important;
}
a:hover {
color: white !important;
}
`}
</style>
</div>
</Card>
</Col>
))}
<Col xs="3" style={{ padding: 0 }}>
<div>
<Cart />
</div>
</Col>
</Row>
</>
);
}
return <h1>Add Dishes</h1>;
}
export default Restaurants;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment