Sure! Let's create a basic e-commerce template using React. We'll use React for structure, React Router for navigation, Styled Components for styling, and React Hooks for state management. This template will include the key features similar to thebeerstore.ca: age verification, product catalog, shopping cart, and responsive design.
Step 1: Set Up Your React Project
- Install Node.js and npm if you haven't already.
- Create a new React project using Create React App:
npx create-react-app thebeerstore-template
cd thebeerstore-template
- Install additional dependencies:
npm install react-router-dom styled-components
Step 2: Project Structure
Your project structure should look like this:
src/
├── components/
│ ├── AgeGate.js
│ ├── Cart.js
│ ├── Header.js
│ ├── ProductCard.js
│ └── ProductGrid.js
├── pages/
│ ├── Home.js
│ └── ProductPage.js
├── App.js
├── index.js
└── GlobalStyles.js
Step 3: Global Styles
Create a GlobalStyles.js file for global styles using Styled Components.
// src/GlobalStyles.js
import { createGlobalStyle } from 'styled-components';
const GlobalStyles = createGlobalStyle`
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
background: #f4f4f4;
}
`;
export default GlobalStyles;
Step 4: Components
AgeGate.js
// src/components/AgeGate.js
import React from 'react';
import styled from 'styled-components';
const Modal = styled.div`
display: flex;
justify-content: center;
align-items: center;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
`;
const ModalContent = styled.div`
background: white;
padding: 20px;
border-radius: 8px;
text-align: center;
`;
const Button = styled.button`
margin: 10px;
padding: 10px 20px;
border: none;
border-radius: 5px;
background: #333;
color: white;
cursor: pointer;
`;
const AgeGate = ({ onConfirm, onDeny }) => {
return (
<Modal>
<ModalContent>
<h2>Age Verification</h2>
<p>Are you 19 years or older? You must be of legal drinking age to enter this site.</p>
<Button onClick={onConfirm}>Yes, I am 19+</Button>
<Button onClick={onDeny}>No, I'm under 19</Button>
</ModalContent>
</Modal>
);
};
export default AgeGate;
Header.js
// src/components/Header.js
import React from 'react';
import { Link } from 'react-router-dom';
import styled from 'styled-components';
const Nav = styled.nav`
background: #333;
color: white;
padding: 1rem;
display: flex;
justify-content: space-between;
align-items: center;
`;
const NavList = styled.ul`
list-style: none;
display: flex;
`;
const NavItem = styled.li`
margin: 0 1rem;
`;
const NavLink = styled(Link)`
color: white;
text-decoration: none;
`;
const Header = () => {
return (
<Nav>
<div className="logo">
<h1>The Beer Store</h1>
</div>
<NavList>
<NavItem>
<NavLink to="/">Home</NavLink>
</NavItem>
<NavItem>
<NavLink to="/products">Products</NavLink>
</NavItem>
<NavItem>
<NavLink to="/categories">Categories</NavLink>
</NavItem>
<NavItem>
<NavLink to="/about">About</NavLink>
</NavItem>
<NavItem>
<NavLink to="/contact">Contact</NavLink>
</NavItem>
<NavItem>
<NavLink to="/cart">Cart</NavLink>
</NavItem>
</NavList>
</Nav>
);
};
export default Header;
ProductCard.js
// src/components/ProductCard.js
import React from 'react';
import styled from 'styled-components';
const Card = styled.div`
border: 1px solid #ddd;
padding: 1rem;
text-align: center;
`;
const Image = styled.img`
max-width: 100%;
height: auto;
`;
const Button = styled.button`
margin-top: 1rem;
padding: 10px 20px;
border: none;
border-radius: 5px;
background: #333;
color: white;
cursor: pointer;
`;
const ProductCard = ({ product, onAddToCart }) => {
return (
<Card>
<Image src={product.image} alt={product.name} />
<h3>{product.name}</h3>
<p>{product.description}</p>
<p>Price: ${product.price.toFixed(2)}</p>
<Button onClick={() => onAddToCart(product)}>Add to Cart</Button>
</Card>
);
};
export default ProductCard;
ProductGrid.js
// src/components/ProductGrid.js
import React from 'react';
import styled from 'styled-components';
import ProductCard from './ProductCard';
const Grid = styled.div`
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
padding: 2rem;
`;
const ProductGrid = ({ products, onAddToCart }) => {
return (
<Grid>
{products.map((product) => (
<ProductCard key={product.id} product={product} onAddToCart={onAddToCart} />
))}
</Grid>
);
};
export default ProductGrid;
Cart.js
// src/components/Cart.js
import React from 'react';
import styled from 'styled-components';
const CartContainer = styled.div`
position: fixed;
right: 0;
top: 100px;
background: white;
border: 1px solid #ddd;
padding: 1rem;
width: 300px;
`;
const Cart = ({ cartItems, total, onCheckout }) => {
return (
<CartContainer>
<h2>Shopping Cart</h2>
<ul>
{cartItems.map((item, index) => (
<li key={index}>
{item.name} - ${item.price.toFixed(2)}
</li>
))}
</ul>
<p>Total: ${total.toFixed(2)}</p>
<button onClick={onCheckout}>Checkout</button>
</CartContainer>
);
};
export default Cart;
Step 5: Pages
Home.js
// src/pages/Home.js
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import AgeGate from '../components/AgeGate';
import styled from 'styled-components';
const Hero = styled.section`
background: #f4f4f4;
text-align: center;
padding: 4rem 2rem;
`;
const Home = () => {
const [isAgeVerified, setIsAgeVerified] = useState(false);
if (!isAgeVerified) {
return <AgeGate onConfirm={() => setIsAgeVerified(true)} onDeny={() => window.location.href = 'https://www.example.com/underage'} />;
}
return (
<div>
<Hero>
<h2>Discover Your Favorite Beers</h2>
<p>Shop from a wide selection of craft and premium beers. Limited-time offers!</p>
<Link to="/products">
<button>Shop Now</button>
</Link>
</Hero>
</div>
);
};
export default Home;
ProductPage.js
// src/pages/ProductPage.js
import React, { useState } from 'react';
import ProductGrid from '../components/ProductGrid';
import Cart from '../components/Cart';
const products = [
{ id: 1, name: 'Product Name 1', description: 'Description: A crisp lager with fruity notes.', price: 10.99, image: 'placeholder-image.jpg' },
{ id: 2, name: 'Product Name 2', description: 'Description: Bold IPA with hoppy flavors.', price: 12.5, image: 'placeholder-image.jpg' },
// Add more products as needed
];
const ProductPage = () => {
const [cartItems, setCartItems] = useState([]);
const [total, setTotal] = useState(0);
const handleAddToCart = (product) => {
setCartItems([...cartItems, product]);
setTotal(total + product.price);
};
return (
<div>
<ProductGrid products={products} onAddToCart={handleAddToCart} />
<Cart cartItems={cartItems} total={total} onCheckout={() => alert('Checkout functionality not implemented')} />
</div>
);
};
export default ProductPage;
Step 6: App.js
// src/App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import GlobalStyles from './GlobalStyles';
import Header from './components/Header';
import Home from './pages/Home';
import ProductPage from './pages/ProductPage';
const App = () => {
return (
<Router>
<GlobalStyles />
<Header />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/products" element={<ProductPage />} />
{/* Add more routes as needed */}
</Routes>
</Router>
);
};
export default App;
Step 7: index.js
Ensure index.js renders the App component.
// src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
How to Run the Project
- Start the development server:
npm start
- Open your browser and navigate to
http://localhost:3000.
How to Customize and Enhance:
- Replace Placeholders: Swap out placeholder images, product names, and descriptions with your actual content.
- Add Functionality:
- Integrate a backend (e.g., using Node.js, WooCommerce, or Shopify) for real shopping carts, user logins, and payments.
- Use React Context or Redux for more advanced state management.
- Add filters to the product grid (e.g., via React state for categories).
- Age Verification: In a real site, this should use geolocation and legal checks (e.g., via API).
- Testing: Test on mobile devices for responsiveness.
- Legal Considerations: If you're selling alcohol, ensure compliance with laws (e.g., age gates, shipping restrictions).
This template provides a solid foundation. For a production-ready site, you'll need to add more features and integrate with a backend. If you need further assistance, feel free to ask!