# jugalg/p1 architecture

> Architecture diagram of the GitHub repository jugalg/p1, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jugalg/p1
- Repository: https://github.com/jugalg/p1

## Overview

##### Project Type and Purpose
- This is a full-stack (frontend + mock backend) e-commerce web app.
- Frontend: Next.js (App Router) + React + Redux for cart state.
- Mock backend: json-server exposing REST endpoints for users, sessions, products.
- Key features: login/auth flow, protected pages, product listing/details, cart management, quick-view modal, and unit tests.

##### File Structure
- Top-level entries:
- README.md, data-flow.txt (documentation)
- package.json, tsconfig.json, jest.config.js (build/test configs)
- next.config.ts, next-env.d.ts (Next.js config)
- public/ (static assets, GOV.UK styles)
- json-server/ (mock backend: db.json, package.json)
- src/ (application source)
- src/app/: Next.js App Router pages & API routes:
- /api/login, /api/logout, /api/session, /api/products
- UI pages: /login, /homepage, /checkout, dynamic /product/[id]
- src/components/: shared UI pieces (Header/Footer, Cart, ProductCard, QuickViewModal)
- src/lib/features/: Redux slices (cartSlice, productSlice), hooks (useAuth, useCartSync), StoreProvider
- src/lib/api/api.ts: common fetch wrapper
- src/lib/helper: utility logic (sorting)
- src/styles/: local and global CSS
- json-server/db.json: mock data store

##### Extract README Insights
- Auth flow: credentials validated against json-server, cookie + sessionStorage for session.
- API surface: Next.js routes proxy front to JSON-server.
- Redux: holds cart state client-side, persisted via API calls to json-server.
- SSR used for dynamic product routes.

