# amarnaath05/the-bookshelf architecture

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

- Interactive diagram: https://gitdiagram.com/amarnaath05/the-bookshelf
- Repository: https://github.com/amarnaath05/the-bookshelf

## Overview

##### Project type and purpose
- This is a full-stack e-commerce bookstore web application.
- Frontend: React.js SPA styled with Tailwind CSS and React Router for navigation.
- Backend: A mock API server implemented with MirageJS (controllers, in-memory “db”).
- Purpose: allow users to browse books by category, search, authenticate, add to wishlist/cart, checkout.

##### High-level file-structure analysis
- public/: static HTML and manifest.
- src/
- backend/: MirageJS mock-server
– db/: in-memory data sets (users, products, categories)
– controllers/: REST handlers (AuthController, ProductController, CartController, etc.)
– utils/: authentication helpers (JWT validation, token utilities)
- services/: client-side wrappers for API & localStorage interactions (auth-service, cart-service, etc.)
- contexts/: React Context providers and reducers for global state (Auth, Books, Filters, Address)
- components/: reusable UI pieces (product cards, buttons, loader, navbar, route guards)
- pages/: route-level containers (Home, Login, Products, Cart, Checkout, Thank You, etc.)
- layouts/: top-level layouts (RootLayout, ProductLayout)
- App.js + index.js: application entrypoint and router setup

##### Key insights from README
- Uses JWT-based token authentication.
- MirageJS simulates API endpoints for all CRUD operations.
- State flows through Context + Reducer + Services pattern.
- Tailwind CSS ensures responsive design.
- React Router DOM enforces protected routes via CanActivate.

##### Components
A. Frontend Layer
1. Browser / Client Application
– React SPA (App.js)
– React Router (public & protected routes)
2. UI Components & Layouts
– components/ & pages/
3. State Management
– Context Providers (AuthProvider, BooksProvider, AddressProvider)
– Reducers (Books, Filters)
4. Services Layer (API clients)
– auth-service.js, products-service.js, cart-service.js, wishlist-service.js, etc.
5. Local Storage Adapter
– localstorage-service.js

B. Backend (Mock API) Layer
1. MirageJS Server (initialized in src/server.js)
2. Controllers
– AuthController (login, signup, token management)
– ProductController (fetch, filter, search)
– CategoryController
– CartController
– WishlistController
3. In-Memory DB
– Collections: users, products, categories

C. External Dependencies
– JWT Decode
– UUID (for ID generation)
– Day.js (date formatting)
– React Hot Toast (notifications)

##### Relationships & Data Flows
1. User → Browser: interacts with UI (clicks, form submissions).
2. UI Component → Service: calls service method (e.g., productsService.getProducts()).
3. Service → MirageJS API: issues HTTP request.
4. Mirage Controller → Database: CRUD on in-memory collections.
5. Controller → Service: returns JSON payload.
6. Service → Context Reducer: dispatches action to update global state.
7. State → UI: components re-render with new data.
8. For protected routes: React Router → CanActivate → checks AuthContext token validity → allow/redirect.

##### Architectural patterns & principles
- Component-based UI (React)
- Context + Reducer for centralized state
- Service layer abstracts API calls
- MVC-style structure in mock backend (controllers + db)
- Clear separation of concerns
- Single-page application model with client-side routing

