Skip to content

PriyanshuBh/E-Com-Cart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

E-Commerce Cart Application

A modern, full-stack e-commerce application built with Next.js, React, MongoDB, and Tailwind CSS. This project demonstrates a complete shopping experience with product browsing, cart management, and checkout functionality.


E-Commerce Cart Application

Table of Contents


Features

βœ… Product Catalog - Browse a responsive grid of products with descriptions and prices βœ… Shopping Cart - Add, remove, and adjust quantities of items βœ… Cart Persistence - Cart state managed in client-side memory βœ… Checkout System - Collect customer information and process orders βœ… Order Receipts - Display confirmation with order ID after successful checkout βœ… MongoDB Integration - Persistent storage for products and orders βœ… Responsive Design - Mobile, tablet, and desktop optimized layouts βœ… Type Safety - Full TypeScript support throughout the application βœ… Accessible Components - ShadCN UI components with built-in accessibility βœ… Real-time Updates - Instant cart total calculations and UI updates


Tech Stack

Frontend

  • Next.js 15 - React-based full-stack framework with App Router
  • React 19 - UI library for interactive components
  • TypeScript - Static type checking and better developer experience
  • Tailwind CSS v4 - Utility-first CSS framework for styling
  • ShadCN/UI - Pre-built, accessible UI components (Button, Card, Modal, etc.)
  • Lucide React - Icon library for UI elements

Backend

  • Next.js API Routes - Built-in serverless API endpoints
  • Node.js Runtime - JavaScript runtime for server-side logic

Database

  • MongoDB - NoSQL database for flexible data storage
  • Mongoose - ODM (Object Document Mapper) for schema definition and validation

Build & Deployment

  • Vercel - Recommended deployment platform
  • GitHub - Version control and deployment integration

Project Structure

``` e-commerce-cart-app/ β”œβ”€β”€ app/ β”‚ β”œβ”€β”€ page.tsx # Main page with cart state management β”‚ β”œβ”€β”€ layout.tsx # Root layout with metadata β”‚ β”œβ”€β”€ globals.css # Global styles and design tokens β”‚ └── api/ β”‚ β”œβ”€β”€ products/ β”‚ β”‚ └── route.ts # GET /api/products endpoint β”‚ β”œβ”€β”€ checkout/ β”‚ β”‚ └── route.ts # POST /api/checkout endpoint β”‚ └── cart/ β”‚ └── [id]/ β”‚ └── route.ts # Cart item operations (future use) β”œβ”€β”€ components/ β”‚ β”œβ”€β”€ header.tsx # Navigation bar with cart icon β”‚ β”œβ”€β”€ products-grid.tsx # Responsive product display grid β”‚ β”œβ”€β”€ cart-sidebar.tsx # Slide-out shopping cart panel β”‚ └── checkout-modal.tsx # Checkout form and receipt modal β”œβ”€β”€ models/ β”‚ β”œβ”€β”€ Product.ts # Product schema with validation β”‚ └── Order.ts # Order schema with timestamps β”œβ”€β”€ lib/ β”‚ └── mongodb.ts # MongoDB connection with singleton pattern β”œβ”€β”€ scripts/ β”‚ └── seed-products.ts # Script to populate sample products └── README.md # This file ```


Prerequisites

Before running this application, ensure you have:


Setup Instructions

Step 1: Environment Configuration

The application requires a MongoDB connection string. Add it as an environment variable:

Connection String Examples:

  • MongoDB Atlas (Cloud): ``` mongodb+srv://username:password@cluster-name.mongodb.net/dbname?retryWrites=true&w=majority ```

  • Local MongoDB: ``` mongodb://localhost:27017/ecommerce ```

Step 2: Deployment Options

Option A: Deploy to Vercel (Recommended)

  1. Click the Publish button in the top right corner
  2. Follow the prompts to authenticate with GitHub
  3. Select repository settings
  4. Vercel will automatically configure environment variables
  5. Your app will be live in seconds!

Option B: Run Locally via GitHub

  1. Click the GitHub icon in the top right

  2. Push code to your GitHub repository

  3. Clone locally: ```bash git clone https URL cd ecommerce-cart-app ```

  4. Install dependencies: ```bash npm install ```

  5. Create .env.local file in the root: ``` MONGODB_URI=your_mongodb_connection_string ```

  6. Run development server: ```bash npm run dev ```

  7. Open http://localhost:3000 in your browser

Option C: Download as ZIP

  1. Click the three dots (...) in the top right
  2. Select "Download ZIP"
  3. Extract the archive
  4. Follow the GitHub setup steps 4-7 above

Running the Application

Development Mode

```bash npm run dev ``` Server runs on http://localhost:3000 with hot reload enabled.

Production Build

```bash npm run build npm start ```

Seed Database with Sample Products

The application includes 8 sample products (headphones, cables, cases, etc.).

  1. Go to the /scripts folder
  2. Click the Execute button on seed-products.ts
  3. Check your MongoDB to see the products created

Locally: ```bash npx ts-node --compiler-options '{"module":"CommonJS"}' scripts/seed-products.ts ```


Database Models

Product Schema

Stores all available products for sale.

```typescript { _id: ObjectId, name: String (required), price: Number (required), description: String (optional), stock: Number (default: 100), createdAt: Date, updatedAt: Date } ```

