multy-stub/server/routers/questioneer/public/static/js/index.js
2025-03-12 00:37:32 +03:00

82 lines
3.4 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 = () => {
// Проверяем, содержит ли путь /ms/ (продакшн на dev.bro-js.ru)
const pathname = window.location.pathname;
const isMsPath = pathname.includes('/ms/questioneer');
if (isMsPath) {
// Для продакшна: если в пути есть /ms/, то API доступно по /ms/questioneer/api
return '/ms/questioneer/api';
} else {
// Для локальной разработки: формируем путь к API для главной страницы
// Убираем завершающий слеш, если он есть
const basePath = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;
// Путь до API приложения
return basePath + '/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 = (() => {
const pathname = window.location.pathname;
const isMsPath = pathname.includes('/ms/questioneer');
if (isMsPath) {
// Для продакшна: нужно использовать /ms/questioneer/ для ссылок
return '/ms/questioneer/';
} else {
// Для локальной разработки: используем текущий путь
return pathname.endsWith('/') ? pathname : 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);
});