multy-stub/server/routers/questioneer/public/static/js/index.js
Primakov Alexandr Alexandrovich 1fcc5ed70d init Questionnaire
2025-03-11 23:50:50 +03:00

67 lines
2.7 KiB
JavaScript
Raw 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.

/* global $, window, document */
$(document).ready(function() {
// Функция для получения базового пути API
const getApiPath = () => {
// Извлекаем базовый путь из URL страницы
const pathParts = window.location.pathname.split('/');
// Если последний сегмент пустой (из-за /) - удаляем его
if (pathParts[pathParts.length - 1] === '') {
pathParts.pop();
}
// Путь до корня приложения
return pathParts.join('/') + '/api';
};
// Функция для загрузки списка опросов
const loadQuestionnaires = () => {
$.ajax({
url: getApiPath() + '/questionnaires',
method: 'GET',
success: function(result) {
if (result.success) {
renderQuestionnaires(result.data);
} else {
$('#questionnaires-container').html(`<p class="error">Ошибка: ${result.error}</p>`);
}
},
error: function(error) {
console.error('Error loading questionnaires:', error);
$('#questionnaires-container').html('<p class="error">Не удалось загрузить опросы. Пожалуйста, попробуйте позже.</p>');
}
});
};
// Функция для отображения списка опросов
const renderQuestionnaires = (questionnaires) => {
if (!questionnaires || questionnaires.length === 0) {
$('#questionnaires-container').html('<p>У вас еще нет созданных опросов.</p>');
return;
}
// Получаем базовый путь (для работы и с /questioneer, и с /ms/questioneer)
const basePath = window.location.pathname.endsWith('/')
? window.location.pathname
: window.location.pathname + '/';
const questionnairesHTML = questionnaires.map(q => `
<div class="questionnaire-item">
<h3>${q.title}</h3>
<p>${q.description || 'Нет описания'}</p>
<p>Создан: ${new Date(q.createdAt).toLocaleString()}</p>
<div class="questionnaire-links">
<a href="${basePath}admin/${q.adminLink}" class="btn btn-small">Редактировать</a>
<a href="${basePath}poll/${q.publicLink}" class="btn btn-small btn-primary" target="_blank">Смотреть как участник</a>
</div>
</div>
`).join('');
$('#questionnaires-container').html(questionnairesHTML);
};
// Инициализация страницы
loadQuestionnaires();
// Обновление данных каждые 30 секунд
setInterval(loadQuestionnaires, 30000);
});