Files
gym-bro/frontend/js/logic/exercise.js
T

63 lines
2.0 KiB
JavaScript

import { state, saveState, genId } from '../state.js';
import { renderMainScreen } from '../render/main-screen.js';
import { showExerciseDialog } from '../modals/exercise-modal.js';
export function addExercise(workoutId) {
const w = state.workouts.find(x => x.id === workoutId);
showExerciseDialog(w.type, (name) => {
if (!name) return;
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 exists = state.exerciseRef.find(
e => e.name.trim().toLowerCase() === normalizedName
);
if (!exists) {
state.exerciseRef.push({
id: genId(),
name: name.trim(),
workoutType,
});
} else if (!exists.workoutType && workoutType) {
exists.workoutType = 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);
w.exercises = w.exercises.filter(e => e.id !== exerciseId);
saveState();
renderMainScreen();
}
export function getExerciseHistory(exerciseName) {
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 });
}
}
}
history.sort((a, b) => new Date(b.date) - new Date(a.date));
return history.slice(0, 3);
}
window.EL = { addExercise, toggleExercise, deleteExercise };