Utility Functions
Utility Functions
The src/Components/utilityFunctions.js file contains a collection of helper functions designed to standardize data formatting and handle common logic across the application. These utilities simplify tasks such as date manipulation, media parsing, and file management.
General Utilities
extractDate
Converts a date string (typically an ISO timestamp from the backend) into a human-readable format.
- Input:
dateString(String) - The raw date string to be formatted. - Output:
formattedDate(String) - The date formatted for display (e.g.,YYYY-MM-DD).
Usage Example:
import { extractDate } from '../utilityFunctions';
const rawDate = "2023-10-27T10:30:00Z";
const displayDate = extractDate(rawDate); // "2023-10-27"
videoIdParser
Extracts the unique video ID from various YouTube URL formats. This is primarily used within the Gallery component to render YouTube embeds.
- Input:
url(String) - A full YouTube URL (e.g.,https://www.youtube.com/watch?v=dQw4w9WgXcQ). - Output:
videoId(String) - The 11-character YouTube video ID.
Usage Example:
import { videoIdParser } from '../../../Components/utilityFunctions';
const url = "https://youtu.be/aqz-KE-BPKQ";
const id = videoIdParser(url); // "aqz-KE-BPKQ"
File Handling & API Helpers
These functions are located within the API service layers but serve as critical utility operations for interacting with files and authentication.
downloadPublicationFile
Handles the logic for fetching a file from the server and triggering a browser-level download. It manages blob conversion and creates a temporary DOM element to initiate the download process.
- Input:
filename(String) - The name of the file stored on the server. - Action: Triggers a file save dialog in the user's browser.
Usage Example:
import { downloadPublicationFile } from './publicationApi';
const handleDownload = (file) => {
downloadPublicationFile(file.name);
};
getAuthToken (Internal)
A helper function used within the API service files to retrieve the current session's token from local storage.
- Output:
token(String | null) - Returns theauthTokenstring if present, otherwisenull.
Internal Role:
This utility is used to populate the Authorization headers for protected routes (e.g., adding/deleting content in the CMS).
Environment Variable Usage
The application relies on environment-specific utilities to handle URLs dynamically via import.meta.env.
| Variable | Description |
| :--- | :--- |
| VITE_API_URL | Base endpoint for all backend API calls. |
| VITE_FILE_URL | Base path for accessing uploaded files/documents. |
| VITE_IMAGE_URL | Base path for serving images from the CDN or server. |
Example of dynamic URL utility:
const BASE_URL = import.meta.env.VITE_API_URL;
const response = await axios.get(`${BASE_URL}/endpoint`);