##### Main Components
a. Client Browser
- UI components (Login page, Homepage, Product page, Checkout)
- Redux Store / Provider
- React hooks (useAuth, useCartSync)
- sessionStorage + auth-token cookie
b. Next.js Application (Server + Client)
- App Router pages and layout
- API Routes (/api/*) acting as an abstraction layer
- SSR for product detail routes
c. Mock Backend (json-server)
- Endpoints: /users, /user-data, /products
- db.json data
d. External Services / Tools
- Jest & React Testing Library (for unit tests)
- PostCSS (styling)
- Public asset hosting (fonts, images, manifest)

##### Relationships & Data Flows
- User → Browser UI → Next.js page requests (CSR/SSR)
- Login form → Next.js /api/login → json-server /users → Next.js sets cookie & sessionStorage
- Protected pages → Next.js middleware (API /api/session) → cookie vs. sessionStorage check → redirect if unauthorized
- Product listing/detail:
- Next.js page (SSR or CSR) → /api/products → json-server /products → UI
- Cart operations:
- UI dispatches Redux actions → cartSlice → api.ts → /user-data on json-server → update state & persist
- QuickViewModal triggered client-side; uses existing product data
- Logout → /api/logout → clear cookie & sessionStorage

##### Architectural Patterns & Principles
- Separation of concerns:
- Presentation (components) vs. state management (Redux) vs. API layer.
- Client-server abstraction:
- Next.js API routes isolate the mock backend from UI.
- Protected routes with token/session validation.
- SSR for SEO/performance with dynamic product pages.
- Modular Redux slices following “feature folder” pattern.

##### Relevant Technologies to Annotate
- Next.js App Router
- React + JSX/TSX
- Redux Toolkit (cartSlice, productSlice)
- json-server mock API
- Jest & React Testing Library
- sessionStorage + cookie-based auth
- PostCSS / CSS Modules

##### Diagramming Conventions
- Use clear, descriptive labels (e.g., “Next.js /api/login route”, “Redux cartSlice”).
- UI → Next.js API → json-server → DB reads/writes
- Next.js SSR → product DB → render HTML
- Redux dispatch → API call → state update
- Blue for UI components
- Green for state management (Redux)
- Orange for API layer (Next.js routes)
- Gray for mock backend / database
- Rectangles for services/modules
- Clouds for external/testing tools

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Browser Layer
    subgraph "Frontend Browser" 
        direction TB
        UIPages["UI Pages"]:::ui
        subgraph "Pages" 
            direction TB
            LoginPage["Login Page"]:::ui
            HomepagePage["Homepage"]:::ui
            ProductPage["Product Page"]:::ui
            CheckoutPage["Checkout Page"]:::ui
        end
        UIComponents["Shared Components"]:::ui
        subgraph "Cart Components"
            direction TB
            Cart["Cart"]:::ui
            CartItem["CartItem"]:::ui
            CartWrapper["CartWrapper"]:::ui
        end
        subgraph "Product Card Components"
            direction TB
            ProductData["productData"]:::ui
            ProductGrid["productGrid"]:::ui
        end
        subgraph "Quick-View Modal Components"
            direction TB
            QuickView["ProductQuickView"]:::ui
            QuickViewModal["ProductQuickViewModal"]:::ui
            QuickViewClose["ProductQuickViewModalCloseButton"]:::ui
        end
        ReduxStore["Redux Store"]:::state
        subgraph "Redux Slices & Provider"
            direction TB
            CartSlice["cartSlice"]:::state
            ProductSlice["productSlice"]:::state
            StoreProvider["StoreProvider"]:::state
        end
        subgraph "Custom Hooks"
            direction TB
            UseAuth["useAuth"]:::state
            UseCartSync["useCartSync"]:::state
        end
        APIUtil["api.ts"]:::state
        Helper["sortingLogicHelper"]:::state
        Storage["sessionStorage + auth-cookie"]:::state
    end

    %% Next.js App Layer
    subgraph "Next.js Application (Server + Client)"
        direction TB
        AppLayout["Main App Layout"]:::api
        subgraph "API Routes (Middleware)"
            direction TB
            APILogin["/api/login"]:::api
            APILogout["/api/logout"]:::api
            APISession["/api/session"]:::api
            APIProducts["/api/products"]:::api
        end
        SSR["SSR for Product Page"]:::api
        StaticAssets["Public Assets"]:::api
    end

    %% Mock Backend Layer
    subgraph "Mock Backend (json-server)"
        direction TB
        JSONServer["json-server"]:::backend
        DB["db.json (Data Store)"]:::db
    end

    %% External Tools Layer
    subgraph "External Tools"
        direction TB
        Jest["Jest & Testing Library"]:::external
        PostCSS["PostCSS"]:::external
    end

    %% Connections
    LoginPage -->|submits credentials| APILogin
    APILogin -->|validates| JSONServer
    JSONServer -->|user data| APILogin
    APILogin -->|sets cookie| Storage

    ProductPage -->|request data| SSR
    SSR -->|fetch| APIProducts
    APIProducts --> JSONServer
    APIProducts -->|respond products| SSR
    SSR --> ProductPage

    HomepagePage -->|fetch products| APIProducts
    CheckoutPage -->|protected via| APISession
    APISession -->|validates| Storage

    UIPages -->|renders shared| UIComponents
    UIPages --> ReduxStore
    ReduxStore --> StoreProvider
    StoreProvider --> CartSlice
    StoreProvider --> ProductSlice
    CartSlice --> APIUtil
    ProductSlice --> APIUtil
    APIUtil --> APIProducts
    APIUtil --> APILogin
    APIUtil --> APILogout

    Cart -->|dispatch actions| CartSlice
    CartSlice -->|sync| UseCartSync
    UseCartSync --> APIUtil
    ProductGrid -->|show items| ProductPage
    QuickView -->|open modal| QuickViewModal
    QuickViewModalClose --> QuickViewModal

    Storage --> APISession

    JSONServer --> DB

    AppLayout --> UIPages
    AppLayout --> StaticAssets

    Jest -->|tests| UIComponents
    Jest -->|tests| CartSlice
    PostCSS --> StaticAssets

    %% Click Events
    click APILogin "https://github.com/jugalg/p1/blob/main/src/app/api/login/route.ts"
    click APILogout "https://github.com/jugalg/p1/blob/main/src/app/api/logout/route.ts"
    click APISession "https://github.com/jugalg/p1/blob/main/src/app/api/session/route.ts"
    click APIProducts "https://github.com/jugalg/p1/blob/main/src/app/api/products/route.ts"

    click LoginPage "https://github.com/jugalg/p1/blob/main/src/app/login/page.tsx"
    click HomepagePage "https://github.com/jugalg/p1/blob/main/src/app/homepage/layout.tsx"
    click HomepagePage "https://github.com/jugalg/p1/blob/main/src/app/homepage/page.tsx"
    click CheckoutPage "https://github.com/jugalg/p1/blob/main/src/app/checkout/page.tsx"
    click AppLayout "https://github.com/jugalg/p1/blob/main/src/app/layout.tsx"

    click Header "https://github.com/jugalg/p1/blob/main/src/components/common/Header.tsx"
    click Footer "https://github.com/jugalg/p1/blob/main/src/components/common/Footer.tsx"
    click LogoutButton "https://github.com/jugalg/p1/blob/main/src/components/common/logoutButton.tsx"

    click Cart "https://github.com/jugalg/p1/blob/main/src/components/product/cart/Cart.tsx"
    click CartItem "https://github.com/jugalg/p1/blob/main/src/components/product/cart/CartItem.tsx"
    click CartWrapper "https://github.com/jugalg/p1/blob/main/src/components/product/cart/CartWrapper.tsx"

    click ProductData "https://github.com/jugalg/p1/blob/main/src/components/product/productCard/productData.ts"
    click ProductGrid "https://github.com/jugalg/p1/blob/main/src/components/product/productCard/productGrid.tsx"

    click QuickView "https://github.com/jugalg/p1/blob/main/src/components/product/productCard/productQuickView/ProductQuickView.tsx"
    click QuickViewModal "https://github.com/jugalg/p1/blob/main/src/components/product/productCard/productQuickView/ProductQuickViewModal.tsx"
    click QuickViewClose "https://github.com/jugalg/p1/blob/main/src/components/product/productCard/productQuickView/ProductQuickViewModalCloseButton.tsx"

    click CartSlice "https://github.com/jugalg/p1/blob/main/src/lib/features/cart/cartSlice.ts"
    click ProductSlice "https://github.com/jugalg/p1/blob/main/src/lib/features/product/productSlice.ts"
    click StoreProvider "https://github.com/jugalg/p1/blob/main/src/lib/features/store/StoreProvider.tsx"
    click Store "https://github.com/jugalg/p1/blob/main/src/lib/features/store/store.ts"

    click UseAuth "https://github.com/jugalg/p1/blob/main/src/lib/features/hooks/useAuth.ts"
    click UseCartSync "https://github.com/jugalg/p1/blob/main/src/lib/features/hooks/useCartSync.ts"

    click APIUtil "https://github.com/jugalg/p1/blob/main/src/lib/api/api.ts"
    click Helper "https://github.com/jugalg/p1/blob/main/src/lib/helper/sortingLogicHelper.ts"

    click DB "https://github.com/jugalg/p1/blob/main/json-server/db.json"

    click GlobalCSS "https://github.com/jugalg/p1/blob/main/src/styles/global.css"
    click CartCSS "https://github.com/jugalg/p1/blob/main/src/styles/Cart.css"
    click CartItemCSS "https://github.com/jugalg/p1/blob/main/src/styles/CartItem.css"
    click QuickViewCSS "https://github.com/jugalg/p1/blob/main/src/styles/quick-view.css"
    click SummaryCSS "https://github.com/jugalg/p1/blob/main/src/styles/summary-card.css"

    click PostCSS "https://github.com/jugalg/p1/blob/main/postcss.config.mjs"
    click Jest "https://github.com/jugalg/p1/blob/main/jest.config.js"
    click Jest "https://github.com/jugalg/p1/blob/main/jest.setup.js"
    click Jest "https://github.com/jugalg/p1/blob/main/jest.setup.ts"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0366D6,stroke-width:1px
    classDef state fill:#E6F4EA,stroke:#22863A,stroke-width:1px
    classDef api fill:#FFEFD5,stroke:#FF8C00,stroke-width:1px
    classDef backend fill:#F0F0F0,stroke:#6A737D,stroke-width:1px
    classDef db fill:#EAEAEA,stroke:#6A737D,stroke-width:1px,stroke-dasharray: 5 5
    classDef external fill:#F5F0FF,stroke:#6F42C1,stroke-width:1px
```
