# giozulber/frontend-api-coder architecture

> Architecture diagram of the GitHub repository giozulber/frontend-api-coder, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/giozulber/frontend-api-coder
- Repository: https://github.com/giozulber/frontend-api-coder

## Overview

##### Project type and purpose
- This is a full-stack e-commerce application’s frontend, built with React and TypeScript.
- Purpose: Allow users to register, log in (with JWT), browse and manage products, add items to a shopping cart, and for admins to create/update/delete products.

##### File structure
- src/App.tsx and src/main.tsx: application entry point and root component.
- src/components: UI broken down into Feature Folders:
- Form (Login, Register, PostProducts)
- Home (public Home, UserHome)
- Navbar
- Products (list, item, service)
- User (user details, userService)
- cart (Cart, CartItem, cartService, types)
- src/context: React Context providers for user and cart state (cartContext.tsx, userContext.tsx).
- src/utils/UserGuard.tsx: route protection component.
- src/styles: theming (theme.ts via Chakra UI).
- Services: separate files that wrap Axios calls for products, users, cart.
- Config: tsconfig, vite.config.ts for build tooling.

##### README
- Confirms technologies: Chakra UI for design, Axios for HTTP, JSON Web Token for authentication.
- Backend is a separate Vercel-hosted API at https://ecommerce-app-coder.vercel.app/.
- Frontend hosted on Vercel.

##### Main components and their interactions
a. Frontend Client (browser)
- UI Tier: React Components (Forms, Navbar, Home, Products, Cart)
- State Tier: React Context providers (UserContext, CartContext)
- Utils: Route protection (UserGuard)
- Services: productService, userService, cartService (wrap Axios calls)
b. API Gateway / Backend Microservice (external)
- Authentication Endpoints (login, register, JWT verification)
- Product Endpoints (CRUD)
- Cart Endpoints (add, remove, list)
c. Database (external to backend)
- Users table (with roles)
- Products table
- Orders/Cart table
d. External Services
- Vercel for hosting
- JSON Web Token provider/logic

##### Relationships and data flows
- User submits login/register form → userService (Axios) → Backend Auth API → returns JWT → stored in UserContext.
- Browsing Products: on UserHome mount, productService GET → Backend Products API → returns product list → stored in local state.
- Adding to Cart: Cart component calls cartService POST → Backend Cart API → updates cart in database → CartContext updates local state.
- Admin actions (PostProducts): Admin form → productService POST/PUT/DELETE → Backend Products API → updates products in database.
- Protected routes: UserGuard reads JWT from UserContext → allows or denies navigation.
- Theme and layout provided by Chakra UI, wrapped in App.tsx.