Example: ```json { "_id": "507f1f77bcf86cd799439011", "name": "Wireless Headphones", "price": 79.99, "description": "Premium noise-cancelling headphones with 30-hour battery", "stock": 50, "createdAt": "2024-01-15T10:30:00Z" } ```

Order Schema

Stores all customer orders and transactions.

```typescript { _id: ObjectId, name: String (required), email: String (required), items: [ { productId: String, name: String, price: Number, quantity: Number } ], total: Number (required), createdAt: Date, updatedAt: Date } ```

Example: ```json { "_id": "507f1f77bcf86cd799439012", "name": "John Doe", "email": "john@example.com", "items": [ { "productId": "507f1f77bcf86cd799439011", "name": "Wireless Headphones", "price": 79.99, "quantity": 1 } ], "total": 79.99, "createdAt": "2024-01-15T11:45:00Z" } ```


API Endpoints

GET /api/products

Retrieves all available products from the database.

Request: ```bash GET /api/products ```

Response (200 OK): ```json [ { "_id": "507f1f77bcf86cd799439011", "name": "Wireless Headphones", "price": 79.99, "description": "Premium noise-cancelling headphones", "stock": 50, "createdAt": "2024-01-15T10:30:00Z", "updatedAt": "2024-01-15T10:30:00Z" } ] ```

Error Response (500 Internal Server Error): ```json { "error": "Failed to fetch products" } ```


POST /api/checkout

Processes a checkout request and creates an order in the database.

Request Body: ```json { "cartItems": [ { "productId": "507f1f77bcf86cd799439011", "name": "Wireless Headphones", "price": 79.99, "quantity": 1 } ], "name": "John Doe", "email": "john@example.com" } ```

Response (200 OK): ```json { "orderId": "507f1f77bcf86cd799439012", "name": "John Doe", "email": "john@example.com", "items": [ { "name": "Wireless Headphones", "price": 79.99, "quantity": 1 } ], "total": 79.99, "message": "Order placed successfully!" } ```

Error Response (400 Bad Request): ```json { "error": "Missing required fields: cartItems, name, email" } ```


Component Overview

Header Component

Displays the application logo and shopping cart icon with item count badge.

Props:

  • cartCount: number - Number of items in cart
  • onCartClick: () => void - Callback when cart icon is clicked

Features:

  • Responsive navigation bar
  • Cart count badge
  • Click to open/close cart

ProductsGrid Component

Displays all products in a responsive grid layout.

Features:

  • Fetches products from /api/products on mount
  • Responsive grid: 1 column (mobile) β†’ 2 columns (tablet) β†’ 4 columns (desktop)
  • Loading spinner while fetching
  • Add to cart button for each product
  • Product details: name, description, price

CartSidebar Component

Slide-out panel showing current cart items and checkout option.

Features:

  • Slide-in animation from the right
  • Display all items with quantity controls
  • Increase/decrease quantity buttons
  • Remove item button
  • Cart total calculation
  • Checkout button
  • Empty cart state message

CheckoutModal Component

Modal form for collecting customer information and displaying order receipt.

States:

  • Form State - Collect name and email
  • Receipt State - Show order confirmation with details

Features:

  • Input validation for name and email
  • Displays cart summary
  • Shows calculated total
  • Generate order ID on successful checkout
  • Confirmation message with order details

Usage Guide

1. Browsing Products

  • The homepage displays all available products in a grid
  • Scroll through products and view descriptions and prices
  • Products load automatically when the page loads

2. Adding Items to Cart

  • Click the "Add to Cart" button on any product
  • Select desired quantity
  • Item is added to cart (cart counter updates in header)

3. Viewing Cart

  • Click the cart icon in the header
  • Cart sidebar slides in from the right
  • Shows all items with individual prices and quantities

4. Managing Cart Items

  • Increase Quantity: Click the + button next to an item
  • Decrease Quantity: Click the βˆ’ button next to an item
  • Remove Item: Click the trash icon next to an item
  • Cart Total: Updates automatically as you modify items

5. Checkout Process

  • Click the "Proceed to Checkout" button in the cart sidebar
  • Enter your full name
  • Enter your email address
  • Review order summary and total
  • Click "Complete Order" button

6. Order Confirmation

  • After successful checkout, you'll see an order receipt modal
  • Receipt displays:
    • Order ID
    • Your name and email
    • Itemized list of products with quantities
    • Order total
  • Close the modal to return to shopping

Future Enhancements

Potential features for expanding the application:

  • User Authentication - Login/signup with Supabase or NextAuth
  • Product Filtering & Search - Search by name, filter by price range
  • Product Reviews - Customer ratings and comments
  • Wishlist Feature - Save favorite items
  • Payment Integration - Stripe or PayPal integration
  • Order History - Track customer orders
  • Admin Dashboard - Manage products and orders
  • Product Images - Display product photos in grid
  • Stock Management - Update inventory after checkout
  • Email Notifications - Send order confirmation emails
  • Multi-currency Support - Display prices in different currencies

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository on GitHub
  2. Create a feature branch (git checkout -b feature/YourFeature)
  3. Commit your changes (git commit -m 'Add your feature')
  4. Push to the branch (git push origin feature/YourFeature)
  5. Open a Pull Request

License

This project is open source and available under the MIT License.

Getting Help


Created with Next.js, React, MongoDB, and Tailwind CSS

Happy shopping! πŸ›’

About

A modern, full-stack e-commerce application demonstrates a complete shopping experience with product browsing, cart management, and checkout functionality.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors