31 lines
1.3 KiB
JavaScript
31 lines
1.3 KiB
JavaScript
// Modal with the training edit form
|
||
|
||
import { alert, api, h } from './common.js';
|
||
import { editForm } from './edit-form.js';
|
||
import { openModal } from './modal.js';
|
||
|
||
/** "Изменить" button: opens the modal, the page reloads after a save to show fresh data */
|
||
export function editButton(trainingId) {
|
||
const button = h('button', { type: 'button', class: 'btn btn-sm btn-outline-secondary' }, 'Изменить');
|
||
button.addEventListener('click', () => openEditDialog(trainingId, () => location.reload()));
|
||
return button;
|
||
}
|
||
|
||
/** Opens the form for the training; onChange() is called after it was saved or deleted. */
|
||
export async function openEditDialog(trainingId, onChange) {
|
||
const { body, hide } = openModal('Редактирование тренировки');
|
||
try {
|
||
const [training, exerciseNames] = await Promise.all([
|
||
api(`/trainings/${trainingId}/`),
|
||
api('/exercises/'),
|
||
]);
|
||
body.replaceChildren(editForm(training, exerciseNames, {
|
||
onDone: () => { hide(); onChange(); },
|
||
onCancel: hide,
|
||
}));
|
||
} catch (error) {
|
||
body.replaceChildren(alert('danger', error.status === 404
|
||
? 'Тренировка не найдена.'
|
||
: `Не удалось загрузить тренировку: ${error.message}`));
|
||
}
|
||
}
|