feat: Add mobile-first frontend for training tracker
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
139
frontend/js/api.js
Normal file
139
frontend/js/api.js
Normal file
@@ -0,0 +1,139 @@
|
||||
// API Client for Training Tracker
|
||||
|
||||
const API_BASE_URL = 'http://localhost:8080/api';
|
||||
|
||||
class ApiClient {
|
||||
constructor(baseUrl = API_BASE_URL) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
async request(endpoint, options = {}) {
|
||||
const url = `${this.baseUrl}${endpoint}`;
|
||||
const config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
},
|
||||
...options,
|
||||
};
|
||||
|
||||
if (config.body && typeof config.body === 'object') {
|
||||
config.body = JSON.stringify(config.body);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, config);
|
||||
|
||||
if (response.status === 204) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || 'API request failed');
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('API Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Exercises
|
||||
async getExercises() {
|
||||
return this.request('/exercises');
|
||||
}
|
||||
|
||||
async getExercise(id) {
|
||||
return this.request(`/exercises/${id}`);
|
||||
}
|
||||
|
||||
async createExercise(data) {
|
||||
return this.request('/exercises', {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
async updateExercise(id, data) {
|
||||
return this.request(`/exercises/${id}`, {
|
||||
method: 'PUT',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
async deleteExercise(id) {
|
||||
return this.request(`/exercises/${id}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
// Training Plans
|
||||
async getPlans() {
|
||||
return this.request('/plans');
|
||||
}
|
||||
|
||||
async getPlan(id) {
|
||||
return this.request(`/plans/${id}`);
|
||||
}
|
||||
|
||||
async createPlan(data) {
|
||||
return this.request('/plans', {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
async updatePlan(id, data) {
|
||||
return this.request(`/plans/${id}`, {
|
||||
method: 'PUT',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
async deletePlan(id) {
|
||||
return this.request(`/plans/${id}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
// Sessions
|
||||
async getSessions() {
|
||||
return this.request('/sessions');
|
||||
}
|
||||
|
||||
async getSession(id) {
|
||||
return this.request(`/sessions/${id}`);
|
||||
}
|
||||
|
||||
async createSession(data) {
|
||||
return this.request('/sessions', {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
async updateSession(id, data) {
|
||||
return this.request(`/sessions/${id}`, {
|
||||
method: 'PUT',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
|
||||
async deleteSession(id) {
|
||||
return this.request(`/sessions/${id}`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
}
|
||||
|
||||
async addSessionEntry(sessionId, data) {
|
||||
return this.request(`/sessions/${sessionId}/entries`, {
|
||||
method: 'POST',
|
||||
body: data,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const api = new ApiClient();
|
||||
Reference in New Issue
Block a user