f1tness_parser/frontend/js/edit-dialog.js

35 lines
1.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Modal with the training edit form
import { alert, api, h } from './common.js';
import { editForm } from './edit-form.js';
import { openModal } from './modal.js';
import { user } from './page.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, trainees, trainers] = await Promise.all([
api(`/trainings/${trainingId}/`),
api('/exercises/'),
// only a superuser may hand a training to another trainee
user.is_superuser ? api('/users/?role=trainee') : null,
api('/users/trainers/'),
]);
body.replaceChildren(editForm(training, exerciseNames, { trainees, trainers }, {
onDone: () => { hide(); onChange(); },
onCancel: hide,
}));
} catch (error) {
body.replaceChildren(alert('danger', error.status === 404
? 'Тренировка не найдена.'
: `Не удалось загрузить тренировку: ${error.message}`));
}
}