92 lines
2.0 KiB
JavaScript
92 lines
2.0 KiB
JavaScript
import { state, saveState, genId } from '../state.js';
|
|
import { renderMainScreen } from '../render/main-screen.js';
|
|
import { showSetDialog } from '../modals/set-modal.js';
|
|
import { getExerciseHistory } from './exercise.js';
|
|
|
|
export function addSet(workoutId, exerciseId) {
|
|
const w = state.workouts.find(x => x.id === workoutId);
|
|
if (!w) return;
|
|
|
|
const ex = w.exercises.find(e => e.id === exerciseId);
|
|
if (!ex) return;
|
|
|
|
if (!Array.isArray(ex.sets)) {
|
|
ex.sets = [];
|
|
}
|
|
|
|
const lastSet = ex.sets[ex.sets.length - 1];
|
|
|
|
const history = getExerciseHistory(
|
|
ex.name,
|
|
w.client_id ?? null,
|
|
w.id
|
|
);
|
|
|
|
showSetDialog(lastSet, history, (setData) => {
|
|
if (!setData) return;
|
|
|
|
setData.id = genId();
|
|
setData.timestamp = new Date().toISOString();
|
|
|
|
ex.sets.push(setData);
|
|
|
|
saveState();
|
|
renderMainScreen();
|
|
});
|
|
}
|
|
|
|
export function editSet(workoutId, exerciseId, setId) {
|
|
const w = state.workouts.find(x => x.id === workoutId);
|
|
if (!w) return;
|
|
|
|
const ex = w.exercises.find(e => e.id === exerciseId);
|
|
if (!ex) return;
|
|
|
|
if (!Array.isArray(ex.sets)) {
|
|
ex.sets = [];
|
|
}
|
|
|
|
const set = ex.sets.find(s => s.id === setId);
|
|
if (!set) return;
|
|
|
|
const history = getExerciseHistory(
|
|
ex.name,
|
|
w.client_id ?? null,
|
|
w.id
|
|
);
|
|
|
|
showSetDialog(set, history, (setData) => {
|
|
if (!setData) return;
|
|
|
|
Object.assign(set, setData);
|
|
|
|
saveState();
|
|
renderMainScreen();
|
|
});
|
|
}
|
|
|
|
export function deleteSet(workoutId, exerciseId, setId) {
|
|
if (!confirm('Удалить подход?')) return;
|
|
|
|
const w = state.workouts.find(x => x.id === workoutId);
|
|
if (!w) return;
|
|
|
|
const ex = w.exercises.find(e => e.id === exerciseId);
|
|
if (!ex) return;
|
|
|
|
if (!Array.isArray(ex.sets)) {
|
|
ex.sets = [];
|
|
}
|
|
|
|
ex.sets = ex.sets.filter(s => s.id !== setId);
|
|
|
|
saveState();
|
|
renderMainScreen();
|
|
}
|
|
|
|
window.SL = {
|
|
addSet,
|
|
editSet,
|
|
deleteSet,
|
|
};
|