╔══════════════════════════════════════════════════════════════════╗ ║ SHOP FRONTEND ARCHITECTURE OVERVIEW ║ ║ Alpine.js + Jinja2 + Tailwind CSS + Multi-Theme ║ ╚══════════════════════════════════════════════════════════════════╝ 📦 WHAT IS THIS? ═════════════════════════════════════════════════════════════════ Customer-facing shop frontend provides visitors with a branded e-commerce experience unique to each vendor. Built with: ✅ Jinja2 Templates (server-side rendering) ✅ Alpine.js (client-side reactivity) ✅ Tailwind CSS (utility-first styling) ✅ Multi-Theme System (vendor branding) ✅ FastAPI (backend routes) 🎯 KEY PRINCIPLES ═════════════════════════════════════════════════════════════════ 1. Theme-First Design • Each vendor has unique colors, fonts, logos • CSS variables for dynamic theming • Custom CSS support per vendor • Dark mode with vendor colors 2. Progressive Enhancement • Works without JavaScript (basic HTML) • JavaScript adds cart, search, filters • Graceful degradation for older browsers 3. API-First Data Loading • All products from REST APIs • Client-side cart management • Real-time stock updates • Search and filtering 4. Responsive & Mobile-First • Mobile-first Tailwind approach • Touch-friendly interactions • Optimized images • Fast page loads 📁 FILE STRUCTURE ═════════════════════════════════════════════════════════════════ app/ ├── templates/shop/ │ ├── base.html ← Base template (layout) │ ├── home.html ← Homepage / product grid │ ├── product-detail.html ← Single product page │ ├── cart.html ← Shopping cart │ ├── checkout.html ← Checkout flow │ ├── search.html ← Search results │ ├── category.html ← Category browse │ ├── about.html ← About the shop │ ├── contact.html ← Contact form │ └── partials/ ← Reusable components │ ├── product-card.html ← Product display card │ ├── cart-item.html ← Cart item row │ ├── search-modal.html ← Search overlay │ └── filters.html ← Product filters │ ├── static/shop/ │ ├── css/ │ │ ├── shop.css ← Shop-specific styles │ │ └── themes/ ← Optional theme stylesheets │ │ ├── modern.css │ │ ├── minimal.css │ │ └── elegant.css │ ├── js/ │ │ ├── shop-layout.js ← Base shop functionality │ │ ├── home.js ← Homepage logic │ │ ├── product-detail.js ← Product page logic │ │ ├── cart.js ← Cart management │ │ ├── checkout.js ← Checkout flow │ │ ├── search.js ← Search functionality │ │ └── filters.js ← Product filtering │ └── img/ │ ├── placeholder-product.png │ └── empty-cart.svg │ ├── static/shared/ ← Shared across all areas │ ├── js/ │ │ ├── log-config.js ← Logging setup │ │ ├── icons.js ← Icon registry │ │ ├── utils.js ← Utility functions │ │ └── api-client.js ← API wrapper │ └── css/ │ └── base.css ← Global styles │ └── api/v1/shop/ └── pages.py ← Route handlers 🏗️ ARCHITECTURE LAYERS ═════════════════════════════════════════════════════════════════ Layer 1: Routes (FastAPI) ↓ Layer 2: Middleware (Vendor + Theme Detection) ↓ Layer 3: Templates (Jinja2) ↓ Layer 4: JavaScript (Alpine.js) ↓ Layer 5: API (REST endpoints) ↓ Layer 6: Database Layer 1: ROUTES (FastAPI) ────────────────────────────────────────────────────────────────── Purpose: Vendor Detection + Template Rendering Location: app/api/v1/shop/pages.py Example: @router.get("/") async def shop_home( request: Request, db: Session = Depends(get_db) ): vendor = request.state.vendor # From middleware theme = request.state.theme # From middleware return templates.TemplateResponse( "shop/home.html", { "request": request, "vendor": vendor, "theme": theme, } ) Responsibilities: ✅ Access vendor from middleware ✅ Access theme from middleware ✅ Render template ❌ NO database queries (data loaded client-side) ❌ NO business logic Layer 2: MIDDLEWARE ────────────────────────────────────────────────────────────────── Purpose: Vendor & Theme Identification Two middleware components work together: 1. Vendor Context Middleware • Detects vendor from domain/subdomain • Sets request.state.vendor • Returns 404 if vendor not found 2. Theme Context Middleware • Loads theme for detected vendor • Sets request.state.theme • Falls back to default theme Order matters: vendor_context_middleware → theme_context_middleware Layer 3: TEMPLATES (Jinja2) ────────────────────────────────────────────────────────────────── Purpose: HTML Structure + Vendor Branding Location: app/templates/shop/ Template Hierarchy: base.html (layout + theme injection) ↓ home.html (product grid) ↓ partials/product-card.html (components) Example: {% extends "shop/base.html" %} {% block title %}{{ vendor.name }}{% endblock %} {% block alpine_data %}shopHome(){% endblock %} {% block content %}
Loading products...
{% endblock %} Key Features: ✅ Theme CSS variables injection ✅ Vendor logo (light/dark mode) ✅ Custom CSS from theme ✅ Social links from theme ✅ Dynamic favicon Layer 4: JAVASCRIPT (Alpine.js) ────────────────────────────────────────────────────────────────── Purpose: Client-Side Interactivity + Cart + Search Location: app/static/shop/js/ Example (shop-layout.js): function shopLayoutData() { return { dark: false, cartCount: 0, cart: [], init() { this.loadCart(); this.loadThemePreference(); }, addToCart(product, quantity) { // Add to cart logic this.cart.push({ ...product, quantity }); this.saveCart(); }, toggleTheme() { this.dark = !this.dark; localStorage.setItem('shop-theme', this.dark ? 'dark' : 'light'); } }; } Responsibilities: ✅ Load products from API ✅ Manage cart in localStorage ✅ Handle search and filters ✅ Update DOM reactively ✅ Theme toggling Layer 5: API (REST) ────────────────────────────────────────────────────────────────── Purpose: Product Data + Cart + Orders Location: app/api/v1/shop/*.py (not pages.py) Example Endpoints: GET /api/v1/shop/{vendor_code}/products GET /api/v1/shop/{vendor_code}/products/{id} GET /api/v1/shop/{vendor_code}/categories POST /api/v1/shop/{vendor_code}/search POST /api/v1/shop/{vendor_code}/cart/checkout 🔄 DATA FLOW ═════════════════════════════════════════════════════════════════ Page Load Flow: ────────────────────────────────────────────────────────────────── 1. Customer → visits acme-shop.com 2. Vendor Middleware → Identifies "ACME" vendor 3. Theme Middleware → Loads ACME's theme config 4. FastAPI → Renders shop/home.html 5. Browser → Receives HTML with theme CSS variables 6. Alpine.js → init() executes 7. JavaScript → GET /api/v1/shop/ACME/products 8. API → Returns product list JSON 9. Alpine.js → Updates products array 10. Browser → DOM updates with product cards Add to Cart Flow: ────────────────────────────────────────────────────────────────── 1. Customer → Clicks "Add to Cart" 2. Alpine.js → addToCart(product, quantity) 3. Alpine.js → Updates cart array 4. Alpine.js → Saves to localStorage 5. Alpine.js → Updates cartCount badge 6. Alpine.js → Shows toast notification 7. Browser → Cart icon updates automatically Checkout Flow: ────────────────────────────────────────────────────────────────── 1. Customer → Goes to /cart 2. Page → Loads cart from localStorage 3. Customer → Fills checkout form 4. Alpine.js → POST /api/v1/shop/ACME/cart/checkout 5. API → Creates order + payment intent 6. Alpine.js → Redirects to payment 7. Payment → Completes 8. Redirect → /order/{order_id}/confirmation 🎨 MULTI-THEME SYSTEM ═════════════════════════════════════════════════════════════════ How Themes Work: 1. Database Storage • Each vendor has a theme record • Stores colors, fonts, logos, layout prefs • Custom CSS per vendor 2. CSS Variables Injection • base.html injects variables in