##### Detailed elements to capture
- Entrypoint: index.js → App.js
- Router configuration with protected route guard.
- Context providers wrapping App.
- Key pages (Home, ProductOverview, Cart, Checkout, Login/Signup).
- Service calls and corresponding controllers.
- In-memory DB schemas (collections).
- Utility modules (authUtils, localstorage-service).
- External libraries that impact architecture (MirageJS, JWT Decode).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        INDEX["Index.js"]:::frontend
        APP["App.js"]:::frontend
        ROUTER["React Router"]:::frontend
        CANACT["CanActivate (Route Guard)"]:::frontend
        subgraph "UI Components & Pages"
            direction TB
            UI["Components"]:::frontend
            PAGES["Pages"]:::frontend
            LAYOUTS["Layouts"]:::frontend
        end
        subgraph "State Management"
            direction TB
            AUTHPROV["AuthProvider"]:::frontend
            BOOKSPROV["BooksProvider"]:::frontend
            ADDRESSPROV["AddressProvider"]:::frontend
            subgraph "Reducers"
                direction TB
                BOOKSRED["Books Reducer"]:::frontend
                FILTERSRED["Filters Reducer"]:::frontend
            end
            subgraph "Initial States"
                direction TB
                AUTHINIT["Auth Initial State"]:::frontend
                BOOKSINIT["Books Initial State"]:::frontend
                FILTERINIT["Filter Initial State"]:::frontend
            end
        end
    end

    %% Services Layer
    subgraph "Services Layer"
        direction TB
        AUTHSVC["AuthService"]:::services
        PRODSSVC["ProductsService"]:::services
        CARTSVC["CartService"]:::services
        WISHLISTSVC["WishlistService"]:::services
        SIGNUPSVC["SignupService"]:::services
        LOGINSVC["LoginService"]:::services
        LOCALSSVC["LocalStorageService"]:::services
    end

    %% Mock Backend Layer
    subgraph "Mock Backend (MirageJS)" 
        direction TB
        SERVER["MirageJS Server"]:::backend
        subgraph "Controllers"
            direction TB
            AUTHCTRL["AuthController"]:::backend
            PRODCTRL["ProductController"]:::backend
            CATCTRL["CategoryController"]:::backend
            CARTCTRL["CartController"]:::backend
            WISHCTRL["WishlistController"]:::backend
        end
        subgraph "In-Memory DB"
            direction TB
            USERSDB["Users Collection"]:::datastore
            PRODSDB["Products Collection"]:::datastore
            CATDB["Categories Collection"]:::datastore
        end
        AUTHCTRL -->|reads/writes| USERSDB
        PRODCTRL -->|reads/writes| PRODSDB
        CATCTRL -->|reads/writes| CATDB
        CARTCTRL -->|reads/writes| USERSDB
        WISHCTRL -->|reads/writes| USERSDB
    end

    %% Utilities
    AUTHUTIL["authUtils.js"]:::backend

    %% External Dependencies
    subgraph "External Libraries"
        direction TB
        JWT["JWT Decode"]:::external
        UUID["UUID"]:::external
        DAYJS["Day.js"]:::external
        TOAST["React Hot Toast"]:::external
    end

    %% Connections
    INDEX --> APP
    APP --> ROUTER
    ROUTER --> CANACT
    CANACT --> PAGES
    ROUTER --> PAGES
    PAGES --> UI
    UI --> SERVICES_LAYER
    LAYOUTS --> UI
    INDEX --> AUTHPROV
    INDEX --> BOOKSPROV
    INDEX --> ADDRESSPROV
    AUTHPROV --> BOOKSPROV
    BOOKSPROV --> ADDRESSPROV
    ADDRESSPROV --> APP
    AUTHPROV --> BOOKSRED
    BOOKSPROV --> BOOKSRED
    ADDRESSPROV --> FILTERSRED
    BOOKSRED --> AUTHINIT
    BOOKSRED --> BOOKSINIT
    FILTERSRED --> FILTERINIT

    %% Service calls
    UI -->|calls| AUTHSVC
    UI -->|calls| PRODSSVC
    UI -->|calls| CARTSVC
    UI -->|calls| WISHLISTSVC
    UI -->|calls| SIGNUPSVC
    UI -->|calls| LOGINSVC
    UI -->|uses| LOCALSSVC

    AUTHSVC -->|HTTP| SERVER
    PRODSSVC -->|HTTP| SERVER
    CARTSVC -->|HTTP| SERVER
    WISHLISTSVC -->|HTTP| SERVER
    SIGNUPSVC -->|HTTP| SERVER
    LOGINSVC -->|HTTP| SERVER

    SERVER --> AUTHCTRL
    SERVER --> PRODCTRL
    SERVER --> CATCTRL
    SERVER --> CARTCTRL
    SERVER --> WISHCTRL
    AUTHCTRL --> AUTHUTIL

    %% External deps usage
    AUTHUTIL --> JWT
    AUTHUTIL --> UUID
    PRODCTRL --> DAYJS
    UI --> TOAST

    %% Click Events
    click INDEX "https://github.com/amarnaath05/the-bookshelf/blob/main/src/index.js"
    click APP "https://github.com/amarnaath05/the-bookshelf/blob/main/src/App.js"
    click UI "https://github.com/amarnaath05/the-bookshelf/tree/main/src/components/"
    click LAYOUTS "https://github.com/amarnaath05/the-bookshelf/blob/main/src/layouts/RootLayout.jsx"
    click LAYOUTS "https://github.com/amarnaath05/the-bookshelf/blob/main/src/layouts/ProductLayout.jsx"
    click PAGES "https://github.com/amarnaath05/the-bookshelf/tree/main/src/pages/"
    click CANACT "https://github.com/amarnaath05/the-bookshelf/blob/main/src/components/routegurad/CanActivate.js"
    click AUTHPROV "https://github.com/amarnaath05/the-bookshelf/blob/main/src/contexts/AuthProvider.js"
    click BOOKSPROV "https://github.com/amarnaath05/the-bookshelf/blob/main/src/contexts/BooksProvider.js"
    click ADDRESSPROV "https://github.com/amarnaath05/the-bookshelf/blob/main/src/contexts/AddressProvider.js"
    click BOOKSRED "https://github.com/amarnaath05/the-bookshelf/blob/main/src/contexts/reducers/Books.jsx"
    click FILTERSRED "https://github.com/amarnaath05/the-bookshelf/blob/main/src/contexts/reducers/Filters.jsx"
    click AUTHINIT "https://github.com/amarnaath05/the-bookshelf/blob/main/src/contexts/initialStates/AuthInitialState.js"
    click BOOKSINIT "https://github.com/amarnaath05/the-bookshelf/blob/main/src/contexts/initialStates/BooksInitialState.js"
    click FILTERINIT "https://github.com/amarnaath05/the-bookshelf/blob/main/src/contexts/initialStates/FilterInitialState.js"
    click AUTHSVC "https://github.com/amarnaath05/the-bookshelf/blob/main/src/services/auth-service.js"
    click PRODSSVC "https://github.com/amarnaath05/the-bookshelf/blob/main/src/services/products-service.js"
    click CARTSVC "https://github.com/amarnaath05/the-bookshelf/blob/main/src/services/cart-service.js"
    click WISHLISTSVC "https://github.com/amarnaath05/the-bookshelf/blob/main/src/services/wishlist-service.js"
    click SIGNUPSVC "https://github.com/amarnaath05/the-bookshelf/blob/main/src/services/signup-service.js"
    click LOGINSVC "https://github.com/amarnaath05/the-bookshelf/blob/main/src/services/login-service.js"
    click LOCALSSVC "https://github.com/amarnaath05/the-bookshelf/blob/main/src/services/localstorage-service.js"
    click SERVER "https://github.com/amarnaath05/the-bookshelf/blob/main/src/server.js"
    click AUTHCTRL "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/controllers/AuthController.js"
    click PRODCTRL "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/controllers/ProductController.js"
    click CATCTRL "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/controllers/CategoryController.js"
    click CARTCTRL "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/controllers/CartController.js"
    click WISHCTRL "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/controllers/WishlistController.js"
    click USERSDB "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/db/users.js"
    click PRODSDB "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/db/products.js"
    click CATDB "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/db/categories.js"
    click AUTHUTIL "https://github.com/amarnaath05/the-bookshelf/blob/main/src/backend/utils/authUtils.js"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef services fill:#ccffcc,stroke:#333,stroke-width:1px
    classDef backend fill:#ffdb99,stroke:#333,stroke-width:1px
    classDef datastore fill:#fff2cc,stroke:#333,stroke-width:1px
    classDef external fill:#e0e0e0,stroke:#333,stroke-width:1px
```