##### Architectural patterns and design principles
- Component-based architecture (feature folders).
- Context API for global state (single source of truth for user and cart).
- Services layer abstracts HTTP calls.
- Separation of concerns: UI vs state vs network.
- JWT-based authentication and role-based access (admin vs user).
- Vite as module bundler for fast HMR.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Client
    subgraph "Frontend Client"
        direction TB
        %% Presentation Layer
        subgraph "Presentation Layer"
            direction TB
            Login["Login Form"]:::ui
            Register["Register Form"]:::ui
            PostProducts["PostProducts Form"]:::ui
            Input["Shared Input"]:::ui
            Home["Public Home Page"]:::ui
            UserHome["Authenticated Home Page"]:::ui
            Navbar["Navbar"]:::ui
            ListProducts["Product Listing"]:::ui
            ItemProd["Single Product Item"]:::ui
            ProductDetail["Product Detail View"]:::ui
            UserProfile["User Profile"]:::ui
            CartView["Cart View"]:::ui
            CartItem["Cart Item"]:::ui
        end

        %% State Management Layer
        subgraph "State Management"
            direction TB
            UserContext["UserContext"]:::state
            CartContext["CartContext"]:::state
            ContextProps["Context Props"]:::state
        end

        %% Services Layer
        subgraph "Services & Utils"
            direction TB
            ProductService["productService"]:::service
            UserService["userService"]:::service
            CartService["cartService"]:::service
            UserGuard["UserGuard"]:::util
        end
    end

    %% Backend and Database
    BackendAPI["Backend API (Auth, Products, Cart Endpoints)"]:::backend
    subgraph "Database"
        direction TB
        UsersDB["Users Table"]:::db
        ProductsDB["Products Table"]:::db
        CartDB["Cart/Orders Table"]:::db
    end

    %% External Hosting
    Vercel["Vercel Hosting"]:::external

    %% Connections: UI -> Services
    Login -->|"POST /login"| UserService
    Register -->|"POST /register"| UserService
    Home -->|"load products"| ProductService
    UserHome -->|"load user data"| UserService
    ListProducts -->|"GET /products"| ProductService
    ItemProd -->|"view details"| ProductService
    PostProducts -->|"POST/PUT/DELETE /products"| ProductService
    CartView -->|"GET/POST /cart"| CartService
    CartItem -->|"update quantity"| CartService
    Navbar -->|"auth state"| UserContext

    %% Services -> Backend
    ProductService -->|"HTTP calls"| BackendAPI
    UserService -->|"HTTP calls"| BackendAPI
    CartService -->|"HTTP calls"| BackendAPI
    UserGuard -->|"checks JWT"| UserContext

    %% Backend -> Database
    BackendAPI -->|"queries"| UsersDB
    BackendAPI -->|"queries"| ProductsDB
    BackendAPI -->|"queries"| CartDB

    %% Auth flow
    UserService -->|"returns JWT"| UserContext
    UserContext -->|stores JWT| UserGuard

    %% Hosting
    FrontendClient["Frontend Client"]:::external
    FrontendClient -.->|deployed on| Vercel
    BackendAPI -.->|deployed on| Vercel

    %% Click Events
    click Login "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Form/Login.tsx"
    click Register "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Form/Register.tsx"
    click PostProducts "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Form/PostProducts.tsx"
    click Input "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Form/Input.tsx"
    click Home "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Home/Home.tsx"
    click UserHome "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Home/UserHome.tsx"
    click Navbar "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Navbar/Navbar.tsx"
    click ListProducts "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Products/ListProducts.tsx"
    click ItemProd "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Products/ItemProd.tsx"
    click ProductDetail "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Products/Product.ts"
    click ProductService "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/Products/productService.ts"
    click UserProfile "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/User/User.ts"
    click UserService "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/User/userService.ts"
    click CartView "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/cart/Cart.tsx"
    click CartItem "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/cart/CartItem.tsx"
    click CartService "https://github.com/giozulber/frontend-api-coder/blob/main/src/components/cart/cartService.ts"
    click ContextProps "https://github.com/giozulber/frontend-api-coder/blob/main/src/context/contextProps.ts"
    click CartContext "https://github.com/giozulber/frontend-api-coder/blob/main/src/context/cartContext.tsx"
    click UserContext "https://github.com/giozulber/frontend-api-coder/blob/main/src/context/userContext.tsx"
    click UserGuard "https://github.com/giozulber/frontend-api-coder/blob/main/src/utils/UserGuard.tsx"
    click Theme "https://github.com/giozulber/frontend-api-coder/blob/main/src/styles/theme.ts"
    click Main "https://github.com/giozulber/frontend-api-coder/blob/main/src/main.tsx"
    click App "https://github.com/giozulber/frontend-api-coder/blob/main/src/App.tsx"
    click ViteConfig "https://github.com/giozulber/frontend-api-coder/blob/main/vite.config.ts"
    click TSConfig "https://github.com/giozulber/frontend-api-coder/blob/main/tsconfig.json"
    click TSNodeConfig "https://github.com/giozulber/frontend-api-coder/blob/main/tsconfig.node.json"
    click Env "https://github.com/giozulber/frontend-api-coder/blob/main/src/vite-env.d.ts"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0366D6,color:#0366D6
    classDef state fill:#E6FFEA,stroke:#28A745,color:#28A745
    classDef service fill:#FFF4E5,stroke:#D69E2E,color:#D97706
    classDef util fill:#FFF4E5,stroke:#D97706,color:#D97706
    classDef backend fill:#F5F5F5,stroke:#6B7280,color:#6B7280
    classDef db fill:#DCFCE7,stroke:#065F46,color:#065F46
    classDef external fill:#F0F0F0,stroke:#9CA3AF,color:#374151.Viewing
```
