Skip to main content

Overview

EducaStream provides a comprehensive set of utility functions for managing users, courses, payments, and data validation. All API calls use Axios and are configured to work with the backend endpoints.

Data Fetching Functions

getAllCourses

Fetches all courses from the database and stores them in local storage, sorted by creation date (newest first).
none
void
This function takes no parameters
data
array
Array of course objects sorted by createdAt in descending order
id
string
Unique course identifier
title
string
Course title
description
string
Course description
createdAt
string
ISO timestamp of course creation
Source: src/utils/getAllCourses.js:3
import { getAllCourses } from './utils/getAllCourses';

// Fetch and store all courses
const courses = await getAllCourses();
console.log(courses); // Sorted by newest first

getAllCategories

Retrieves all course categories from the database, sorted alphabetically by name.
none
void
This function takes no parameters
data
array
Array of category objects sorted alphabetically
name
string
Category name
id
string
Category identifier
Source: src/utils/getAllCategories.js:3
import { getAllCategories } from './utils/getAllCategories';

// Used in App.jsx on initial load
useEffect(() => {
  getAllCategories();
}, []);

getAllUser

Fetches all users from the database.
none
void
This function takes no parameters
data
array
Array of user objects
Source: src/utils/getAllUser.js:3
import { getAllUser } from './utils/getAllUser';

// Example from App.jsx
useEffect(() => {
  getAllUser();
  getAllCourses();
  getAllCategories();
}, []);

getUserById

Retrieves a specific user by their unique ID.
id
string
required
The unique identifier of the user to fetch
data
object
User object with all user details
id
string
Unique user identifier
email
string
User email address
user_name
string
Username
Payments
array
Array of user’s payment records
Source: src/utils/getUserById.js:3
import { getUserById } from './utils/getUserById';

const user = await getUserById('ab518b48-1a30-4d10-b525-479167e4fdd4');

getUser

Fetches a user by their email address.
email
string
required
The email address of the user to retrieve
data
object
User object matching the email
Source: src/utils/getUser.js:3
import { getUser } from './utils/getUser';

const user = await getUser('[email protected]');

getOnSaleCourses

Retrieves all courses currently on sale.
none
void
This function takes no parameters
data
array
Array of course objects with active sales/discounts
Source: src/utils/getOnSaleCourses.js:3
import { getOnSaleCourses } from './utils/getOnSaleCourses';

const saleCourses = await getOnSaleCourses();

getAllPayments

Fetches all payment records from the database.
none
void
This function takes no parameters
data
array
Array of payment objects
Courses
array
Courses included in the payment
Source: src/utils/getAllPayments.js:3
import { getAllPayments } from './utils/getAllPayments';

const payments = await getAllPayments();

Data Mutation Functions

postUser

Creates a new user in the database.
user
object
required
User object containing registration data
email
string
required
User’s email address
password
string
required
User’s password
user_name
string
Username
first_name
string
User’s first name
last_name
string
User’s last name
birthdate
string
User’s birthdate in ISO format
role_instructor
boolean
Whether user has instructor role
role_student
boolean
Whether user has student role
data
object
Created user object
Source: src/utils/postUser.js:3
import { postUser } from './utils/postUser';

const newUser = await postUser({
  email: "[email protected]",
  password: "securePassword123",
  user_name: "johndoe",
  first_name: "John",
  last_name: "Doe",
  role_student: true,
  role_instructor: false
});

updateUser

Updates an existing user’s information.
user
object
required
User object containing fields to update (must include user ID)
data
object
Updated user object
Source: src/utils/updateUser.js:3
import { updateUser } from './utils/updateUser';

const updated = await updateUser({
  id: "ab518b48-1a30-4d10-b525-479167e4fdd4",
  first_name: "UpdatedName"
});

Validation Functions

validation

Validates course creation/update form data.
props
object
required
Object containing course form fields
title
string
required
Course title (min 3 characters)
description
string
required
Course description (20-90 characters)
category
string
required
Course category
sections
number
required
Number of sections (min 1)
price
number
required
Course price (min $0.50)
errors
object
Object containing validation error messages keyed by field name
title
string
Title validation error message
description
string
Description validation error message
category
string
Category validation error message
sections
string
Sections validation error message
price
string
Price validation error message
Source: src/utils/validation.js:1
import { validation } from './utils/validation';

const formData = {
  title: "JS",  // Too short
  description: "Short desc",  // Too short
  category: "Programming",
  sections: 3,
  price: 29.99
};

const errors = validation(formData);
// Returns: {
//   title: "Debe tener al menos 3 caracteres.",
//   description: "La descripción debe tener al menos 20 caracteres."
// }

validationLesson

Validates lesson creation/update form data.
props
object
required
Object containing lesson form fields
title
string
required
Lesson title (min 3 characters)
description
string
required
Lesson description (min 20 characters)
section
number
required
Section number (must be >= 1)
video_url
string
required
Video URL for the lesson
errors
object
Object containing validation error messages keyed by field name
Source: src/utils/validation.js:39
import { validationLesson } from './utils/validation';

const lessonData = {
  title: "Introduction",
  description: "This lesson covers the basics of JavaScript",
  section: 1,
  video_url: "https://youtube.com/watch?v=abc123"
};

const errors = validationLesson(lessonData);
if (Object.keys(errors).length === 0) {
  // Validation passed
}

Utility Functions

clear

Clears all user session data from localStorage and reloads the page. Used for logout functionality.
none
void
This function takes no parameters
Source: src/utils/clear.js:1
import { clear } from './utils/clear';

// Logout user and clear session
clear();
// Removes: userOnSession, logged, payment
// Then reloads the page

filterBack

Filters courses by a specific parameter on the backend.
params
object
Filter parameters object
type
string
Filter type (e.g., “category”, “instructor”)
value
string
Filter value
Source: src/utils/FiltersBc.js:3
import { filterBack } from './utils/FiltersBc';

// Filter courses by category
await filterBack({
  type: "category",
  value: "Programación"
});

Error Handling

All API functions implement try-catch error handling:
try {
  const response = await axios.get('/endpoint');
  if (response.status !== 200) {
    throw new Error(`HTTP error! Status: ${response.status}`);
  }
  return response.data;
} catch (error) {
  console.error("Error fetching data:", error);
  throw error;
}
Payment functions include user-friendly alerts:
try {
  const { data } = await axios.post("payment/enrollment", payload);
  return data;
} catch (error) {
  window.alert("Error en la pasarela de pago");
}

Build docs developers (and LLMs) love