api.js
4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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
};
};