initial commit
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000/api';
|
||||
|
||||
// Create axios instance
|
||||
const api = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
// Add auth token to requests
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// Handle errors
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
// Clear token on unauthorized
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// Participant API
|
||||
export const participantAPI = {
|
||||
// Join an event
|
||||
joinEvent: (pseudonym, eventId) =>
|
||||
api.post('/participant/join', { pseudonym, eventId }),
|
||||
|
||||
// Get active events
|
||||
getEvents: () =>
|
||||
api.get('/participant/events'),
|
||||
|
||||
// Get profile
|
||||
getProfile: () =>
|
||||
api.get('/participant/profile'),
|
||||
|
||||
// Get progress
|
||||
getProgress: () =>
|
||||
api.get('/participant/progress'),
|
||||
|
||||
// Get lessons for event
|
||||
getEventLessons: (eventId) =>
|
||||
api.get(`/lesson/event/${eventId}/lessons`),
|
||||
|
||||
// Get lesson content
|
||||
getLessonContent: (eventLessonId) =>
|
||||
api.get(`/lesson/${eventLessonId}`),
|
||||
|
||||
// Start lesson
|
||||
startLesson: (eventLessonId) =>
|
||||
api.post(`/lesson/${eventLessonId}/start`),
|
||||
|
||||
// Submit answer
|
||||
submitAnswer: (eventLessonId, questionId, answer) =>
|
||||
api.post(`/lesson/${eventLessonId}/answer`, { questionId, answer }),
|
||||
|
||||
// Complete lesson
|
||||
completeLesson: (eventLessonId) =>
|
||||
api.post(`/lesson/${eventLessonId}/complete`),
|
||||
|
||||
// Execute lesson-specific actions (e.g., SQL query, interactive demos)
|
||||
executeLessonAction: (eventLessonId, action, data) =>
|
||||
api.post(`/lesson/${eventLessonId}/action/${action}`, data)
|
||||
};
|
||||
|
||||
// Admin API
|
||||
export const adminAPI = {
|
||||
// Login
|
||||
login: (username, password) =>
|
||||
api.post('/admin/login', { username, password }),
|
||||
|
||||
// Get profile
|
||||
getProfile: () =>
|
||||
api.get('/admin/profile'),
|
||||
|
||||
// Verify token
|
||||
verifyToken: () =>
|
||||
api.get('/admin/verify'),
|
||||
|
||||
// Events
|
||||
getEvents: () =>
|
||||
api.get('/admin/events'),
|
||||
|
||||
getEvent: (eventId) =>
|
||||
api.get(`/admin/events/${eventId}`),
|
||||
|
||||
createEvent: (eventData) =>
|
||||
api.post('/admin/events', eventData),
|
||||
|
||||
updateEvent: (eventId, eventData) =>
|
||||
api.put(`/admin/events/${eventId}`, eventData),
|
||||
|
||||
deleteEvent: (eventId) =>
|
||||
api.delete(`/admin/events/${eventId}`),
|
||||
|
||||
// Participants
|
||||
getEventParticipants: (eventId) =>
|
||||
api.get(`/admin/events/${eventId}/participants`),
|
||||
|
||||
getEventAnalytics: (eventId) =>
|
||||
api.get(`/admin/events/${eventId}/analytics`),
|
||||
|
||||
// Lessons
|
||||
getAllLessons: () =>
|
||||
api.get('/admin/lessons'),
|
||||
|
||||
getEventLessons: (eventId) =>
|
||||
api.get(`/admin/events/${eventId}/lessons`),
|
||||
|
||||
assignLessonToEvent: (eventId, lessonData) =>
|
||||
api.post(`/admin/events/${eventId}/lessons`, lessonData),
|
||||
|
||||
updateEventLesson: (eventLessonId, updates) =>
|
||||
api.put(`/admin/event-lessons/${eventLessonId}`, updates),
|
||||
|
||||
removeEventLesson: (eventLessonId) =>
|
||||
api.delete(`/admin/event-lessons/${eventLessonId}`),
|
||||
|
||||
// Participant progress
|
||||
getParticipantProgress: (participantId) =>
|
||||
api.get(`/admin/participants/${participantId}/progress`)
|
||||
};
|
||||
|
||||
export default api;
|
||||
@@ -0,0 +1,71 @@
|
||||
// Session management for participant
|
||||
export const sessionService = {
|
||||
// Save session
|
||||
saveSession: (token, participant, event) => {
|
||||
localStorage.setItem('token', token);
|
||||
localStorage.setItem('participant', JSON.stringify(participant));
|
||||
localStorage.setItem('event', JSON.stringify(event));
|
||||
},
|
||||
|
||||
// Get token
|
||||
getToken: () => {
|
||||
return localStorage.getItem('token');
|
||||
},
|
||||
|
||||
// Get participant
|
||||
getParticipant: () => {
|
||||
const data = localStorage.getItem('participant');
|
||||
return data ? JSON.parse(data) : null;
|
||||
},
|
||||
|
||||
// Get event
|
||||
getEvent: () => {
|
||||
const data = localStorage.getItem('event');
|
||||
return data ? JSON.parse(data) : null;
|
||||
},
|
||||
|
||||
// Clear session
|
||||
clearSession: () => {
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('participant');
|
||||
localStorage.removeItem('event');
|
||||
},
|
||||
|
||||
// Check if has session
|
||||
hasSession: () => {
|
||||
return !!localStorage.getItem('token');
|
||||
}
|
||||
};
|
||||
|
||||
// Admin session management
|
||||
export const adminSessionService = {
|
||||
// Save admin session
|
||||
saveSession: (token, admin) => {
|
||||
localStorage.setItem('adminToken', token);
|
||||
localStorage.setItem('admin', JSON.stringify(admin));
|
||||
},
|
||||
|
||||
// Get admin token
|
||||
getToken: () => {
|
||||
return localStorage.getItem('adminToken');
|
||||
},
|
||||
|
||||
// Get admin
|
||||
getAdmin: () => {
|
||||
const data = localStorage.getItem('admin');
|
||||
return data ? JSON.parse(data) : null;
|
||||
},
|
||||
|
||||
// Clear admin session
|
||||
clearSession: () => {
|
||||
localStorage.removeItem('adminToken');
|
||||
localStorage.removeItem('admin');
|
||||
},
|
||||
|
||||
// Check if has admin session
|
||||
hasSession: () => {
|
||||
return !!localStorage.getItem('adminToken');
|
||||
}
|
||||
};
|
||||
|
||||
export default sessionService;
|
||||
Reference in New Issue
Block a user