integrated front and back

This commit is contained in:
2026-10-01 23:52:12 +03:00
parent 1f81632083
commit 3e966c9ff6
15 changed files with 634 additions and 72 deletions
+73 -29
View File
@@ -1,30 +1,54 @@
import { state, saveState, genId } from '../state.js';
import { renderMainScreen } from '../render/main-screen.js';
import { showExerciseDialog } from '../modals/exercise-modal.js';
import { fetchExerciseHistory } from '../api.js';
function normalizeName(name) {
return String(name || '').trim().toLowerCase();
}
function normalizeClientId(id) {
if (id === undefined || id === null || id === '') return null;
return String(id);
}
function isSameClient(a, b) {
return normalizeClientId(a) === normalizeClientId(b);
}
export function addExercise(workoutId) {
const w = state.workouts.find(x => x.id === workoutId);
if (!w) return;
showExerciseDialog(w.type, (name) => {
if (!name) return;
const ex = { id: genId(), name, sets: [] };
const ex = {
id: genId(),
name,
sets: [],
};
w.exercises.push(ex);
addToRef(name, w.type);
state.expandedExerciseId = ex.id;
saveState();
renderMainScreen();
});
}
export function addToRef(name, workoutType = '') {
const normalizedName = name.trim().toLowerCase();
const normalizedName = normalizeName(name);
const exists = state.exerciseRef.find(
e => e.name.trim().toLowerCase() === normalizedName
e => normalizeName(e.name) === normalizedName
);
if (!exists) {
state.exerciseRef.push({
id: genId(),
name: name.trim(),
name: String(name).trim(),
workoutType,
});
} else if (!exists.workoutType && workoutType) {
@@ -35,48 +59,68 @@ export function addToRef(name, workoutType = '') {
export function toggleExercise(exerciseId) {
state.expandedExerciseId =
state.expandedExerciseId === exerciseId ? null : exerciseId;
renderMainScreen();
}
export function deleteExercise(workoutId, exerciseId) {
if (!confirm('Удалить упражнение?')) return;
const w = state.workouts.find(x => x.id === workoutId);
if (!w) return;
w.exercises = w.exercises.filter(e => e.id !== exerciseId);
saveState();
renderMainScreen();
}
export async function getExerciseHistory(exerciseName) {
// Пытаемся получить с бэкенда
try {
const history = await fetchExerciseHistory(exerciseName, state.currentClientId);
if (history && history.length > 0) {
return history.map(h => ({
date: h.date,
sets: [{ // Адаптируем формат бэкенда под формат фронта
isDropset: h.is_dropset,
weight: h.weight,
reps: h.reps,
subSets: h.is_dropset ? [{weight: h.weight, reps: h.reps}] : undefined // Упрощение для отображения
}]
}));
}
} catch (e) {
console.warn('Не удалось загрузить историю с бэкенда, используем локальную', e);
}
/**
* Локальная история упражнения.
*
* @param {string} exerciseName
* @param {string|number|null} clientId - если тренировка принадлежит клиенту, история берётся только для этого клиента
* @param {string|number|null} excludeWorkoutId - чтобы не показывать текущую тренировку как "прошлую"
*/
export function getExerciseHistory(exerciseName, clientId = null, excludeWorkoutId = null) {
const normalizedName = normalizeName(exerciseName);
const targetClient = normalizeClientId(clientId);
// Fallback на локальные данные
const normalizedName = exerciseName.trim().toLowerCase();
const history = [];
for (const w of state.workouts) {
for (const ex of w.exercises) {
if (ex.name.trim().toLowerCase() === normalizedName && ex.sets.length > 0) {
history.push({ date: w.startTime, sets: ex.sets });
// Не показываем текущую тренировку в истории
if (excludeWorkoutId && String(w.id) === String(excludeWorkoutId)) {
continue;
}
// Если у тренировки есть клиент — сравниваем клиентов
// Если клиента нет — показываем только личные тренировки без клиента
if (!isSameClient(w.client_id, targetClient)) {
continue;
}
for (const ex of w.exercises || []) {
if (
normalizeName(ex.name) === normalizedName &&
Array.isArray(ex.sets) &&
ex.sets.length > 0
) {
history.push({
date: w.startTime,
sets: ex.sets,
});
}
}
}
history.sort((a, b) => new Date(b.date) - new Date(a.date));
return history.slice(0, 3);
}
window.EL = { addExercise, toggleExercise, deleteExercise };
window.EL = {
addExercise,
toggleExercise,
deleteExercise,
};