Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add React Router Example #279

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions with-react-router/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
74 changes: 74 additions & 0 deletions with-react-router/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Getting Started with Create React App

This is a [React](https://reactjs.org/) project bootstrapped with [Create React App](https://github.com/facebook/create-react-app) and structured/developed by Joel Olawanle.

For this app, we made use of react.js to create a blog showcase application that retrieves all blogposts from GraphCMS by querying data using GraphQL and implemented pagination from scratch and using a package. Test it our [here]([https://graphcms-react-pagination.netlify.app/](https://cocktails-graphcms.netlify.app/)).

![image](https://user-images.githubusercontent.com/57611810/174583683-15fc13ad-0b23-4d5a-a8b4-75fd031dfa4d.png)

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
42 changes: 42 additions & 0 deletions with-react-router/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "cocktail-app-graphcms",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"gql": "^1.1.2",
"graphql-request": "^4.3.0",
"graphql-tag": "^2.12.6",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
27 changes: 27 additions & 0 deletions with-react-router/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { lazy, Suspense } from 'react';
import { Routes, Route } from 'react-router-dom';
import NavBar from './Components/NavBar';
const Home = lazy(() => import('./Pages/Home'));
const About = lazy(() => import('./Pages/About'));
const Products = lazy(() => import('./Pages/Products'));
const ProductDetails = lazy(() => import('./Pages/ProductDetails'));
const NoMatch = lazy(() => import('./Components/NoMatch'));

const App = () => {
return (
<>
<NavBar />
<Suspense fallback={<div className="container">Loading...</div>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/products" element={<Products />} />
<Route path="/products/:slug" element={<ProductDetails />} />
<Route path="*" element={<NoMatch />} />
</Routes>
</Suspense>
</>
);
};

export default App;
31 changes: 31 additions & 0 deletions with-react-router/src/Components/NavBar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { NavLink } from 'react-router-dom';

const NavBar = () => {
return (
<div>
<nav>
<div className="nav-items container">
<div className="logo">
<a href="/">
<h1>COCKTAIL APP</h1>
</a>
</div>
<ul>
<li>
<NavLink to="/">Home</NavLink>
</li>
<li>
<NavLink to="/about">About</NavLink>
</li>
<li>
<NavLink to="/products">Products</NavLink>
</li>
</ul>
</div>
</nav>
</div>
);
};

export default NavBar;
16 changes: 16 additions & 0 deletions with-react-router/src/Components/NoMatch.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

const NoMatch = () => {
return (
<div>
<div className="container">
<div className="error-page">
<h1 className="error-code">404</h1>
<p className="error-text">Page not found</p>
</div>
</div>
</div>
);
};

export default NoMatch;
20 changes: 20 additions & 0 deletions with-react-router/src/Components/ProductCard.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Link } from 'react-router-dom';

const ProductCard = ({ product }) => {
return (
<>
<img src={product.image.url} alt="" className="cocktail-img" />
<div className="cocktail-info">
<div className="content-text">
<h2 className="cocktail-name">{product.name}</h2>
<span className="info">{product.info}</span>
</div>
<Link to={`/products/${product.slug}`}>
<div className="btn">View Details</div>
</Link>
</div>
</>
);
};

export default ProductCard;
33 changes: 33 additions & 0 deletions with-react-router/src/Components/ProductDetailsCard.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const ProductDetailsCard = ({ product }) => {
return (
<div className="flex-container">
{product.image && (
<img src={product.image.url} alt="" className="cocktail-img" />
)}
<div className="cocktail-infos">
<div className="row">
<h3 className="label">Name: </h3>
<p className="text">{product.name}</p>
</div>
<div className="row">
<h3 className="label">Category: </h3>
<p className="text">{product.category}</p>
</div>
<div className="row">
<h3 className="label">Info: </h3>
<p className="text">{product.info}</p>
</div>
<div className="row">
<h3 className="label">Instructions: </h3>
<p className="text">{product.instructions}</p>
</div>
<div className="row">
<h3 className="label">Ingredients: </h3>
<p className="text">{product.ingredients}</p>
</div>
</div>
</div>
);
};

export default ProductDetailsCard;
31 changes: 31 additions & 0 deletions with-react-router/src/Pages/About.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useNavigate } from 'react-router-dom';

const About = () => {
const navigate = useNavigate();
return (
<div className="container">
<button className="btn" onClick={() => navigate(-1)}>
Go Back
</button>
<div className="title">
<h1>About</h1>
</div>
<div className="about-container">
<p>
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Voluptatum
eos tenetur beatae nihil dolorum possimus voluptate esse voluptatibus,
aliquid ratione facere enim eveniet modi necessitatibus quas
asperiores pariatur, amet velit?
</p>
<p>
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Voluptatum
eos tenetur beatae nihil dolorum possimus voluptate esse voluptatibus,
aliquid ratione facere enim eveniet modi necessitatibus quas
asperiores pariatur, amet velit?
</p>
</div>
</div>
);
};

export default About;
18 changes: 18 additions & 0 deletions with-react-router/src/Pages/Home.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Link } from 'react-router-dom';

const Home = () => {
return (
<div className="container">
<div className="banner-container">
<div className="banner">
<h2>It's time for some Cocktails</h2>
<Link to="/products">
<div className="btn">View Cocktails</div>
</Link>
</div>
</div>
</div>
);
};

export default Home;
35 changes: 35 additions & 0 deletions with-react-router/src/Pages/ProductDetails.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useEffect, useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import ProductDetailsCard from '../Components/ProductDetailsCard';
import { getProductBySlug } from '../api';

const ProductDetails = () => {
const [product, setProduct] = useState([]);
const navigate = useNavigate();
const { slug } = useParams();

useEffect(() => {
const fetchProduct = async () => {
const { cocktail } = await getProductBySlug(slug);
setProduct(cocktail);
};

fetchProduct();
}, [slug]);

return (
<div className="container">
<button className="btn" onClick={() => navigate(-1)}>
Go Back
</button>
<div>
<div className="title">
<h1>{product.name}</h1>
</div>
<ProductDetailsCard product={product} />
</div>
</div>
);
};

export default ProductDetails;
38 changes: 38 additions & 0 deletions with-react-router/src/Pages/Products.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import ProductCard from '../Components/ProductCard';
import { getAllCocktails } from '../api';

const Products = () => {
const [products, setProducts] = useState([]);
const navigate = useNavigate();

useEffect(() => {
const fetchProducts = async () => {
const { cocktails } = await getAllCocktails();
setProducts(cocktails);
};

fetchProducts();
}, []);

return (
<div className="container">
<button className="btn" onClick={() => navigate(-1)}>
Go Back
</button>
<div className="title">
<h1>CockTails</h1>
</div>
<div className="cocktails-container">
{products.map((product) => (
<div key={product.id} className="cocktail-card">
<ProductCard product={product} />
</div>
))}
</div>
</div>
);
};

export default Products;
Loading