Skip to content
Snippets Groups Projects
Commit b12ec680 authored by shwtea's avatar shwtea
Browse files

reactjs Assignemnt

parents
No related branches found
No related tags found
No related merge requests found
Showing with 722 additions and 0 deletions
# 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*
# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## 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)
Source diff could not be displayed: it is too large. Options to address this: view the blob.
{
"name": "ecommerce-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/react": "^11.14.0",
"@emotion/styled": "^11.14.0",
"@mui/icons-material": "^6.4.3",
"@mui/material": "^6.4.3",
"axios": "^1.7.9",
"cra-template": "1.2.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-redux": "^9.2.0",
"react-router-dom": "^7.1.5",
"react-scripts": "5.0.1",
"redux": "^5.0.1",
"web-vitals": "^4.2.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"
]
}
}
public/favicon.ico

3.78 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
public/logo192.png

5.22 KiB

public/logo512.png

9.44 KiB

{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import { Container, AppBar, Toolbar, Typography, Button } from '@mui/material';
import HomePage from './components/HomePage';
import CartPage from './components/CartPage';
import WishlistPage from './components/WishlistPage';
import CheckoutPage from './components/CheckoutPage';
import ProductPage from './components/ProductPage';
const App = () => {
return (
<Router>
<AppBar position="sticky">
<Toolbar>
<Typography variant="h6" sx={{ flexGrow: 1 }}>
E-Commerce App
</Typography>
<Button color="inherit" href="/cart">
Cart
</Button>
<Button color="inherit" href="/wishlist">
Wishlist
</Button>
<Button color="inherit" href="/checkout">
Checkout
</Button>
</Toolbar>
</AppBar>
<Container sx={{ paddingY: 4 }}>
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/cart" element={<CartPage />} />
<Route path="/wishlist" element={<WishlistPage />} />
<Route path="/checkout" element={<CheckoutPage />} />
<Route path="/product/:id" element={<ProductPage />} /> {/* Product Details Page */}
</Routes>
</Container>
</Router>
);
};
export default App;
import { render, screen } from '@testing-library/react';
import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
import React, { createContext, useState, useContext } from 'react';
// Create a context for cart and wishlist
const CartContext = createContext();
export const useCart = () => {
return useContext(CartContext);
};
export const CartProvider = ({ children }) => {
const [cart, setCart] = useState([]);
const [wishlist, setWishlist] = useState([]);
const addToCart = (product) => {
setCart((prevCart) => [...prevCart, product]);
};
const addToWishlist = (product) => {
setWishlist((prevWishlist) => [...prevWishlist, product]);
};
return (
<CartContext.Provider value={{ cart, wishlist, addToCart, addToWishlist }}>
{children}
</CartContext.Provider>
);
};
import React, { useState } from 'react';
import { Button, Box, Typography, Grid, Card, CardContent, CardMedia, IconButton } from '@mui/material';
import { RemoveCircle, AddCircle } from '@mui/icons-material';
import { Link } from 'react-router-dom';
const CartPage = () => {
const [cartItems, setCartItems] = useState([
{
id: 1,
name: 'Product 1',
description: 'This is a short description for Product 1.',
price: 49.99,
quantity: 1,
image: 'https://via.placeholder.com/300', // Example image
},
{
id: 2,
name: 'Product 2',
description: 'This is a short description for Product 2.',
price: 99.99,
quantity: 1,
image: 'https://via.placeholder.com/300', // Example image
},
]);
const updateQuantity = (id, quantity) => {
setCartItems(prevItems =>
prevItems.map(item =>
item.id === id ? { ...item, quantity } : item
)
);
};
const removeItem = (id) => {
setCartItems(prevItems => prevItems.filter(item => item.id !== id));
};
const calculateTotal = () => {
return cartItems.reduce((total, item) => total + item.price * item.quantity, 0).toFixed(2);
};
return (
<Box sx={{ padding: 4 }}>
<Typography variant="h4" gutterBottom>
Your Cart
</Typography>
{cartItems.length === 0 ? (
<Typography>Your cart is empty. Start shopping!</Typography>
) : (
<Grid container spacing={4}>
{cartItems.map((item) => (
<Grid item xs={12} sm={6} md={4} key={item.id}>
<Card>
<CardMedia
component="img"
height="200"
image={item.image}
alt={item.name}
/>
<CardContent>
<Typography variant="h6">{item.name}</Typography>
<Typography variant="body2" color="text.secondary">
{item.description}
</Typography>
<Typography variant="h6" color="primary">
${item.price}
</Typography>
<Box sx={{ display: 'flex', alignItems: 'center', marginTop: 2 }}>
<IconButton
onClick={() => updateQuantity(item.id, item.quantity - 1)}
disabled={item.quantity <= 1}
>
<RemoveCircle />
</IconButton>
<Typography>{item.quantity}</Typography>
<IconButton onClick={() => updateQuantity(item.id, item.quantity + 1)}>
<AddCircle />
</IconButton>
</Box>
<Button
variant="outlined"
color="secondary"
fullWidth
sx={{ marginTop: 2 }}
onClick={() => removeItem(item.id)}
>
Remove from Cart
</Button>
</CardContent>
</Card>
</Grid>
))}
</Grid>
)}
<Box sx={{ marginTop: 4, display: 'flex', justifyContent: 'space-between' }}>
<Typography variant="h6">Total: ${calculateTotal()}</Typography>
<Link to="/checkout" style={{ textDecoration: 'none' }}>
<Button variant="contained" color="primary">
Proceed to Checkout
</Button>
</Link>
</Box>
</Box>
);
};
export default CartPage;
import React, { useState } from 'react';
import { Button, Box, Typography, Container, TextField, FormControl, InputLabel, Select, MenuItem } from '@mui/material';
const CheckoutPage = () => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [location, setLocation] = useState('');
const [email, setEmail] = useState('');
const handleLogin = () => {
setIsAuthenticated(true);
alert('Logged in!');
};
const handleGuestCheckout = () => {
alert('Proceeding as Guest');
};
const handleLocationChange = (event) => {
setLocation(event.target.value);
};
const handleEmailChange = (event) => {
setEmail(event.target.value);
};
return (
<Container sx={{ paddingY: 4 }}>
<Typography variant="h4" gutterBottom>
Checkout
</Typography>
{isAuthenticated ? (
<Box>
<Typography variant="h6" gutterBottom>
Welcome, User!
</Typography>
</Box>
) : (
<Box>
<TextField
label="Email"
variant="outlined"
fullWidth
value={email}
onChange={handleEmailChange}
sx={{ marginBottom: 2 }}
/>
<Button variant="contained" color="primary" onClick={handleLogin} sx={{ marginBottom: 2 }}>
Login
</Button>
<Button variant="outlined" color="primary" onClick={handleGuestCheckout}>
Checkout as Guest
</Button>
</Box>
)}
{/* Delivery Location */}
<Box sx={{ marginTop: 3 }}>
<Typography variant="h6" gutterBottom>
Delivery Location
</Typography>
<FormControl fullWidth>
<InputLabel>Location</InputLabel>
<Select
value={location}
onChange={handleLocationChange}
label="Location"
>
<MenuItem value="New York">New York</MenuItem>
<MenuItem value="Los Angeles">Los Angeles</MenuItem>
<MenuItem value="San Francisco">San Francisco</MenuItem>
<MenuItem value="Chicago">Chicago</MenuItem>
</Select>
</FormControl>
</Box>
{/* Complete Purchase Button */}
<Box sx={{ marginTop: 2 }}>
<Button variant="contained" color="primary" fullWidth>
Complete Purchase
</Button>
</Box>
</Container>
);
};
export default CheckoutPage;
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { Grid, Card, CardContent, CardMedia, Typography, Button, Box } from '@mui/material';
const HomePage = () => {
const [products, setProducts] = useState([]);
useEffect(() => {
// Simulate fetching products (you can replace this with an actual API call)
setProducts([
{
id: 1,
name: 'Product 1',
description: 'This is a short description for Product 1.',
price: 49.99,
image: 'https://via.placeholder.com/300', // Example image
},
{
id: 2,
name: 'Product 2',
description: 'This is a short description for Product 2.',
price: 99.99,
image: 'https://via.placeholder.com/300', // Example image
},
{
id: 3,
name: 'Product 3',
description: 'This is a short description for Product 3.',
price: 129.99,
image: 'https://via.placeholder.com/300', // Example image
},
]);
}, []);
return (
<Box sx={{ padding: 4 }}>
<Typography variant="h4" gutterBottom>
Welcome to Our Shop!
</Typography>
<Grid container spacing={4}>
{products.map((product) => (
<Grid item xs={12} sm={6} md={4} key={product.id}>
<Card>
<CardMedia component="img" height="200" image={product.image} alt={product.name} />
<CardContent>
<Typography variant="h6">{product.name}</Typography>
<Typography variant="body2" color="text.secondary">
{product.description}
</Typography>
<Typography variant="h5" color="primary">
${product.price}
</Typography>
<Link to={`/product/${product.id}`} style={{ textDecoration: 'none' }}>
<Button variant="contained" color="primary" fullWidth sx={{ marginTop: 2 }}>
View Details
</Button>
</Link>
</CardContent>
</Card>
</Grid>
))}
</Grid>
</Box>
);
};
export default HomePage;
import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { Button, Box, Typography, Card, CardContent, CardMedia, Grid } from '@mui/material';
const ProductPage = () => {
const { id } = useParams(); // Get the product ID from the URL
const [product, setProduct] = useState(null);
const [cart, setCart] = useState([]); // Cart state to manage cart items
const [wishlist, setWishlist] = useState([]); // Wishlist state to manage wishlist items
useEffect(() => {
// Simulate fetching product details (use actual API for real implementation)
const fetchedProduct = {
id: id,
name: `Product ${id}`,
description: `This is a detailed description for Product ${id}.`,
price: (id * 50).toFixed(2),
image: 'https://via.placeholder.com/300',
};
setProduct(fetchedProduct);
}, [id]);
// Add product to cart
const addToCart = () => {
setCart(prevCart => [...prevCart, product]);
alert('Product added to cart!');
};
// Add product to wishlist
const addToWishlist = () => {
setWishlist(prevWishlist => [...prevWishlist, product]);
alert('Product added to wishlist!');
};
if (!product) {
return <Typography>Loading...</Typography>;
}
return (
<Box sx={{ padding: 4 }}>
<Grid container spacing={4}>
<Grid item xs={12} md={6}>
<Card>
<CardMedia
component="img"
height="300"
image={product.image}
alt={product.name}
/>
</Card>
</Grid>
<Grid item xs={12} md={6}>
<Typography variant="h4" gutterBottom>
{product.name}
</Typography>
<Typography variant="body1" paragraph>
{product.description}
</Typography>
<Typography variant="h5" color="primary" paragraph>
${product.price}
</Typography>
<Box sx={{ marginTop: 2 }}>
<Button variant="contained" color="primary" onClick={addToCart}>
Add to Cart
</Button>
<Button
variant="outlined"
color="secondary"
sx={{ marginLeft: 2 }}
onClick={addToWishlist}
>
Add to Wishlist
</Button>
</Box>
</Grid>
</Grid>
</Box>
);
};
export default ProductPage;
import React from 'react';
import { Button, Box, Typography, Card, CardContent, Grid } from '@mui/material';
const WishlistPage = () => {
const wishlistItems = [
{ id: 1, name: 'Product 1', price: 20 },
{ id: 2, name: 'Product 2', price: 30 },
];
const handleAddToCart = (id) => {
// Logic to add item to cart
alert(`Item with id ${id} added to cart`);
};
return (
<Box sx={{ padding: 4 }}>
<Typography variant="h4" gutterBottom>
Your Wishlist
</Typography>
{wishlistItems.map((item) => (
<Card sx={{ marginBottom: 2 }} key={item.id}>
<CardContent>
<Grid container spacing={2}>
<Grid item xs={8}>
<Typography variant="h6">{item.name}</Typography>
<Typography variant="body2">Price: ${item.price}</Typography>
</Grid>
<Grid item xs={4}>
<Button variant="contained" color="primary" onClick={() => handleAddToCart(item.id)}>
Add to Cart
</Button>
</Grid>
</Grid>
</CardContent>
</Card>
))}
</Box>
);
};
export default WishlistPage;
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment