diff --git a/.gitignore b/.gitignore index a547bf3..91c47b6 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ dist-ssr *.njsproj *.sln *.sw? +.aider* diff --git a/src/App.jsx b/src/App.jsx index 3a129b9..454c07b 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -2,6 +2,7 @@ import React from 'react'; import ProductList from './Components/ProductList'; import ShoppingCart from './Components/ShoppingCart'; +import SuperCoin from './Components/SuperCoin'; import './App.css' const App = () => { return ( @@ -10,6 +11,7 @@ const App = () => {

E-Commerce Application

+ ); diff --git a/src/Components/CartSlice.jsx b/src/Components/CartSlice.jsx index f05ba44..ba09201 100644 --- a/src/Components/CartSlice.jsx +++ b/src/Components/CartSlice.jsx @@ -1,6 +1,54 @@ +import { createSlice } from '@reduxjs/toolkit'; + +const initialState = { + cartItems: [], + }; + +const CartSlice = createSlice({ + name: 'cart', + initialState, + reducers: { + addItemToCart(state, action) { + const existingItem = state.cartItems.find(item => item.id === action.payload.id); + if (existingItem) { + existingItem.quantity += 1; + } else { + state.cartItems.push({ ...action.payload, quantity: 1 }); + } + }, + + removeItemFromCart(state, action) { + state.cartItems = state.cartItems.filter(item => item.id !== action.payload); + }, + + clearCart(state) { + state.cartItems = []; + }, + + increaseItemQuantity(state, action) { + const itemToIncrease = state.cartItems.find(item => item.id === action.payload); + if (itemToIncrease) { + itemToIncrease.quantity += 1; + } + }, + + decreaseItemQuantity(state, action) { + const itemToDecrease = state.cartItems.find(item => item.id === action.payload); + if (itemToDecrease && itemToDecrease.quantity > 1) { + itemToDecrease.quantity -= 1; + } + }, + } -const CartSlice = ({ - }); +export const { + addItemToCart, + removeItemFromCart, + clearCart, + increaseItemQuantity, + decreaseItemQuantity, + } = CartSlice.actions; + export default CartSlice.reducer; + diff --git a/src/Components/ProductList.jsx b/src/Components/ProductList.jsx index 33b4977..a21b9ba 100644 --- a/src/Components/ProductList.jsx +++ b/src/Components/ProductList.jsx @@ -1,7 +1,12 @@ import React from 'react'; +import { useDispatch } from 'react-redux'; +import { useState } from 'react'; +import { addItemToCart } from './CartSlice'; import './ProductList.css'; const ProductList = () => { + const dispatch = useDispatch(); + const [disabledProducts, setDisabledProducts] = useState([]); // State to store disabled products const products = [ { id: 1, name: 'Product A', price: 60 }, @@ -9,12 +14,28 @@ const ProductList = () => { { id: 3, name: 'Product C', price: 30 }, ]; + const handleAddToCart = product => { + dispatch(addItemToCart(product)); + setDisabledProducts([...disabledProducts, product.id]); // Mark the product as disabled + }; + return (

Products

    - -
+ {products.map(product => ( +
  • + {product.name} - ${product.price} + +
  • + ))} +
    ); }; diff --git a/src/Components/ShoppingCart.jsx b/src/Components/ShoppingCart.jsx index 5344038..c9d49e1 100644 --- a/src/Components/ShoppingCart.jsx +++ b/src/Components/ShoppingCart.jsx @@ -1,18 +1,51 @@ import React from 'react'; -import './ShoppingCart.css'; +import { useDispatch, useSelector } from 'react-redux'; +import { removeItemFromCart, clearCart, increaseItemQuantity, decreaseItemQuantity } from './CartSlice'; // Assuming you have action creators for increasing and decreasing item quantity +import './ShoppingCart.css'; const ShoppingCart = () => { + const dispatch = useDispatch(); + const cartItems = useSelector(state => state.cart.cartItems); + const totalAmount = cartItems.reduce((total, item) => total + item.price * item.quantity, 0); + + const handleRemoveItem = itemId => { + dispatch(removeItemFromCart(itemId)); + }; + + const handleClearCart = () => { + dispatch(clearCart()); + }; + + const handleIncreaseQuantity = itemId => { + dispatch(increaseItemQuantity(itemId)); + }; + + const handleDecreaseQuantity = itemId => { + dispatch(decreaseItemQuantity(itemId)); + }; + + return ( <>

    Shopping Cart

      - + {cartItems.map(item => ( +
    • + {item.name} - ${item.price} +
      + + {item.quantity} + +
      + +
    • + ))}
    - +
    - +
    {totalAmount ?
    'The total amount is {totalAmount}
    : ''}
    ); }; diff --git a/src/Components/SuperCoin.css b/src/Components/SuperCoin.css new file mode 100644 index 0000000..45318eb --- /dev/null +++ b/src/Components/SuperCoin.css @@ -0,0 +1,36 @@ +.super-coins-container { + border: 2px solid #ffd700; + border-radius: 8px; + padding: 20px; + margin: 20px; + background-color: #fffdf0; +} + +.super-coins-info { + text-align: center; +} + +.coins-display { + margin: 15px 0; + font-size: 24px; +} + +.coin-icon { + font-size: 32px; + margin-right: 10px; +} + +.coin-amount { + font-weight: bold; + color: #ffa500; +} + +.cart-details { + text-align: left; + margin-top: 15px; +} + +.cart-details p { + margin: 5px 0; + color: #666; +} diff --git a/src/Components/SuperCoin.jsx b/src/Components/SuperCoin.jsx new file mode 100644 index 0000000..be566f9 --- /dev/null +++ b/src/Components/SuperCoin.jsx @@ -0,0 +1,41 @@ +import React, { useState, useEffect } from 'react'; +import { useSelector } from 'react-redux'; +import './SuperCoin.css'; + +const SuperCoin = () => { + const [superCoins, setSuperCoins] = useState(0); + const cartItems = useSelector(state => state.cart.cartItems); + const totalQuantity = cartItems.reduce((total, item) => total + item.quantity, 0); + const totalAmount = cartItems.reduce((total, item) => total + item.price * item.quantity, 0); + + useEffect(() => { + if (totalAmount < 100) { + setSuperCoins(0); + } else if (totalAmount >= 100 && totalAmount < 200) { + setSuperCoins(10); + } else if (totalAmount >= 200 && totalAmount < 300) { + setSuperCoins(20); + } else { + setSuperCoins(30); + } + }, [totalAmount]); + + return ( +
    +
    +

    Super Coins Rewards

    +
    + 🪙 + {superCoins} +
    +
    +

    Total Items in Cart: {totalQuantity}

    +

    Total Amount: ${totalAmount}

    +

    Super Coins Earned: {superCoins}

    +
    +
    +
    + ); +}; + +export default SuperCoin; diff --git a/src/main.jsx b/src/main.jsx index 7bc0fd8..1a56c63 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -2,8 +2,12 @@ import React from 'react' import ReactDOM from 'react-dom/client' import App from './App.jsx' import './index.css' +import { Provider } from 'react-redux' +import store from './store.js' ReactDOM.createRoot(document.getElementById('root')).render( + + , -) +) \ No newline at end of file diff --git a/src/store.js b/src/store.js new file mode 100644 index 0000000..563b337 --- /dev/null +++ b/src/store.js @@ -0,0 +1,9 @@ +import { configureStore } from '@reduxjs/toolkit'; +import cartReducer from './Components/CartSlice'; +const store = configureStore({ + reducer: { + cart: cartReducer, + }, +}); + +export default store; \ No newline at end of file