47 lines
2.3 KiB
JavaScript
47 lines
2.3 KiB
JavaScript
// One table for all trainings: a row per exercise
|
||
|
||
import { alert, approachChips, formatDate, h, perArmBadge, SOLO } from './common.js';
|
||
import { editButton } from './edit-dialog.js';
|
||
import { canEditTraining } from './page.js';
|
||
import { createTable } from './table.js';
|
||
|
||
export function trainingsView(trainings) {
|
||
if (!trainings.length) {
|
||
return alert('info', 'Тренировок нет.');
|
||
}
|
||
|
||
// A training without exercises still gets a row, so it doesn't disappear from the list
|
||
const rows = trainings.flatMap((training) =>
|
||
training.exercises?.length
|
||
? training.exercises.map((exercise) => ({ training, exercise }))
|
||
: [{ training, exercise: null }]);
|
||
|
||
// Trainings from the parser preview are not saved yet: nothing to edit
|
||
const editable = trainings.every((training) => training.id);
|
||
const exerciseCount = rows.filter((row) => row.exercise).length;
|
||
|
||
return h('div', {},
|
||
h('p', { class: 'text-muted' },
|
||
'Тренировок: ', h('strong', {}, `${trainings.length}`),
|
||
', упражнений: ', h('strong', {}, `${exerciseCount}`)),
|
||
createTable({
|
||
id: 'trainings-table',
|
||
rows,
|
||
columns: [
|
||
{ title: 'Дата', className: 'text-nowrap date', sort: (row) => row.training.date, cell: (row) => formatDate(row.training.date) },
|
||
{ title: 'Тренер', className: 'text-nowrap', sort: (row) => row.training.trainer || SOLO, cell: (row) => row.training.trainer || SOLO },
|
||
{
|
||
title: 'Упражнение',
|
||
sort: (row) => row.exercise?.name || '',
|
||
cell: (row) => row.exercise
|
||
? h('span', {}, row.exercise.name, ' ', row.exercise.splitted_weight ? perArmBadge() : null)
|
||
: h('span', { class: 'text-muted fst-italic' }, 'Упражнения не записаны'),
|
||
},
|
||
{ title: 'Подходы (вес × повторения)', cell: (row) => h('span', {}, ...(row.exercise ? approachChips(row.exercise.approaches) : [])) },
|
||
...(editable ? [{
|
||
title: '',
|
||
cell: (row) => canEditTraining(row.training.trainee_id) ? editButton(row.training.id) : null,
|
||
}] : []),
|
||
],
|
||
}));
|
||
}
|