Add random quiz version selection and update webhook integration

This commit is contained in:
2025-01-30 16:58:41 +02:00
parent f8bb71a184
commit 823c27e5ca
3 changed files with 28 additions and 19 deletions

View File

@@ -31,6 +31,18 @@
</p>
<button id="consentButton">Погоджуюсь з умовами, розпочати тест</button>
<script>
document.getElementById('consentButton').addEventListener('click', function() {
fetch('data/questions.json')
.then(response => response.json())
.then(data => {
const quizVersions = Object.keys(data.quizzes);
const randomVersion = quizVersions[Math.floor(Math.random() * quizVersions.length)];
localStorage.setItem('quizVersion', randomVersion);
window.location.href = 'quiz.html';
});
});
</script>
</div>
</body>
</html>

View File

@@ -42,7 +42,7 @@ function initResultsPage() {
}
function loadQuestions(version) {
fetch('../data/questions.json')
fetch('data/questions.json')
.then(response => response.json())
.then(data => {
const questions = data.quizzes[version];

View File

@@ -1,5 +1,7 @@
// This file handles the logic for the questionnaire, including collecting additional participant information after the quiz.
const WEBHOOK_URL = "https://n8n.olehomelchenko.com/webhook/kse-research";
document.addEventListener('DOMContentLoaded', function() {
const questionnaireForm = document.getElementById('questionnaire-form');
@@ -39,35 +41,30 @@ document.addEventListener('DOMContentLoaded', function() {
const quizId = 'quiz';
if (allQuizzes[quizId]) {
allQuizzes[quizId].participantData = participantData;
allQuizzes[quizId].iterationVersion = 'v1.0.0'; // Add semantic versioning
allQuizzes[quizId].iterationVersion = 'v1.0.1'; // Add semantic versioning
localStorage.setItem('allQuizzes', JSON.stringify(allQuizzes));
}
// Send data to the backend
fetch('/api/responses', {
// Combine quiz results and questionnaire data
const combinedData = {
allQuizzes: allQuizzes
};
// Send combined data to webhook
fetch(WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(allQuizzes)
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data.success) {
console.log('Success:', data);
window.location.href = 'results.html';
} else {
throw new Error(data.message || 'Failed to submit the form');
}
.then(() => {
alert('Анкету надіслано успішно');
window.location.href = 'results.html';
})
.catch((error) => {
console.error('Error:', error);
showError('Failed to submit the form. Please try again.');
console.error('Error sending data:', error);
alert('Сталася помилка при надсиланні анкети');
submitButton.disabled = false;
});
});