# rnhc1000/calculatorui architecture

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

- Interactive diagram: https://gitdiagram.com/rnhc1000/calculatorui
- Repository: https://github.com/rnhc1000/calculatorui

## Overview

##### Main components
- Frontend Application: Represent the ReactJS codebase that includes the UI and components.
- Key folders: src (with App.tsx, assets, components, routes, hooks, services, models, and utils).
- Components such as Header, Footer, Login, Operations, and various UI elements (like FormInput, AG-GRID based datatable, etc.) form the core view layer.
- React Router: Routing between pages such as Home, Login, Operation, and Records.
- React Context & State Management: Components such as ContextWalletBalance (in utils/context-wallet.ts and similar for token). This handles real-time balance updates and authentication state.
- Services Layer: Responsible for handling communication with the external RESTful API. Services include:
- auth-services.ts, operation-services.ts, csrf-services.ts, records-services.ts, user-service.ts, wallet-services.ts.
- Local Storage Repositories: Modules to manage tokens and wallet data (in src/localstorage).
- External RESTful API: Located externally (https://api.ferreiras.dev.br), which actually performs the math operations, ensures security with authentication, and manages recorded operations in the backend database.

##### Relationships and interactions
- The user interacts with the UI (constructed of multiple React components).
- Upon login, the authentication service (auth-services.ts) communicates with the REST API to validate credentials, retrieves JWT tokens, and then updates the React Context.
- The UI components take advantage of React Hooks (useState, useEffect, etc.) to update UI state (for balance, operation results) in real time.
- When the user initiates a math operation, the front-end makes a REST call through its service layer to the back-end API. The response (result of the operation and updated balance) flows back to update the UI, stored locally, and possibly persisted in localStorage.
- The AG-GRID based table component displays a history of operations by fetching data from the backend via the corresponding service.

##### Important architectural patterns and design principles
- Separation of Concerns: UI components, business logic (services), and state management (React Context/localstorage) are mutually separated.
- Component-Based UI: Every UI part (header, login, table view, footer) is built as its own component, encouraging reuse and maintainability.
- Service Abstraction: All API communication is encapsulated in dedicated service modules.
- Routing and Navigation: React Router is used to neatly manage different pages and their state.
- Configuration & Environment: Environment variables (seen in the system.ts file) are used to set the base URL for the backend, making the instance configurable per deployment.

##### Specific technologies and frameworks
- ReactJS with TypeScript
- React Router for navigation
- React Hooks and React Context for state management (e.g., wallet and token)
- AG-GRID for displaying operation records
- Vite as the build tool
- Additional libraries: axios for HTTP requests, jwt-decode for token management, and others found in the .vite/deps folder.

- User -> UI Components
- UI Components <-> Service Modules
- Service Modules -> External REST API
- API responses -> UI update (via Context and Hooks)

##### Bring it all together
- Next, illustrate the API Service Layer as a middle tier that abstracts direct REST calls.
- Notes about environment configuration (e.g., BASE_URL in system.ts) and mention that deployment may require tweaking these settings (as mentioned in the README).

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% User
    user["User"]:::external

    %% Calculator Web UI (React)
    subgraph "Calculator Web UI (React)"
        subgraph "React Components & Routes"
            Header["Header"]:::ui
            Footer["Footer"]:::ui
            Login["Login"]:::ui
            Operations["Operations"]:::ui
            DataTable["Data Table"]:::ui
            Routing["Routing"]:::ui
        end
        subgraph "State Management (Context & Local Storage)"
            ContextWallet["Context: Wallet"]:::context
            ContextToken["Context: Token"]:::context
            AccessTokenRepo["Access Token Repo"]:::context
            WalletRepo["Wallet Repo"]:::context
        end
        EnvConfig["Environment Config"]:::config
    end

    %% Services Layer (API Clients)
    subgraph "Services Layer (API Clients)"
        AuthService["Auth Service"]:::service
        OperationsService["Operations Service"]:::service
        CSRFService["CSRF Service"]:::service
        RecordsService["Records Service"]:::service
        UserService["User Service"]:::service
        WalletService["Wallet Service"]:::service
    end

    %% External REST API
    subgraph "Calculator Backend (REST API)"
        API["REST API"]:::external
    end

    %% Connections between User and UI
    user -->|"interacts"| Login

    %% Connections from UI Components to Services and State Management
    Login -->|"auth"| AuthService
    Operations -->|"calculate"| OperationsService
    DataTable -->|"fetch_records"| RecordsService
    Login -->|"store_token"| ContextToken
    Login -->|"update_wallet"| ContextWallet

    %% Connections from Services Layer to External REST API (bidirectional)
    AuthService -->|"REST_call"| API
    OperationsService -->|"REST_call"| API
    CSRFService -->|"REST_call"| API
    RecordsService -->|"REST_call"| API
    UserService -->|"REST_call"| API
    WalletService -->|"REST_call"| API

    API -->|"response"| AuthService
    API -->|"response"| OperationsService
    API -->|"response"| CSRFService
    API -->|"response"| RecordsService
    API -->|"response"| UserService
    API -->|"response"| WalletService

    %% Configuration influence from Environment Config to Services Layer
    EnvConfig -->|"config"| AuthService
    EnvConfig -->|"config"| OperationsService
    EnvConfig -->|"config"| CSRFService
    EnvConfig -->|"config"| RecordsService
    EnvConfig -->|"config"| UserService
    EnvConfig -->|"config"| WalletService

    %% Styling classes
    classDef ui fill:#e1f5fe,stroke:#0277bd,stroke-width:2px;
    classDef service fill:#ffe082,stroke:#f57f17,stroke-width:2px;
    classDef context fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef external fill:#ffcdd2,stroke:#c62828,stroke-width:2px;
    classDef config fill:#d1c4e9,stroke:#512da8,stroke-width:2px;

    %% Click Events for Frontend Application and UI Components
    click Header "https://github.com/rnhc1000/calculatorui/blob/main/src/components/Header/index.tsx"
    click Footer "https://github.com/rnhc1000/calculatorui/blob/main/src/components/Footer/index.tsx"
    click Login "https://github.com/rnhc1000/calculatorui/blob/main/src/components/Login/index.tsx"
    click Operations "https://github.com/rnhc1000/calculatorui/blob/main/src/components/Operations/index.tsx"
    click DataTable "https://github.com/rnhc1000/calculatorui/blob/main/src/components/Tables/index.tsx"
    click Routing "https://github.com/rnhc1000/calculatorui/tree/main/src/routes/"

    %% Click Events for State Management and Local Storage
    click ContextWallet "https://github.com/rnhc1000/calculatorui/blob/main/src/utils/context-wallet.ts"
    click ContextToken "https://github.com/rnhc1000/calculatorui/blob/main/src/utils/context-token.ts"
    click AccessTokenRepo "https://github.com/rnhc1000/calculatorui/blob/main/src/localstorage/access-token-repository.ts"
    click WalletRepo "https://github.com/rnhc1000/calculatorui/blob/main/src/localstorage/wallet-repository.ts"

    %% Click Event for Environment Configuration
    click EnvConfig "https://github.com/rnhc1000/calculatorui/blob/main/src/utils/system.ts"

    %% Click Events for Services Layer
    click AuthService "https://github.com/rnhc1000/calculatorui/blob/main/src/services/auth-services.ts"
    click OperationsService "https://github.com/rnhc1000/calculatorui/blob/main/src/services/operation-services.ts"
    click CSRFService "https://github.com/rnhc1000/calculatorui/blob/main/src/services/csrf-services.ts"
    click RecordsService "https://github.com/rnhc1000/calculatorui/blob/main/src/services/records-services.ts"
    click UserService "https://github.com/rnhc1000/calculatorui/blob/main/src/services/user-service.ts"
    click WalletService "https://github.com/rnhc1000/calculatorui/blob/main/src/services/wallet-services.ts"
```
