api.js 4.83 KB
// Mock API functions to fetch data from JSON files

// Fetch activities from mock data
export const fetchActivities = async () => {
  try {
    const response = await fetch('/data/activities.json');
    if (!response.ok) throw new Error('Failed to fetch activities');
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching activities:', error);
    throw error;
  }
};

// Fetch a single activity by ID
export const fetchActivityById = async (activityId) => {
  try {
    const response = await fetch('/data/activities.json');
    if (!response.ok) throw new Error('Failed to fetch activity');
    const activities = await response.json();
    const activity = activities.find(act => act.id === activityId);
    
    if (!activity) throw new Error('Activity not found');
    return activity;
  } catch (error) {
    console.error(`Error fetching activity ${activityId}:`, error);
    throw error;
  }
};

// Fetch users data
export const fetchUsers = async () => {
  try {
    const response = await fetch('/data/users.json');
    if (!response.ok) throw new Error('Failed to fetch users');
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching users:', error);
    throw error;
  }
};

// Fetch a single user by ID
export const fetchUserById = async (userId) => {
  try {
    const response = await fetch('/data/users.json');
    if (!response.ok) throw new Error('Failed to fetch user');
    const users = await response.json();
    const user = users.find(u => u.id === userId);
    
    if (!user) throw new Error('User not found');
    return user;
  } catch (error) {
    console.error(`Error fetching user ${userId}:`, error);
    throw error;
  }
};

// Fetch registrations data
export const fetchRegistrations = async (activityId = null, userId = null) => {
  try {
    const response = await fetch('/data/registrations.json');
    if (!response.ok) throw new Error('Failed to fetch registrations');
    let registrations = await response.json();
    
    // Filter by activity ID if provided
    if (activityId) {
      registrations = registrations.filter(reg => reg.activity_id === activityId);
    }
    
    // Filter by user ID if provided
    if (userId) {
      registrations = registrations.filter(reg => reg.user_id === userId);
    }
    
    return registrations;
  } catch (error) {
    console.error('Error fetching registrations:', error);
    throw error;
  }
};

// Fetch check-in data
export const fetchCheckins = async (activityId = null, userId = null) => {
  try {
    const response = await fetch('/data/checkins.json');
    if (!response.ok) throw new Error('Failed to fetch check-ins');
    let checkins = await response.json();
    
    // Filter by activity ID if provided
    if (activityId) {
      checkins = checkins.filter(check => check.activity_id === activityId);
    }
    
    // Filter by user ID if provided
    if (userId) {
      checkins = checkins.filter(check => check.user_id === userId);
    }
    
    return checkins;
  } catch (error) {
    console.error('Error fetching check-ins:', error);
    throw error;
  }
};

// Fetch messages data
export const fetchMessages = async (recipientId = null) => {
  try {
    const response = await fetch('/data/messages.json');
    if (!response.ok) throw new Error('Failed to fetch messages');
    let messages = await response.json();
    
    // Filter by recipient ID if provided
    if (recipientId) {
      messages = messages.filter(msg => msg.recipient_id === recipientId);
    }
    
    return messages;
  } catch (error) {
    console.error('Error fetching messages:', error);
    throw error;
  }
};

// Mock function for creating an activity (would be an API POST in a real app)
export const createActivity = async (activityData) => {
  // In a real app, this would send a POST request
  console.log('Creating activity with data:', activityData);
  return {
    success: true,
    id: `A${Math.floor(Math.random() * 10000).toString().padStart(4, '0')}`,
    ...activityData
  };
};

// Mock function for registering to an activity
export const registerForActivity = async (activityId, userData) => {
  // In a real app, this would send a POST request
  console.log(`Registering for activity ${activityId} with data:`, userData);
  return {
    success: true,
    id: `R${Math.floor(Math.random() * 10000).toString().padStart(4, '0')}`,
    activity_id: activityId,
    ...userData
  };
};

// Mock function for checking in to an activity
export const checkInForActivity = async (registrationId, checkinData) => {
  // In a real app, this would send a POST request
  console.log(`Checking in for registration ${registrationId} with data:`, checkinData);
  return {
    success: true,
    id: `C${Math.floor(Math.random() * 10000).toString().padStart(4, '0')}`,
    registration_id: registrationId,
    ...checkinData
  };
};