KingNish smolSWE commited on
Commit
5cc830e
·
verified ·
1 Parent(s): 20c9b10

Implemented product details, shopping cart, and checkout functionality (#20)

Browse files

- Implemented product details, shopping cart, and checkout functionality (9a0c1bc34c6da36cfe62a70dbb4bca1b8cb7509a)


Co-authored-by: smolSWE Bot <smolSWE@users.noreply.huggingface.co>

Files changed (8) hide show
  1. cart.html +39 -0
  2. checkout.html +53 -0
  3. confirmation.html +30 -0
  4. index.html +22 -11
  5. product.html +39 -0
  6. script.js +146 -57
  7. style.css +8 -76
  8. tailwind.config.js +15 -0
cart.html ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Shopping Cart</title>
8
+ <link href="style.css" rel="stylesheet">
9
+ </head>
10
+ <body class="bg-gray-100">
11
+ <header class="bg-amazon-blue p-4 text-white">
12
+ <div class="container mx-auto">
13
+ <div class="flex justify-between items-center"><h1 class="text-2xl font-bold">Amazon Dummy</h1><a href="cart.html" class="text-white">Cart</a></div>
14
+ <a href="index.html" class="text-sm">Back to Home</a>
15
+ </div>
16
+ </header>
17
+
18
+ <main class="container mx-auto mt-8">
19
+ <h2 class="text-2xl font-semibold mb-4">Shopping Cart</h2>
20
+
21
+ <div id="cart-items">
22
+ <!-- Cart items will be dynamically added here using JavaScript -->
23
+ </div>
24
+
25
+ <div class="mt-6">
26
+ <h3 class="text-lg font-semibold">Total: <span id="cart-total">0.00</span></h3>
27
+ <a href="checkout.html"><button class="bg-amazon-yellow hover:bg-yellow-500 text-black font-bold py-2 px-4 rounded">Checkout</button></a>
28
+ Checkout
29
+ </button>
30
+ </div>
31
+ </main>
32
+
33
+ <footer class="bg-gray-200 p-4 mt-8 text-center">
34
+ <p>&copy; 2024 Amazon Dummy</p>
35
+ </footer>
36
+
37
+ <script src="script.js"></script>
38
+ </body>
39
+ </html>
checkout.html ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Checkout</title>
8
+ <link href="style.css" rel="stylesheet">
9
+ </head>
10
+ <body class="bg-gray-100">
11
+ <header class="bg-amazon-blue p-4 text-white">
12
+ <div class="container mx-auto">
13
+ <div class="flex justify-between items-center">
14
+ <h1 class="text-2xl font-bold">Amazon Dummy</h1>
15
+ <a href="cart.html" class="text-white">Cart</a>
16
+ </div>
17
+ </div>
18
+ </header>
19
+
20
+ <main class="container mx-auto mt-8">
21
+ <h2 class="text-2xl font-semibold mb-4">Checkout</h2>
22
+
23
+ <form id="checkout-form">
24
+ <div class="mb-4">
25
+ <label for="shipping-address" class="block text-gray-700 text-sm font-bold mb-2">Shipping Address:</label>
26
+ <textarea id="shipping-address" name="shipping-address" rows="4" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"></textarea>
27
+ </div>
28
+
29
+ <div class="mb-4">
30
+ <label for="payment-info" class="block text-gray-700 text-sm font-bold mb-2">Payment Information:</label>
31
+ <input type="text" id="payment-info" name="payment-info" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
32
+ </div>
33
+
34
+ <button type="submit" class="bg-amazon-yellow hover:bg-yellow-500 text-black font-bold py-2 px-4 rounded">
35
+ Place Order
36
+ </button>
37
+ </form>
38
+ </main>
39
+
40
+ <footer class="bg-gray-200 p-4 mt-8 text-center">
41
+ <p>&copy; 2024 Amazon Dummy</p>
42
+ </footer>
43
+ </body>
44
+ </html>
45
+
46
+ <script>
47
+ document.getElementById('checkout-form').addEventListener('submit', function(event) {
48
+ event.preventDefault(); // Prevent the default form submission
49
+
50
+ // Redirect to the confirmation page
51
+ window.location.href = 'confirmation.html';
52
+ });
53
+ </script>
confirmation.html ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Order Confirmation</title>
8
+ <link href="style.css" rel="stylesheet">
9
+ </head>
10
+ <body class="bg-gray-100">
11
+ <header class="bg-amazon-blue p-4 text-white">
12
+ <div class="container mx-auto">
13
+ <div class="flex justify-between items-center">
14
+ <h1 class="text-2xl font-bold">Amazon Dummy</h1>
15
+ <a href="cart.html" class="text-white">Cart</a>
16
+ </div>
17
+ </div>
18
+ </header>
19
+
20
+ <main class="container mx-auto mt-8">
21
+ <h2 class="text-2xl font-semibold mb-4">Order Confirmation</h2>
22
+
23
+ <p class="text-gray-700">Thank you for your order! Your order has been placed successfully.</p>
24
+ </main>
25
+
26
+ <footer class="bg-gray-200 p-4 mt-8 text-center">
27
+ <p>&copy; 2024 Amazon Dummy</p>
28
+ </footer>
29
+ </body>
30
+ </html>
index.html CHANGED
@@ -1,17 +1,28 @@
1
  <!DOCTYPE html>
2
- <html>
3
  <head>
4
- <title>Google Clone</title>
5
- <link rel="stylesheet" href="style.css">
 
 
6
  </head>
7
  <body>
8
- <div class="logo-container"><img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" alt="Google Logo"></div><div class="search-container">
9
- <input type="text" id="search-input" placeholder="Search Google">
10
- <div id="suggestions"></div>
11
- </div>
12
- <div id="results-container">
13
- </div>
14
-
15
- <script src="script.js"></script>
 
 
 
 
 
 
 
 
 
16
  </body>
17
  </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Amazon Dummy</title>
7
+ <link href="style.css" rel="stylesheet">
8
  </head>
9
  <body>
10
+ <header class="bg-amazon-blue">
11
+ <div class="container mx-auto py-2">
12
+ <div class="flex items-center justify-between">
13
+ <div class="text-white font-bold text-2xl">Amazon Dummy</div>
14
+ <div class="flex items-center">
15
+ <input type="text" class="px-4 py-2 rounded-l-md focus:outline-none" placeholder="Search">
16
+ <button class="bg-amazon-orange hover:bg-yellow-500 text-white font-bold py-2 px-4 rounded-r-md">Search</button>
17
+ </div>
18
+ </div>
19
+ </div>
20
+ </header>
21
+ <main class="container mx-auto mt-4">
22
+ <section id="products" class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 gap-4">
23
+ <!-- Products will be dynamically added here -->
24
+ </section>
25
+ </main>
26
+ <script src="script.js"></script>
27
  </body>
28
  </html>
product.html ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Product Details</title>
8
+ <link href="style.css" rel="stylesheet">
9
+ </head>
10
+ <body class="bg-gray-100">
11
+ <header class="bg-amazon-blue p-4 text-white">
12
+ <div class="container mx-auto">
13
+ <div class="flex justify-between items-center"><h1 class="text-2xl font-bold">Amazon Dummy</h1><a href="cart.html" class="text-white">Cart</a></div>
14
+ <a href="index.html" class="text-sm">Back to Home</a>
15
+ </div>
16
+ </header>
17
+
18
+ <main class="container mx-auto mt-8">
19
+ <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
20
+ <div class="md:col-span-1">
21
+ <img src="https://picsum.photos/400/300" alt="Product Image" class="w-full rounded-lg shadow-md">
22
+ </div>
23
+ <div class="md:col-span-1">
24
+ <h2 class="text-2xl font-semibold mb-4">Product Name</h2>
25
+ <p class="text-gray-700 mb-6">
26
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
27
+ </p>
28
+ <button id="add-to-cart-btn" class="bg-amazon-yellow hover:bg-yellow-500 text-black font-bold py-2 px-4 rounded">
29
+ Add to Cart
30
+ </button>
31
+ </div>
32
+ </div>
33
+ </main>
34
+
35
+ <footer class="bg-gray-200 p-4 mt-8 text-center">
36
+ <p>&copy; 2024 Amazon Dummy</p>
37
+ </footer>
38
+ </body>
39
+ </html>
script.js CHANGED
@@ -1,62 +1,151 @@
1
- const searchInput = document.getElementById("search-input");
2
- const suggestionsDiv = document.getElementById("suggestions");
3
- const resultsContainer = document.getElementById("results-container");
4
-
5
- const dummySuggestions = [
6
- "what is javascript",
7
- "how to make a website",
8
- "best restaurants near me",
9
- "weather today",
10
- "current news"
11
- ];
12
-
13
- searchInput.addEventListener("input", function() {
14
- const inputValue = searchInput.value.toLowerCase();
15
- const filteredSuggestions = dummySuggestions.filter(suggestion =>
16
- suggestion.toLowerCase().startsWith(inputValue)
17
- );
18
-
19
- displaySuggestions(filteredSuggestions);
20
- });
21
 
22
- function displaySuggestions(suggestions) {
23
- suggestionsDiv.innerHTML = "";
24
- if (suggestions.length > 0 && searchInput.value) {
25
- suggestions.forEach(suggestion => {
26
- const suggestionElement = document.createElement("div");
27
- suggestionElement.textContent = suggestion;
28
- suggestionElement.addEventListener("click", function() {
29
- searchInput.value = suggestion;
30
- suggestionsDiv.style.display = "none";
31
- displayResults(suggestion);
32
- });
33
- suggestionsDiv.appendChild(suggestionElement);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  });
35
- suggestionsDiv.style.display = "block";
36
- } else {
37
- suggestionsDiv.style.display = "none";
38
- }
 
39
  }
40
 
41
- searchInput.addEventListener("keydown", function(event) {
42
- if (event.key === "Enter") {
43
- const inputValue = searchInput.value;
44
- displayResults(inputValue);
45
- suggestionsDiv.style.display = "none";
46
- }
47
- });
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- function displayResults(query) {
50
- resultsContainer.innerHTML = "";
51
- const dummyResults = [
52
- `Result 1 for ${query}`,
53
- `Result 2 for ${query}`,
54
- `Result 3 for ${query}`
55
- ];
56
-
57
- dummyResults.forEach(result => {
58
- const resultElement = document.createElement("div");
59
- resultElement.textContent = result;
60
- resultsContainer.appendChild(resultElement);
61
- });
62
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
+ // script.js
3
+
4
+ // Function to add a product to the cart
5
+ function addToCart(productName, productPrice) {
6
+ let cart = JSON.parse(localStorage.getItem('cart')) || [];
7
+ let existingProduct = cart.find(item => item.name === productName);
8
+
9
+ if (existingProduct) {
10
+ existingProduct.quantity += 1;
11
+ } else {
12
+ cart.push({
13
+ name: productName,
14
+ price: productPrice,
15
+ quantity: 1
16
+ });
17
+ }
18
+
19
+ localStorage.setItem('cart', JSON.stringify(cart));
20
+ updateCartTotal();
21
+ }
22
+
23
+ // Function to display cart items on cart.html
24
+ function displayCartItems() {
25
+ let cart = JSON.parse(localStorage.getItem('cart')) || [];
26
+ let cartItemsContainer = document.getElementById('cart-items');
27
+ let cartTotalElement = document.getElementById('cart-total');
28
+
29
+ if (cartItemsContainer) {
30
+ cartItemsContainer.innerHTML = ''; // Clear existing items
31
+
32
+ if (cart.length === 0) {
33
+ cartItemsContainer.innerHTML = '<p>Your cart is empty.</p>';
34
+ cartTotalElement.textContent = '0.00';
35
+ return;
36
+ }
37
+
38
+ let total = 0;
39
+
40
+ cart.forEach(item => {
41
+ let itemElement = document.createElement('div');
42
+ itemElement.classList.add('cart-item', 'mb-4', 'p-4', 'border', 'border-gray-300', 'rounded-lg');
43
+
44
+ itemElement.innerHTML = `
45
+ <h4 class="text-lg font-semibold">${item.name}</h4>
46
+ <p>Price: $${item.price.toFixed(2)}</p>
47
+ <div class="flex items-center">
48
+ <label for="quantity-${item.name}" class="mr-2">Quantity:</label>
49
+ <input type="number" id="quantity-${item.name}" name="quantity-${item.name}" value="${item.quantity}" min="1" class="w-20 px-2 py-1 border border-gray-300 rounded">
50
+ <button class="remove-item-btn bg-red-500 hover:bg-red-700 text-white font-bold py-1 px-2 rounded ml-2" data-product="${item.name}">Remove</button>
51
+ </div>
52
+ `;
53
+
54
+ cartItemsContainer.appendChild(itemElement);
55
+ total += item.price * item.quantity;
56
+ });
57
+
58
+ cartTotalElement.textContent = total.toFixed(2);
59
+
60
+ // Add event listeners to quantity inputs and remove buttons
61
+ let quantityInputs = cartItemsContainer.querySelectorAll('input[type="number"]');
62
+ quantityInputs.forEach(input => {
63
+ input.addEventListener('change', updateQuantity);
64
+ });
65
+
66
+ let removeButtons = cartItemsContainer.querySelectorAll('.remove-item-btn');
67
+ removeButtons.forEach(button => {
68
+ button.addEventListener('click', removeItem);
69
+ });
70
+ }
71
+ }
72
+
73
+ // Function to update cart item quantity
74
+ function updateQuantity(event) {
75
+ let productName = event.target.id.replace('quantity-', '');
76
+ let newQuantity = parseInt(event.target.value);
77
+
78
+ if (newQuantity > 0) {
79
+ let cart = JSON.parse(localStorage.getItem('cart')) || [];
80
+ let itemToUpdate = cart.find(item => item.name === productName);
81
+
82
+ if (itemToUpdate) {
83
+ itemToUpdate.quantity = newQuantity;
84
+ localStorage.setItem('cart', JSON.stringify(cart));
85
+ updateCartTotal();
86
+ displayCartItems(); // Re-render the cart items to reflect the updated quantity
87
+ }
88
+ }
89
+ }
90
+
91
+
92
+ // Function to remove item from cart
93
+ function removeItem(event) {
94
+ let productName = event.target.dataset.product;
95
+ let cart = JSON.parse(localStorage.getItem('cart')) || [];
96
+ let updatedCart = cart.filter(item => item.name !== productName);
97
+
98
+ localStorage.setItem('cart', JSON.stringify(updatedCart));
99
+ updateCartTotal();
100
+ displayCartItems(); // Re-render the cart items after removing the item
101
+ }
102
+
103
+ // Function to calculate and update the cart total
104
+ function updateCartTotal() {
105
+ let cart = JSON.parse(localStorage.getItem('cart')) || [];
106
+ let total = 0;
107
+
108
+ cart.forEach(item => {
109
+ total += item.price * item.quantity;
110
  });
111
+
112
+ let cartTotalElement = document.getElementById('cart-total');
113
+ if (cartTotalElement) {
114
+ cartTotalElement.textContent = total.toFixed(2);
115
+ }
116
  }
117
 
118
+ // Add to cart functionality to product.html (example)
119
+ //document.addEventListener('DOMContentLoaded', () => {
120
+ // const addToCartButton = document.querySelector('#add-to-cart-btn'); // Assuming you have an add to cart button with this ID
121
+ // if (addToCartButton) {
122
+ // addToCartButton.addEventListener('click', () => {
123
+ // const productName = 'Product Name'; // Replace with the actual product name
124
+ // const productPrice = 20.00; // Replace with the actual product price
125
+ // addToCart(productName, productPrice);
126
+ // });
127
+ // }
128
+ //});
129
+
130
+ // Call displayCartItems() when cart.html loads
131
+ if (document.body.id === 'cart-page') {
132
+ displayCartItems();
133
+ }
134
+
135
+ // Update cart total on page load
136
+ document.addEventListener('DOMContentLoaded', updateCartTotal);
137
 
138
+
139
+ // Add to cart functionality to product.html
140
+ document.addEventListener('DOMContentLoaded', () => {
141
+ // Set the ID of the body
142
+ document.body.id = 'product-page';
143
+ const addToCartButton = document.querySelector('#add-to-cart-btn'); // Assuming you have an add to cart button with this ID
144
+ if (addToCartButton) {
145
+ addToCartButton.addEventListener('click', () => {
146
+ const productName = 'Product Name'; // Replace with the actual product name
147
+ const productPrice = 20.00; // Replace with the actual product price
148
+ addToCart(productName, productPrice);
149
+ });
150
+ }
151
+ });
style.css CHANGED
@@ -1,79 +1,11 @@
 
 
 
1
 
2
- body {
3
- font-family: Arial, sans-serif;
4
- margin: 0;
5
- padding: 0;
6
- display: flex;
7
- flex-direction: column;
8
- align-items: center;
9
- min-height: 100vh; /* Use min-height for better centering */
10
  }
11
 
12
- .logo-container {
13
- margin-top: 100px; /* Adjust as needed */
14
- margin-bottom: 20px; /* Space between logo and search bar */
15
- text-align: center;
16
- }
17
-
18
- .logo-container img {
19
- width: auto; /* Maintain aspect ratio */
20
- height: 92px; /* Google logo height */
21
- }
22
-
23
- .search-container {
24
- /* margin-top: 100px; */ /* Removed, logo container handles top margin */
25
- position: relative;
26
- }
27
-
28
- #search-input {
29
- width: 580px; /* Increased width slightly */
30
- padding: 13px 20px; /* Adjusted padding */
31
- font-size: 16px;
32
- border: 1px solid #dfe1e5; /* Softer border color */
33
- border-radius: 24px;
34
- outline: none;
35
- box-shadow: 0 1px 6px rgb(32 33 36 / 28%); /* Add a subtle shadow */
36
- transition: box-shadow 0.3s ease-in-out; /* Smooth transition */
37
- }
38
-
39
- #search-input:focus {
40
- border-color: #adadad; /* Slightly darker border on focus */
41
- box-shadow: 0 1px 6px rgb(32 33 36 / 28%); /* Keep shadow on focus */
42
- }
43
-
44
-
45
- #suggestions {
46
- position: absolute;
47
- top: 100%;
48
- left: 0;
49
- width: calc(580px + 40px); /* Match input width + padding */
50
- background-color: #fff;
51
- border: 1px solid #dfe1e5;
52
- border-top: none;
53
- border-radius: 0 0 8px 8px;
54
- box-shadow: 0 4px 6px rgb(32 33 36 / 10%); /* Softer shadow */
55
- display: none;
56
- z-index: 1;
57
- }
58
-
59
- #suggestions div {
60
- padding: 10px 16px; /* Adjusted padding */
61
- cursor: pointer;
62
- font-size: 14px;
63
- }
64
-
65
- #suggestions div:hover {
66
- background-color: #f0f0f0;
67
- }
68
-
69
- #results-container {
70
- margin-top: 40px; /* Increased margin for results */
71
- width: 600px; /* Adjusted width */
72
- }
73
-
74
- #results-container div {
75
- margin-bottom: 15px; /* Space between results */
76
- padding: 10px 0;
77
- border-bottom: 1px solid #eee; /* Separator line */
78
- font-size: 14px;
79
- }
 
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
4
 
5
+ .bg-amazon-blue {
6
+ background-color: #232F3E;
 
 
 
 
 
 
7
  }
8
 
9
+ .bg-amazon-orange {
10
+ background-color: #FF9900;
11
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tailwind.config.js ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /** @type {import('tailwindcss').Config} */
2
+ module.exports = {
3
+ content: ["./*.{html,js}"],
4
+ theme: {
5
+ extend: {
6
+ colors: {
7
+ amazon: {
8
+ blue: '#232F3E',
9
+ orange: '#FF9900',
10
+ }
11
+ }
12
+ },
13
+ },
14
+ plugins: [],
15
+ }