src/utils: Utility Functions for Enhanced Functionality

The src/utils directory houses a collection of utility functions that provide reusable logic and functionality for various aspects of the SILAS Web demo app. These functions encapsulate common tasks, making the code more modular, readable, and maintainable.

1. request.js

This file defines a function for making API requests using axios. It provides a centralized way to handle API calls, including setting up base URLs, interceptors, and error handling.

1.1. request

This function takes an options object and a config object as input and returns a Promise that resolves with the API response data. It handles making the API request, processing the response, and handling potential errors.

2. url.js

This file defines functions for parsing and validating URLs. It provides utilities for breaking down URLs into their components and checking if a given string is a valid URL.

2.1. parseUri

This function takes a URL string as input and returns an object containing the different components of the URL, such as the protocol, host, path, and query parameters.

2.2. validateUrl

This function takes a URL string as input and returns true if the string is a valid URL, and false otherwise.

3. routes.js

This file defines functions for building the application's routes. It uses React Router to define the different routes within the application and map them to corresponding components. This allows for a structured and organized approach to navigation within the app.

3.1. onAuthEnterCallback

This function is called when a user navigates to the /auth route. It handles authentication logic, such as retrieving an access token from the URL and communicating with the parent window.

3.2. buildAppRoutes

This function takes the main application component (App) as input and returns an array of route objects. Each route object defines a path and a corresponding component to be rendered when that path is accessed. This function allows for defining the application's navigation structure in a centralized and reusable manner.

4. strings.js

This file defines a function for generating a random hash string.

4.1. generateHashId

This function takes a number as input and returns a random hash string of the specified length.

5. api.js

This file contains the loadApiData function, which is responsible for retrieving and processing data from a specified API endpoint. The function is designed to facilitate seamless integration with Kepler.gl by converting the fetched data into a compatible format for visualization.

5.1. loadApiData

The loadApiData function is a versatile utility that accepts two parameters: an endpoint name, which defaults to 'points', and an optional customUrl. It returns a Promise that resolves to an object structured for Kepler.gl visualization, including datasets and configuration options.

  • Base URL and Endpoints: The function constructs API request URLs using a base URL (http://127.0.0.1:5000) and predefined endpoints (points, routes, trips). Each endpoint has a specific URL with query parameters tailored to fetch relevant data.

  • Data Fetching: The function performs an HTTP GET request to the constructed URL (or the customUrl if provided) using the Fetch API. It processes the JSON response to extract data.

  • Data Processing: Depending on the endpoint, the function processes the data differently:

    • For points, it extracts fields like order_id, time_stamp, and geographical coordinates, mapping them to a structured array.
    • For routes, it processes source and target coordinates, timestamps, and additional metadata.
    • For trips, it handles GeoJSON features, converting geometries into JSON strings.
  • Kepler.gl Integration: The processed data is formatted into a Kepler.gl-compatible structure, including:

    • datasets: An array containing dataset information (info and data), with fields and rows derived from the processed data.
    • options: Configuration options for map centering and interactivity.
    • config: An empty object reserved for future configuration enhancements.